Adding formatting to a text string in code?

M

ML

I have a large portion of text that I need to build up in VBA based on
selections made by the user on a custom form. This is basically a long
string of text strung together and then set to a custom field which is then
displayed on the page it is embedded on.

Is there any way to add formatting such as italics and bold to specific
words in this string as I build it up? I assume not as a string is just
plain text but thought I would ask in case someone has a suggestion.
 
D

Dave Lett

Hi ML,

No, you can apply formatting, as you mentioned, because it is just plain
text. However, you CAN place any arbitrary delimiter before and after this
plain text. For example, you could have a string like the following:

you could have a <b>string<\b> like the <i>following<\i>

Once this plain text is in Word, you could then do a wildcard find/replace
to format your plain text, as in the following example:

With Selection.Find
.ClearFormatting
.Text = "(\<b\>)(*)(\<\\b\>)"
.MatchWildcards = True
With .Replacement
.ClearFormatting
.Text = "\2"
.Font.Bold = True
End With
.Execute Replace:=wdReplaceAll

.Text = "(\<i\>)(*)(\<\\i\>)"
With .Replacement
.ClearFormatting
.Font.Italic = True
End With
.Execute Replace:=wdReplaceAll
End With

HTH,
Dave
 
M

ML

Great thanks! The tags with wildcard search and replace should do the trick
for my needs.

For the replacement, how do you specify it to remove the tags but leave the
wildcard portion text?
i.e. find <i>my text</i> and replace it with just "my text" in italics where
in "my text" could be any text value.
 
J

Jean-Guy Marcil

ML was telling us:
ML nous racontait que :
Great thanks! The tags with wildcard search and replace should do
the trick for my needs.

For the replacement, how do you specify it to remove the tags but
leave the wildcard portion text?
i.e. find <i>my text</i> and replace it with just "my text" in
italics where in "my text" could be any text value.

The code works as is.

In the first part:

With Selection.Find
.ClearFormatting
.Text = "(\<b\>)(*)(\<\\b\>)"

'means find a <b> tag followed by any text (*)
'and followed by a <\b> tag.
'The() create groups and the \ character tells the
'Find engine to find the character following
'the "\" as is, not as a special character.

.MatchWildcards = True

'use wild cards so that we can create groups and
'use * for any characters

With .Replacement
.ClearFormatting
.Text = "\2"

'replace the found text by whatever the second group is
'(i.e, delete groups 1 and 3, or the tags)

.Font.Bold = True

'and set to bold

End With
.Execute Replace:=wdReplaceAll

'do it

.Text = "(\<i\>)(*)(\<\\i\>)"

'change the "Find" string

With .Replacement
.ClearFormatting

'since the "Replace by" string is not mentioned,
' it will use the one from before ("\2")

.Font.Italic = True

'but set to italic, not bold

End With
.Execute Replace:=wdReplaceAll

'do it

End With

--
Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
(e-mail address removed)
Word MVP site: http://www.word.mvps.org
 
T

Tony Jollans

For bold and italics, rather than using arbitrary tags youi could use *bold
text* and _italic text_ and then just Autoformat the Range afterwards.
 
P

Peter Jamieson

If you are allowed to create a file on disk, you can try using HTML
formatting tags, save the string to a .htm file, then insert the file into
the document, but be careful with
a. format conversion dialogs
b. stuff like trailing paragraph marks.

Peter Jamieson
 
M

ML

Is there a way to suppress the message displayed that the replace has
reached the end of the document and a way to ensure it does the replace from
top to bottom regardless of start position?
 
J

Jonathan West

ML said:
Is there a way to suppress the message displayed that the replace has
reached the end of the document and a way to ensure it does the replace
from top to bottom regardless of start position?

Yes, set the Wrap property of the Find object to wdFindStop.


--
Regards
Jonathan West - Word MVP
www.intelligentdocuments.co.uk
Please reply to the newsgroup
Keep your VBA code safe, sign the ClassicVB petition www.classicvb.org
 
M

ML

Thanks, does that cause it to stop at the first occurrence? I need it to
basically do one full pass of the document for all occurrences.
 
J

Jonathan West

ML said:
Thanks, does that cause it to stop at the first occurrence?

No, not if you are using wdReplaceAll. Setting Wrap to wdFindStop will
search just the selection (if you have a solid selection block) or from the
cursor to the end of the document (if you have a flashing cursor)

--
Regards
Jonathan West - Word MVP
www.intelligentdocuments.co.uk
Please reply to the newsgroup
Keep your VBA code safe, sign the ClassicVB petition www.classicvb.org
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top