Getting formatted text into Word

BobSullivan
3StarLounger
Posts: 235
Joined: 08 Jun 2010, 20:03
Location: Morgantown, PA

Getting formatted text into Word

Post by BobSullivan »

Hello,

I've taken over a database that essentially produces bid proposals. Suffice to say that the database is an mde file, and I can't see how things were originally done. There is an existing table with lots of document parts in it. A user goes through the list of document parts, selecting what they want to include in the bid (thanks Hans for helping me on that one). The bid parts are rich text memo fields. I've decided to build a one-time table for each bid containing all the verbiage in it. when the user is finished assembling the bid parts, they create a bid by clicking a button. This bid is stored as a word document.

The rich text fields look very nice in the access form, like this:
Our company

I insert the code into a new word document using the following command:

objWord.Documents.Add
FilePath = rst!QuotesBaseDir & Int(Me.QuoteIDPK / 1000) & "\"
objWord.ActiveDocument.SaveAs (FilePath & Me.QuoteIDPK)
objWord.ActiveDocument.Range.InsertAfter (Me.QuoteDocument)
objWord.Visible = True

where QuoteDocument is the field on the form that contains the concatenated bids. Quotedocument is an unbound text box with the text format property set to Rich Text.

But when the word document opens up, the text looks like this:

<div><font face="Bodoni MT Black" size=5>Our Company; AIR</font></div>

What happenned to my nice formatting, and where did all those tags come from?
Cordially,

Bob Sullivan
Elverson, PA

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

Re: Getting formatted text into Word

Post by HansV »

A text box set to Rich Text actually contains HTML; Access renders the HTML as formatted text.
The line

objWord.ActiveDocument.Range.InsertAfter (Me.QuoteDocument)

inserts the HTML source code into the Word document. There is no built-in way to transfer the formatted text to Word, but you can copy and paste it:

Code: Select all

    With Me.QuoteDocument
        .SetFocus
        .SelStart = 0
        .SelLength = Len(.Text)
    End With
    RunCommand acCmdCopy
    Dim rng As Object
    Set rng = objWord.ActiveDocument.Content
    rng.Collapse Direction:=0 ' wdCollapseEnd
    rng.Paste
Best wishes,
Hans

BobSullivan
3StarLounger
Posts: 235
Joined: 08 Jun 2010, 20:03
Location: Morgantown, PA

Re: Getting formatted text into Word

Post by BobSullivan »

It still looks like html with the tags. Doesn't have the formatting.

But if I manually copy and paste it, the formatting comes across. (You know, the old Ctrl+C, Crtl+V)

Is there a better way to do this? Is there a way to send formatting from Access to Word? Use rtf codes or something like that? Or is there a way to send word formatting codes so that the document will be formatted?

I really don't need much formatting: bold, italic, change point sizes, things like that.

Thanks for any help...
Cordially,

Bob Sullivan
Elverson, PA

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

Re: Getting formatted text into Word

Post by HansV »

Hmmm - I tested the code that I posted on a form with a memo field set to rich text. The code copied the formatted text (bold, italic, color) to Word, not the HTML source code. (In fact, it simulates copy and paste). Could you try it again? If it still fails, could you attach a stripped down and zipped copy of the database without sensitive information?
Best wishes,
Hans

BobSullivan
3StarLounger
Posts: 235
Joined: 08 Jun 2010, 20:03
Location: Morgantown, PA

Re: Getting formatted text into Word

Post by BobSullivan »

Hans,

I tested it by itself as well, and it works. I am suspicious of the intermediate text box involved in the form. I've enclosed a small version of the database, and there's a read me file with some instructions in the database as a splash screen. I hope the instructions are clear enough. The form in question is called frmQuote.

Thanks for any help that can be provided...
You do not have the required permissions to view the files attached to this post.
Cordially,

Bob Sullivan
Elverson, PA

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

Re: Getting formatted text into Word

Post by HansV »

The sample database still contains the original code. If I replace it with the code that I suggested, it copies the formatted text to the Word document.

Here is a screenshot of the form in Access. I added some nonsensical formatting just for illustration purposes:
x860.png
And this is the result of clicking the 'Create Word Document' button:
x861.png
As you see, the formatting has been preserved.

Here is the relevant part of the code:

Code: Select all

    Set objword = New Word.Application
    objword.Documents.Add
    filePath = rst!QuotesBaseDir & Int(Me.QuoteIDPK / 1000) & "\"
    objword.ActiveDocument.SaveAs (filePath & Me.QuoteIDPK)
    ' **** start of modification ****
    With Me.QuoteDocument
        .SetFocus
        .SelStart = 0
        .SelLength = Len(.Text)
    End With
    RunCommand acCmdCopy
    Dim rng As Word.Range
    Set rng = objword.ActiveDocument.Content
    rng.Collapse Direction:=wdCollapseEnd
    rng.Paste
    ' **** end of modification ****
    objword.Visible = True
You do not have the required permissions to view the files attached to this post.
Best wishes,
Hans

BobSullivan
3StarLounger
Posts: 235
Joined: 08 Jun 2010, 20:03
Location: Morgantown, PA

Re: Getting formatted text into Word

Post by BobSullivan »

Hans,

Thanks very much for all that you do.

Your Code is working great. Now I just have to figure out how to bend some more Access rules in some other areas.
Cordially,

Bob Sullivan
Elverson, PA