Hi, I'm trying to insert a RichText field into a Word document at bookmark by first exporting the field contents into a temp HTML file and then inserting the HTML file into Word.
The export to HTML works, Access opening Word document and finding the Bookmark works, but what doesn't work is inserting the text file at bookmark.
There's no error message, there just nothing happens.
The .Selection.InsertFile seems pretty straightforward to use, I can't figure out why it's failing. I know I could strap HTML tags from my string and just paste pure text, but I'd rather keep the RichText fromatting if possible.
Anyone able to troubleshoot this?
The export to HTML works, Access opening Word document and finding the Bookmark works, but what doesn't work is inserting the text file at bookmark.
There's no error message, there just nothing happens.
Code:
Private Sub btnPrintForm_Click()
' Do stuff after button pressed
Dim appWord As Word.Application
Dim doc As Word.Document
'Avoid error 429, when Word isn't open.
On Error Resume Next
Err.Clear
' HTML wrapper code
Dim fso As Object ' FileSystemObject
Dim f As Object ' TextStream
Dim myHtml As String
Dim tempFileSpec As String
' Retrieve RichText field from the open form
myHtml = Me!txtRichTextField
Set fso = CreateObject("Scripting.FileSystemObject") ' New FileSystemObject
tempFileSpec = fso.GetSpecialFolder(2) & "\" & fso.GetTempName & ".htm"
' write to temporary .htm file
Set f = fso.CreateTextFile(tempFileSpec, True)
f.Write "<html>" & myHtml & "</html>"
f.Close
Set f = Nothing
'Open Word document and do stuff
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
appWord.Visible = True
Set doc = appWord.Documents.Open("C:\somedocument.docx", , True)
' Select a Bookmark and try injecting file contents into it
With doc
.Bookmarks("Text18").Select
.Selection.InsertFile FileName:=tempFileSpec, ConfirmConversions:=Flase
'.Selection.InsertFile FileName:="C:\blah.txt", ConfirmConversions:=False ' Trying to insert some plain text doesn't work either
.FormFields("Text1").Result = Me!txtRichTextField ' Directly injecting a text box control contents into a Word bookmark works, but there are HTML tags all over my string
.Visible = True
.Activate
End With
Set doc = Nothing
Set appWord = Nothing
Exit Sub
errHandler:
MsgBox Err.Number & ": " & Err.Description
End Sub
The .Selection.InsertFile seems pretty straightforward to use, I can't figure out why it's failing. I know I could strap HTML tags from my string and just paste pure text, but I'd rather keep the RichText fromatting if possible.
Anyone able to troubleshoot this?