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.

RE: Adding formatting to a text string in code? by DaveLett

DaveLett
Thu Jan 19 15:30:03 CST 2006

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

"ML" wrote:

> 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.
>
>
>

Re: Adding formatting to a text string in code? by ML

ML
Thu Jan 19 15:47:29 CST 2006

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.

"Dave Lett" <DaveLett@discussions.microsoft.com> wrote in message
news:2FE5BA90-8943-4590-840C-728D8EC87E78@microsoft.com...
> 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
>
> "ML" wrote:
>
>> 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.
>>
>>
>>



Re: Adding formatting to a text string in code? by Jean-Guy

Jean-Guy
Thu Jan 19 17:07:27 CST 2006

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
jmarcilREMOVE@CAPSsympatico.caTHISTOO
Word MVP site: http://www.word.mvps.org



Re: Adding formatting to a text string in code? by Tony

Tony
Thu Jan 19 19:08:15 CST 2006

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

--
Enjoy,
Tony


"Dave Lett" <DaveLett@discussions.microsoft.com> wrote in message
news:2FE5BA90-8943-4590-840C-728D8EC87E78@microsoft.com...
> 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
>
> "ML" wrote:
>
> > 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.
> >
> >
> >



Re: Adding formatting to a text string in code? by Peter

Peter
Fri Jan 20 03:55:05 CST 2006

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

"ML" <schooner@accesswave.ca> wrote in message
news:O8TkNRTHGHA.1676@TK2MSFTNGP09.phx.gbl...
>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.
>



Re: Adding formatting to a text string in code? by ML

ML
Fri Jan 20 10:00:25 CST 2006

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?

"Jean-Guy Marcil" <NoSpam@LeaveMeAlone> wrote in message
news:Orvmi0UHGHA.1288@TK2MSFTNGP09.phx.gbl...
> 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
> jmarcilREMOVE@CAPSsympatico.caTHISTOO
> Word MVP site: http://www.word.mvps.org
>



Re: Adding formatting to a text string in code? by Jonathan

Jonathan
Fri Jan 20 10:02:15 CST 2006


"ML" <schooner@accesswave.ca> wrote in message
news:eBNPmqdHGHA.1760@TK2MSFTNGP10.phx.gbl...
> 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


Re: Adding formatting to a text string in code? by ML

ML
Fri Jan 20 10:29:07 CST 2006

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.

"Jonathan West" <jwest@mvps.org> wrote in message
news:udTkg2dHGHA.2896@TK2MSFTNGP09.phx.gbl...
>
> "ML" <schooner@accesswave.ca> wrote in message
> news:eBNPmqdHGHA.1760@TK2MSFTNGP10.phx.gbl...
>> 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



Re: Adding formatting to a text string in code? by Jonathan

Jonathan
Fri Jan 20 11:17:56 CST 2006


"ML" <schooner@accesswave.ca> wrote in message
news:OMRmo6dHGHA.1452@TK2MSFTNGP11.phx.gbl...
> 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