none
Problems with Access to Word communication - Inlineshapes RRS feed

  • Question

  • Hi,

    I have a lot of data in Acess that I use to generate a Word document built on a template. I insert Inlineshapes in the Word document. I am using Word bookmarks and I use a table with 3 cells where I have the bookmarks.I am also generating some data and the code is generating a Word table to display this data.
    The code works fine the first time I am running the code. The Pictures are inserted. The table is created and all other data too.
    The second time everything else works except for the pictures in the Word tables and creating a Word table with code. After each time I generate the report I have to restart Access in order to generate Another report. To close Word application doesnt help.
    Any idea where to start looking? I have in the end the code

        wordApp.Close
        Set wordApp = Nothing

    So I guess this should be enough to close the object and be able to start all over again but no.

    Would very much appreciate your help.

    Hugo Jörgensen



    Hugo Joergense

    Thursday, May 4, 2017 12:11 PM

Answers

  • But I still have a problem when creating my table. The line below also produces the error 462. I think that I have to put a reference to an object in some way in order to create the table but I dont know how to write the code.

    wordApp.ActiveDocument.Tables.Add Range:=Selection.Range, NumRows:=1, NumColumns:= _
                            3, DefaultTableBehavior:=wdWord9TableBehavior, AutoFitBehavior:= _
                            wdAutoFitFixed

    The Selection property also requires an object reference, so you should write this line as:

    wordApp.ActiveDocument.Tables.Add _
        Range:=wordApp.Selection.Range, _
        NumRows:=1, _
        NumColumns:= 3, _
        DefaultTableBehavior:=wdWord9TableBehavior, _
        AutoFitBehavior:= wdAutoFitFixed

    Does that fix the problem?


    Dirk Goldgar, MS Access MVP
    Access tips: www.datagnostics.com/tips.html

    Tuesday, May 9, 2017 4:28 PM

All replies

  • The appropriate method for terminating Word is to use the Quit method. It would appear that all you are doing is closing a Document object (although I would need to see how wordApp is defined in your code).

    Also keep in mind that any Word objects you create, whether explicitly or implicitly, must be properly destroyed or there is a good chance that the Word application will remain in memory.


    Paul ~~~~ Microsoft MVP (Visual Basic)

    Thursday, May 4, 2017 12:23 PM
  • Thanks for you reply Paul,

    I tested the Quit method but that doesnt help.

    I have submitted the code in this link but there is a lot of code Im afraid.

    https://1drv.ms/w/s!Ag5qUp2GXsJ8goELUoK3b8HsXrMICg

    In the document the code regarding the Inlineshapes and the tables are in bold.

    Best regards

    Hugo


    Hugo Joergense

    Thursday, May 4, 2017 7:31 PM
  • Hi Hugo Jorgensen,

    from your description it's looks like issue related with code below.

     For i = 1 To Antal_hotell
                Select Case i
                    Case 1
                        wordApp.Selection.Goto What:=wdGoToBookmark, Name:="BMHotell1"
                        wordApp.Selection.TypeText Text:=CStr(Me.Hotell)
                        wordApp.Selection.TypeParagraph
                        If Me.Adress_1 <> "" Then
                            wordApp.Selection.TypeText Text:=CStr(Me.Adress_1)
                            wordApp.Selection.TypeParagraph
                        End If
                        If Me.Adress_2 <> "" Then
                            wordApp.Selection.TypeText Text:=CStr(Me.Adress_2)
                            wordApp.Selection.TypeParagraph
                        End If
                        For J = 1 To 4
                            Select Case J
                                Case 1
                                    Akt_bokmarke = "BMHotellBilder" & J
                                    wordApp.Selection.Goto What:=wdGoToBookmark, Name:=Akt_bokmarke
                                    Set Bild = wordApp.Selection.InlineShapes.AddPicture(Me.Bildnr_1, False)
                                Case 2
                                    Akt_bokmarke = "BMHotellBilder" & J
                                    wordApp.Selection.Goto What:=wdGoToBookmark, Name:=Akt_bokmarke
                                    Set Bild = wordApp.Selection.InlineShapes.AddPicture(Me.Bildnr_2, False)
                                Case 3
                                    Akt_bokmarke = "BMHotellBilder" & J
                                    wordApp.Selection.Goto What:=wdGoToBookmark, Name:=Akt_bokmarke
                                    Set Bild = wordApp.Selection.InlineShapes.AddPicture(Me.Bildnr_3, False)
                                    Set Bild = Nothing
                                Case 4
                                    Akt_bokmarke = "BMHotellKartbild" & J
                                    wordApp.Selection.Goto What:=wdGoToBookmark, Name:=Akt_bokmarke
                                    If Not IsNull(Me.Kartbild) Then
                                        Set Bild = wordApp.Selection.InlineShapes.AddPicture(Me.Kartbild, False)
                                        Bildbredd = CentimetersToPoints(16.2)
                                        Bildhojd = CentimetersToPoints(5)
                                    End If
                            End Select
                            With Bild
                                .Width = Bildbredd
                                .Height = Bildhojd
                            '    .LockAspectRatio = True
                            End With
                        Next J
                End Select
                Set Bild = Nothing
            Next i
    

    I can see that you are using 2 switch cases in this part. the first case have only one case. if it only contain one case then why you need to use switch case there?

    is the inner part of the switch case where you add the image get executed every time?

    if you did not check it then first try to debug the code and try to check it.

    check what is the difference in the execution of the code in first and second time.

    it will help you to find the issue and you need to correct your code so it execute in a same way every time.

    let us know about your debugging result. so that we can try to provide you further suggestions.

    also one thing I did not understand that why you need to restart access every time to create new report?

    Regards

    Deepak


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Friday, May 5, 2017 3:19 AM
    Moderator
  • Thanks Deepak

    Regarding the need to restart Access - its because when I do that the report works fine.

    I did some more debugging and found that after I run the code the second time and insert the first Inlineshape I get the error 462. I have searched online for answers and it seems to be connected with object variables. One thing I tried was to save the document before running the rest of the coda and I changed the variable B ild from Word.Inlineshape to Object but still the same problem. I removed a lot of code and here is whats left so the problem is in this code. I cannot figure out myself where the problem might be.

        Dim Bildtest As Shape
        Dim Bild As Object, Bildbredd As Single, Bildhojd As Single
        Dim wordApp As Object
       
        Set wordApp = CreateObject("Word.Application")
        wordApp.Visible = True
        wordApp.Documents.Add Template:="E:\Mallar\Offert.dotx", NewTemplate:=False, DocumentType:=0, Visible:=True
        wordApp.ActiveDocument.SaveAs "E:\Mallar\test8.docx"
        Err.Clear
        wordApp.Selection.Goto What:=wdGoToBookmark, Name:="BMPicturePage1"
        On Error Resume Next
        Set Bild = wordApp.Selection.InlineShapes.AddPicture(Me.Forsattsblad, False)
        Bildbredd = CentimetersToPoints(20)
        Bildhojd = CentimetersToPoints(22)
        Bild.Width = Bildbredd
        Bild.Height = Bildhojd
        Set Bild = Nothing
        wordApp.Selection.Goto What:=wdGoToBookmark, Name:="BMOffertTxtSid1"
        wordApp.Selection.TypeText Text:=CStr(Kundtext)
        wordApp.Selection.TypeParagraph
        wordApp.Selection.ParagraphFormat.SpaceBefore = 0
        wordApp.Selection.Font.Bold = wdToggle
        wordApp.Selection.Font.Size = 22
        wordApp.Selection.TypeText Text:=CStr(Me.Beskrivning)
       
        ' Destinationsbilder
        Bildbredd = CentimetersToPoints(5)
        Bildhojd = CentimetersToPoints(5.1)
        wordApp.Selection.Goto What:=wdGoToBookmark, Name:="BMTreBilder1"
        Set Bild = wordApp.Selection.InlineShapes.AddPicture(Me.Bild_1, False)
        Bild.Width = Bildbredd
        Bild.Height = Bildhojd
        Set Bild = Nothing
        wordApp.Selection.Goto What:=wdGoToBookmark, Name:="BMTreBilder2"
        Set Bild = wordApp.Selection.InlineShapes.AddPicture(Me.Bild_2, False)
        Bild.Width = Bildbredd
        Bild.Height = Bildhojd
        Set Bild = Nothing
        wordApp.Selection.Goto What:=wdGoToBookmark, Name:="BMTreBilder3"
        Set Bild = wordApp.Selection.InlineShapes.AddPicture(Me.Bild_3, False)
        Bild.Width = Bildbredd
        Bild.Height = Bildhojd
        Set Bild = Nothing
        ' infoga säljaruppgifter på försättsbladet
        wordApp.Selection.Goto What:=wdGoToBookmark, Name:="BMSaljare"
        wordApp.Selection.TypeText Text:=CStr(Ansvarig)
        wordApp.Selection.MoveDown Unit:=wdLine, Count:=1
        wordApp.Selection.TypeText Text:=CStr(Titel)
        wordApp.Selection.MoveDown Unit:=wdLine, Count:=1
        wordApp.Selection.TypeText Text:=CStr(Telefon)
        wordApp.Selection.MoveDown Unit:=wdLine, Count:=1
        wordApp.Selection.TypeText Text:=CStr(Epostadress)
        On Error Resume Next
        wordApp.Selection.Goto What:=wdGoToBookmark, Name:="BMDatum"
        wordApp.Selection.TypeText Text:=CStr(Datumtext)
        wordApp.Selection.Goto What:=wdGoToBookmark, Name:="BMAntal"
        wordApp.Selection.TypeText Text:=CStr(Me.Antal_angivet & Persontext)
        wordApp.Selection.Goto What:=wdGoToBookmark, Name:="BMDestinationBeskrivning"
        wordApp.Selection.TypeText Text:=CStr(Me.Beskrivning_destination)
        wordApp.Selection.Goto What:=wdGoToBookmark, Name:="BMTransport"
        wordApp.Selection.TypeText Text:=CStr(Me.Transportinfo_kund)
        Err.Clear
        Set Bild = Nothing
       
        wordApp.Activate
        wordApp.ActiveDocument.Save
        Set wordApp = Nothing
        DoCmd.Hourglass False


    Hugo Joergense

    Friday, May 5, 2017 9:29 AM
  • I solved one problem in my code.
    The code Bildbredd = CentimetersToPoints(5) should be changed to

    Bildbredd = wordApp.CentimetersToPoints(5). Since Im working in Word I have to use the reference to the function in Word instead of Access.

    But I still have a problem when creating my table. The line below also produces the error 462. I think that I have to put a reference to an object in some way in order to create the table but I dont know how to write the code.

    wordApp.ActiveDocument.Tables.Add Range:=Selection.Range, NumRows:=1, NumColumns:= _
                            3, DefaultTableBehavior:=wdWord9TableBehavior, AutoFitBehavior:= _
                            wdAutoFitFixed


    Hugo Joergense

    Friday, May 5, 2017 6:24 PM
  • Hi Hugo Jorgensen,

    did you try to create object of Word document instead of directly using the "ActiveDocument".

    you can try to create document object.

    then when you open the document in code assign it to this object.

    and then try to add table using this object.

    it is possible that your code not able to find the Active Document.

    Regards

    Deepak


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Tuesday, May 9, 2017 9:25 AM
    Moderator
  • But I still have a problem when creating my table. The line below also produces the error 462. I think that I have to put a reference to an object in some way in order to create the table but I dont know how to write the code.

    wordApp.ActiveDocument.Tables.Add Range:=Selection.Range, NumRows:=1, NumColumns:= _
                            3, DefaultTableBehavior:=wdWord9TableBehavior, AutoFitBehavior:= _
                            wdAutoFitFixed

    The Selection property also requires an object reference, so you should write this line as:

    wordApp.ActiveDocument.Tables.Add _
        Range:=wordApp.Selection.Range, _
        NumRows:=1, _
        NumColumns:= 3, _
        DefaultTableBehavior:=wdWord9TableBehavior, _
        AutoFitBehavior:= wdAutoFitFixed

    Does that fix the problem?


    Dirk Goldgar, MS Access MVP
    Access tips: www.datagnostics.com/tips.html

    Tuesday, May 9, 2017 4:28 PM
  • Thanks VERY MUCH!

    That solved the problem!


    Hugo Joergense

    Sunday, May 14, 2017 6:51 PM