Hello I have used the code below for years in Access 2003 (with Word 2003)
When attempting to use it in Office 2013, it causes Access to crash.
Could someone please help me to modify the code so I can continue using it.
Basically it populates the open word document (which has previously been saved with the Access field names inserted between slashes).
It works well and makes available all fields from the query bound to the Access form.
For example I would create a word document with the text: Dear Mr ///FullName/// When the document is opened, the code looks for the field names between the slashes and would produce Dear Mr John Smith.
Many thanks for any assistance. Regards Mark
When attempting to use it in Office 2013, it causes Access to crash.
Could someone please help me to modify the code so I can continue using it.
Basically it populates the open word document (which has previously been saved with the Access field names inserted between slashes).
It works well and makes available all fields from the query bound to the Access form.
For example I would create a word document with the text: Dear Mr ///FullName/// When the document is opened, the code looks for the field names between the slashes and would produce Dear Mr John Smith.
Many thanks for any assistance. Regards Mark
Code:
Public Function CreateWordDocument(ByVal strWordFilename As String, ByVal frmCurrentForm As Form)
Dim appWordApp As WORD.Application
Dim appWordDoc As WORD.Document
Dim appWordStory As WORD.Range
Dim strFieldName As String
If Dir(strWordFilename) <> "" And strWordFilename <> "" Then
'setup word application
Set appWordApp = New WORD.Application
Set appWordDoc = appWordApp.Documents.Open(strWordFilename)
'setup recordset and get current record from form
Dim rsCurrentForm As DAO.Recordset
Set rsCurrentForm = frmCurrentForm.Recordset
rsCurrentForm.Bookmark = frmCurrentForm.Bookmark
'replace in word , each story range
For Each appWordStory In appWordDoc.StoryRanges
With appWordStory.Find
.ClearFormatting
.Forward = True
.MatchCase = False
.MatchWildcards = True
.Wrap = wdFindContinue
Do
.Text = "///*///"
.Execute
If .Found Then
strFieldName = Replace(Mid(appWordStory, 4, Len(appWordStory) - 6), Chr(146), Chr(39))
On Error Resume Next
strCurrentfield = CStr(Nz(rsCurrentForm.Fields(strFieldName).Value))
If Err = 0 Then
strCurrentfield = Replace(strCurrentfield, Chr(13) + Chr(10), Chr(13))
appWordStory.Text = strCurrentfield
Else
appWordStory.Text = ""
End If
Err = 0
On Error GoTo 0
End If
Loop Until Not .Found
End With
Next
'show word
appWordDoc.Parent.Visible = True
End If
End Function