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.