formatting text in a string while building a word document

WorkingVBA

Registered User.
Local time
Today, 02:34
Joined
Jul 3, 2013
Messages
33
Hi,

I am in the process of building a MS Access application that constructs a word document based on predetermined choices. It has been a while so I feel like I am stumbling a bit in the dark. Depending on particular user choices the application builds a document consisting of predetermined paragraphs of text. Right now I am using the Word.application object and the corresponding commands .typetext and .typeparagraph. The problem is that I have no idea how to change the formatting of a word or a few words within the string other than breaking the paragraph up into smaller segments.

For example in order to do something like this:
Code:
    With apWord.Selection
        .TypeText "Lorem Ipsum is simply <b>dummy text</b> of the printing and typesetting industry."
        .TypeParagraph
   End With

(Embedding HTML tags does not work) I have to do:
Code:
    With apWord.Selection
        .TypeText "Lorem Ipsum is simply"
        .Font.Bold = True
        .TypeText " dummy text"
        .Font.Bold = False
        .TypeText " of the printing and typesetting industry."
        .TypeParagraph
   End With

You can see where this would be a problem when having a lot of formatting changes. Also, I am grateful for suggestions that make this easier since I am :banghead: after all. I briefly looked into using the StringBuilder command but could not find any suggestions for embedding formatting tags. Thanks in advance for your help. It feels good to be doing a little coding again, even if it is late at night after work (I think I may need a different hobby)
 
I've never had need to do anything more than a few boldings or italicization. I don't know if you can avoid the toggling on/off of bold etc but you could short cut it with a call to a sub

Code:
sub TextToBold(WdSel as word.selection, TextToBold as string)
   with WdSel
        .font.bold = true
        .TypeText TextToBold
        .Font.Bold = False
   end with
end sub



Evoke like this
Code:
       .TypeText "Lorem Ipsum is simply"
        call TextToBold(apWord.selection, " dummy text")

HTH
 
Just a thought, but if you use Memo (aka Long Text) fields you can specify Rich Text format. Then when you enter the text it works just like Word - i.e. fonts can be set from the menu as you type.

I would imagine you having a Paragraphs table and each record would be a pre-set paragraph which you could pick to assemble your document for export to Word.

I haven't done this with Word but I have used Rich Text memo fields for export as pre-set Outlook Email text and it works there.
 
Use range not selection

Code:
With New Word.Application
    .Visible = True
    With .Documents.Add.Range
        .Text = "Lorem Ipsum is simply dummy text of the printing and typesetting industry."
        .Find.Execute "dUmMy tExt", False
        .Font.Bold = True
    End With
End With
 

Users who are viewing this thread

Back
Top Bottom