Share via

Create a button in Access form to open word document with data from from

Anonymous
2015-08-18T10:31:10+00:00

Gents,

i have found a code for button in Access form to open word document with few data fields from that form. when set up the code in event procedures(On Click) of button it does shoe me below error.

"the expression on click you entered as the event property setting produced the following error: Ambiguous name detected: fillwordform."

here is the code i wrote into button,

Function fillwordform()

Dim appword As Word.Application

Dim doc As Word.Document

Dim Path As String

On Error Resume Next

Error.Clear

Set appword = GetObject(, "Word.Application")

If Err.Number <> 0 Then

Set appword = New Word.Application

appword.Visible = True

End If

Path = "C:\Users\koshala.arachchige\Desktop\Koshala.doc"

Set doc = appword.DOCUMENTS.Open(Path, , True)

With doc

    .FormFields("txtLastName").Result = Me.LastName

appword.Visible = True

appword.Activate

End With

Set doc = Nothing

Set appword = Nothing

End Function

Private Sub Command1026_Click()

Call fillwordform

End Sub

would you please help me to find out where i have been wrong. i tried so many time but still getting the same error.

many thanks.

Microsoft 365 and Office | Access | For home | Windows

Locked Question. This question was migrated from the Microsoft Support Community. You can vote on whether it's helpful, but you can't add comments or replies or follow the question.

0 comments No comments

4 answers

Sort by: Most helpful
  1. Anonymous
    2015-08-18T12:50:21+00:00

    ciao Koshala,

    try to insert the specific bookmark in word file, revising your routine in this way :

    Option Compare Database

    Option Explicit

    Private Sub Comando37_Click()

    Call fillwordform

    End Sub

    Private Sub fillwordform()

     Dim appword As Object 'Word.Application

     Dim doc     As Object 'Word.Document

     Dim fPath   As String

      On Error Resume Next

      Set appword = GetObject(, "Word.Application")

      Error.Clear

     If Err.Number <> 0 Then

        Set appword = CreateObject("Word.Application")

     End If

    appword.Visible = True

    fPath = ""C:\Users\koshala.arachchige\Desktop\Koshala.doc"

    Set doc = appword.Documents.Open(fPath)

     appword.Activate

     With doc

            .Bookmarks("yourBookMarksName").Select

            .Application.Selection.Insertafter Me.LastName

         '.FormFields("txtLastName").Result = Me.LastName

     End With

     Set doc = Nothing

     Set appword = Nothing

     End Sub

    2 people found this answer helpful.
    0 comments No comments
  2. Anonymous
    2015-08-18T18:09:16+00:00

    ciao Koshala,

    sorry not sure to have got well your meaning about "inside text box".

    Anyway to implement a little bit, I suggest to create a template ( file with .dotx extension  instead than doc).

    after that try to apply this new routine :

    Option Compare Database

    Option Explicit

    Private Sub Comando37_Click()

    Call fillwordform

    End Sub

    Private Sub fillwordform()

     Dim appword As Word.Application

     Dim doc     As Word.Document

     Dim fPath   As String

     Dim ReplSel As Boolean

     On Error Resume Next

     Set appword = GetObject(, "Word.Application")

     Error.Clear

     If Err.Number <> 0 Then

        Set appword = CreateObject("Word.Application")

     End If

     On Error GoTo 0

    fPath = "C:\Users\koshala.arachchige\Desktop\Koshala.docx"

     appword.Visible = True

        appword.Activate

        ReplSel = appword.Options.ReplaceSelection

        appword.Options.ReplaceSelection = True

        Set doc = appword.Documents.Add(fPath)

        doc.Activate

        doc.Bookmarks("bknome").Select

        appword.Selection.TypeText Me.Nome

     appword.Options.ReplaceSelection = ReplSel

    Set appword = Nothing

    Set doc = Nothing

    End Sub

    I think a more significant reply should be have the choice to see a Whole word exporation from Access.

    Get a look at this example it contains a file showing how to export apply bookmarks way : http://web.mclink.it/MC5884/  

    section general : 6.137 Esempio di automazione per l'invio e la formattazione di dati da Access a Word usando i bookmarks

    HTH.

    Ciao, Sandro.

    0 comments No comments
  3. Anonymous
    2015-08-18T15:00:49+00:00

    Hi Sandro,

    thank you very much for helping me out in this matter.

    can you also help in below,

    as your code state ".Application.Selection.Insertafter Me.LastName" my field record is showing after the text form field in word file. can have my record shows in inside the  text form field instead. probably change word(Insertafter) in above code.

    thanks.

    0 comments No comments
  4. ScottGem 68,810 Reputation points Volunteer Moderator
    2015-08-18T11:37:44+00:00

    The code should probably go in a global module as a Public Function and then called in your button click. The error message indicates you may have 2 functions with the same name.

    0 comments No comments