1
votes

I am trying to export data from a Access 2016 Form to a Word document. Here is the code I'm using.

Public Function doWordAutomation()

On Error GoTo doWordAutomationErr

Dim objWordDoc As Word.Document

Dim objWord As Word.Application

Dim sDocument As String

sDocument = Application.CurrentProject.Path & "C:Desktop\No Notary Legal Dispatch Affidavit Fill.doc"
Set objWord = CreateObject("Word.Application")
Set objWordDoc = objWord.Documents.Open(Application.CurrentProject.Path & "\C:\Desktop\No Notary Legal Dispatch Affidavit Fill.doc")
If (sDocument) Then
    Kill sDocument
End If
objWordDoc.SaveAs sDocument

With objWordDoc.Bookmarks
    If .Exists("Cause") Then
        .Item("Cause").Range.Text = "Cause"
    If .Exists("Plaintiff") Then
        .Item("Plaintiff").Range.Text = "Plaintiff"
    If .Exists("Court") Then
        .Item("Court").Range.Text = "Court"
    If .Exists("County") Then
        .Item("County").Range.Text = "County"
    If .Exists("State") Then
        .Item("State").Range.Text = "State"
    If .Exists("Defendant") Then
        .Item("Defendant").Range.Text = "Defendant"
    If .Exists("Documents") Then
        .Item("Documents").Range.Text = "Documents"
    If .Exists("NameforService") Then
        .Item("NameforService").Range.Text = "NameforService"
    If .Exists("ServiceAddress") Then
        .Item("ServiceAddress").Range.Text = "ServiceAddress"
    If .Exists("ResultTime") Then
        .Item("ResultTime").Range.Text = "ResultTime"
    If .Exists("ResultDate") Then
        .Item("ResultDate").Range.Text = "ResultDate"
    End If
End

objWordDoc.Save
objWordDoc.Close

doWordAutomationExit:
Exit Function

doWordAutomationErr:

MsgBox Err.Description, vbOKOnly, Err.Source & ":" & Err.Number

Resume doWordAutomationExit

End Function

I have created a button in my form and attached this code to it but when I try to use it nothing happens. Any help that you can give will be greatly appreciated.

Private Sub cmdPrint_Click()
  'Print customer slip for current customer.
  Dim appWord As Word.Application
  Dim doc As Word.Document
  'Avoid error 429, when Word isn’t open.
  On Error Resume Next
  Err.Clear
  'Set appWord object variable to running instance of Word.
  Set appWord = GetObject(, "Word.Application")
  If Err.Number <> 0 Then
    'If Word isn’t open, create a new instance of Word. 
    Set appWord = New Word.Application
  End If
  Set doc = appWord.Documents.Open("C:C:Desktop\No Notary Legal Dispatch Affidavit Fill.doc", , True)
  With doc
    .FormFields("Cause").Result = Me!Cause
    .FormFields("Plaintiff").Result = Me!Plaintiff
    .FormFields("Court").Result = Me!Court
    .FormFields("County").Result = Me!County
    .FormFields("State").Result = Me!State
    .FormFields("Defendant").Result = Me!Defendant
    .FormFields("Documents").Result = Me!Documents
    .FormFields("NameforService").Result = Me!NameforService
    .FormFields("ServiceAddress").Result = Me!ServiceAddress
    .FormFields("ResultTime").Result = Me!ResultTime
    .FormFields("ResultDate").Result = Me!ResultDate
    .Visible = True
    .Activate
  End With
  Set doc = Nothing
  Set appWord = Nothing
  Exit Sub

errHandler:
  MsgBox Err.Number & ": " & Err.Description
End Sub

I have created bookmarks in a Word document that I'm wanting to export the form information into. Neither code I used worked for me so any help would be greatly appreciated.

For some reason, it is still not working. I don't know if it is the button that I have put in the form, which is a command76 button. I know that's not the exact right button I need to export but it's the closest one I see that I'm able to use. I have attached the access document and word document that I'm trying to use. The Word form with bookmarks Word Document and the Access Form Access Document have been linked here. Thanks again in advance for your help. Attached are two of the documents I'm trying to use.

1
(1) I get an error with your code because of the way you are trying to build the path. Why are you combining the 'CurrentProject' path plus 'C:\....'??? (2) Why not just use 'sDocument ' in the 'Open' command rather than repeat? (3) What is 'If (sDocument) Then' for? That gives an error also. (4) There is no 'End With'... just an 'End' (5) All of your 'If' statements either need 'ELSE' or you need to have that command on the same line as the 'If'Wayne G. Dunn
The first code came from here microsoftaccessexpert.com/… and the second code came from here techrepublic.com/blog/microsoft-office/…user7624466
See my original post for answers to your previous questions and my post right above this one has the links. Thanks in advance for your help.user7624466
I have a Word document that has blank information in it that needs to be filled in so that it can be submitted to the courts. I use Access forms that have all this information that is missing (ex: plaintiff v defendant) along with other information. I want the (plaintiff) exported into the Word document where I have a bookmark named Plaintiff and so on and so forth with all ten bookmarks. This will save a ton of time if I don't have to fill out each affidavit but instead just click on a button on the form which already contains all the information and have it export that information.user7624466
See below to what happened when I put your coding in. I have also attached links to access the specific documents that I'm working with. I'm not sure if its still the code or if its the button I'm trying to use.user7624466

1 Answers

1
votes

Now that you have shared the document and names, I changed the code to use the data from your form. You may need to tweak the document spacing or the data you insert. Let me know how it goes.

Also, I suggest you clean up this thread by deleting the unnecessary descriptions and comments.

Option Compare Database
Option Explicit

Private Sub Command75_Click()
    Export_Form_Data_To_Word
End Sub


Public Function Export_Form_Data_To_Word()
Dim objWordDoc  As Word.Document
Dim objWord     As Word.Application
Dim objRange    As Word.Range
Dim sPath       As String
Dim sFileName   As String
Dim sSaveAs     As String
Dim sDocument   As String
Dim i           As Integer
    On Error GoTo Error_Trap

    ' For my testing....
    'sPath = "C:\temp\"                  '
    'sFileName = "NoNotaryLegalDispatchAffidavitFill.docx"        '

    sPath = "C:\Users\Josh Panger\Desktop"                  '
    sFileName = "No Notary Legal Dispatch Affidavit Fill.docx"        '
    i = InStrRev(sFileName, ".doc")        '
    ' Create a new file name
    sSaveAs = Left(sFileName, i - 1) & "_" & Format(Now(), "YYYYMMDD_HHMMSS") & Mid(sFileName, i)
    Set objWord = CreateObject("Word.Application")

    objWord.Visible = True

    Set objWordDoc = objWord.Documents.Open(sPath & sFileName)

    With objWordDoc.Bookmarks
        If .Exists("Cause") Then
            objWordDoc.Bookmarks("Cause").Range.InsertAfter Me.Cause
        Else
            MsgBox "Bookmark: 'Cause' does not exist in this Word Document!", vbOKOnly, "Missing Bookmark"
        End If

        If .Exists("Plaintiff") Then
            objWordDoc.Bookmarks("Plaintiff").Range.InsertAfter Me.Plaintiff & ", Plaintiff"
        Else
            MsgBox "Bookmark: 'Plaintiff' does not exist in this Word Document!", vbOKOnly, "Missing Bookmark"
        End If

        If .Exists("Defendant") Then
            objWordDoc.Bookmarks("Defendant").Range.InsertAfter Me.Defendant & ", Defendant"
        Else
            MsgBox "Bookmark: 'Defendant' does not exist in this Word Document!", vbOKOnly, "Missing Bookmark"
        End If

        If .Exists("Court") Then
            objWordDoc.Bookmarks("Court").Range.InsertAfter Me.Count
        Else
            MsgBox "Bookmark: 'Court' does not exist in this Word Document!", vbOKOnly, "Missing Bookmark"
        End If
        If .Exists("County") Then
            objWordDoc.Bookmarks("County").Range.InsertAfter Me.County
        Else
            MsgBox "Bookmark: 'County' does not exist in this Word Document!", vbOKOnly, "Missing Bookmark"
        End If
        If .Exists("State") Then
            objWordDoc.Bookmarks("State").Range.InsertAfter "My State"
        Else
            MsgBox "Bookmark: 'State' does not exist in this Word Document!", vbOKOnly, "Missing Bookmark"
        End If

        If .Exists("Documents") Then
            objWordDoc.Bookmarks("Documents").Range.InsertAfter Me.Documents
        Else
            MsgBox "Bookmark: 'Documents' does not exist in this Word Document!", vbOKOnly, "Missing Bookmark"
        End If

        If .Exists("NameforService") Then
            objWordDoc.Bookmarks("NameforService").Range.InsertAfter Me.NameforService
        Else
            MsgBox "Bookmark: 'NameforService' does not exist in this Word Document!", vbOKOnly, "Missing Bookmark"
        End If

        If .Exists("ServiceAddress") Then
            objWordDoc.Bookmarks("ServiceAddress").Range.InsertAfter Me.ServiceAddress
        Else
            MsgBox "Bookmark: 'ServiceAddress' does not exist in this Word Document!", vbOKOnly, "Missing Bookmark"
        End If

        If .Exists("ResultTime") Then
            objWordDoc.Bookmarks("ResultTime").Range.InsertAfter Me.ResultTime
        Else
            MsgBox "Bookmark: 'ResultTime' does not exist in this Word Document!", vbOKOnly, "Missing Bookmark"
        End If

        If .Exists("ResultDate") Then
            objWordDoc.Bookmarks("ResultDate").Range.InsertAfter Me.ResultDate
        Else
            MsgBox "Bookmark: 'ResultDate' does not exist in this Word Document!", vbOKOnly, "Missing Bookmark"
        End If

    End With

    objWordDoc.SaveAs2 sPath & sSaveAs, 16
    objWordDoc.Close

Exit_Code:
    Exit Function

Error_Trap:

    Debug.Print Err.Number & vbTab & Err.Description
    If Err.Number = 5174 Then
        MsgBox "The Word document can't be found at location: '" & sDocument & "'", vbOKOnly, "Missing File"
    Else
        MsgBox Err.Number & vbTab & Err.Description, vbOKOnly, Err.Source & ":" & Err.Number
    End If
    Resume Exit_Code
    Resume
End Function