Insert file into Word at Bookmark preserving RichText formatting (1 Viewer)

EonsTimE

Registered User.
Local time
Today, 04:27
Joined
May 8, 2016
Messages
13
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.

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?
 

sneuberg

AWF VIP
Local time
Yesterday, 20:27
Joined
Oct 17, 2014
Messages
3,506
We did something like that. It's really sad that you have to create a file to do this sort of thing. Anyway here's some segments of the code we have. In our case we put the html in a Word text box.

Code:
Dim What As Word.Shape

Set apWord = CreateObject("Word.application")
Set doc = apWord.Documents.Add("Normal", False, 0)

Set What = doc.Shapes.AddTextbox(msoTextOrientationHorizontal, doc.PageSetup.LeftMargin, 200, 240, 0)
Dim sPath As String
sPath = CurrentProject.Path & "\Temp.html"
Open sPath For Output As 1
Print #1, "<HTML>" & pprs!What & "</HTML>"
Close #1
What.TextFrame.TextRange.InsertFile (sPath)
What.TextFrame.AutoSize = True
What.Line.Visible = False
What.TextFrame.MarginLeft = 0 'This puts the text right on the margin
What.TextFrame.TextRange.Paragraphs.SpaceAfter = 0
What.TextFrame.TextRange.Font.Size = 12
 
Last edited:

EonsTimE

Registered User.
Local time
Today, 04:27
Joined
May 8, 2016
Messages
13
We did something like that. It's really sad that you have to create a file to do this sort of thing. Anyway here's some segments of the code we have. In our case we put the html in a Word text box.

Thanks for sharing, sneuberg. You've pointed me in the right direction, also your approach will be useful to me in the future ^^

I'll return the favour and hopefully you'll find these useful as well.

Injecting using Range

Code:
Set doc = appWord.Documents.Open(document.docx, , True)
Dim rawrnge As Range

    Set rawrnge = doc.Range(0, 0) 'beginning of document
    rawrnge.InsertFile filename:=sPath, Range:="", ConfirmConversions:=False, Link:=False, Attachment:=False

Injecting using Bookmarks

Code:
Set doc = appWord.Documents.Open(document.docx, , True)
Dim bkmrkrngeStart As Range
Dim bkmrkrngeEnd As Range
Dim bkmrkRnge As Range

    Set bkmrkrngeStart = ActiveDocument.Bookmarks("Text17").Range
    Set bkmrkrngeEnd = ActiveDocument.Bookmarks("Text17").Range
    Set bkmrkRnge = doc.Range(bkmrkrngeStart.Start, bkmrkrngeEnd.End)
    bkmrkRnge.InsertFile filename:=sPath, Range:="", ConfirmConversions:=False, Link:=False, Attachment:=False
 

Users who are viewing this thread

Top Bottom