Copy Rich Text field to Word

Peter T
NewLounger
Posts: 18
Joined: 27 Dec 2021, 12:17

Copy Rich Text field to Word

Post by Peter T »

I need to copy or transfer formatted text from a Rich Text Field in an Access-DB Record to a Bookmark'd location in a Word document. Without the formatting it would simply be a matter of reading the text (with SQL / DAO) and writing to Word as plain text.

FWIW the only two formats are a yellow Highlight over some of the text and font colour of the entire text. How can I keep this formatting when transferred to Word...?

TIA,
Peter

User avatar
HansV
Administrator
Posts: 78545
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Copy Rich Text field to Word

Post by HansV »

The idea is to create a temporary text file with extension .htm
Write "<html>" & RichTextField & "</html>" to the text file.
Insert the .htm file into the range of the bookmark.
Delete the temporary file.

See for example the last reply in Insert Access 2010 Rich text from Memo field to Word using vba and the long reply by Josef P. in Rich Text into Word document.
Best wishes,
Hans

Peter T
NewLounger
Posts: 18
Joined: 27 Dec 2021, 12:17

Re: Copy Rich Text field to Word

Post by Peter T »

In a light test that works, thank you Hans, and Josef!

I asked AI (Copilot) and it came up with this -
Dim fldRTF as Field, bk As Word.Bookmark
Set fld = rst![RTF]
bk.Range.FormattedText = fldRTF.Value

If this worked it'd be so simple and much faster, but it fails with a #424 Object error.
FWIW bk.Range = "abc" works and so does Debug.? fldRTF.Value as expected, so my objects look fine. It seems FormattedText expects a Range object.

I tried embracing the rtf in the field with <html> rtf </html> but that doesn't help, no do doubt because they're not real format tags; but not sure how to add and embed as tags in the rtf, or even if possible if it helps.

Would you have any ideas how to make this direct approach work...?

Peter

User avatar
HansV
Administrator
Posts: 78545
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Copy Rich Text field to Word

Post by HansV »

The problem is that a Rich Text field contains HTML markup, but Word does not support placing HTML directly into a document. It does support inserting a HTML file, hence the method described in those links...
Best wishes,
Hans

Peter T
NewLounger
Posts: 18
Joined: 27 Dec 2021, 12:17

Re: Copy Rich Text field to Word

Post by Peter T »

Understood, thanks again
Peter