How to format a string in a variable

G

Guillermo

Hello everyone,

I'm sorry if I'm asking something obvious, but I'm at lost with this.

In the line

..Text = b & "[[" & a & " (md)]]"

below I'd like to apply different formats to b, "[[" and " (md)]]" and b (i. e.
different font colors, etc).

Is it possible to format this text before adding it to the document (before the
replace command is executed)?

Thanks,

Guillermo


Here's a snippet of my code:

<code>

Sub MergeTerms(a As Variant, b As Variant)

Dim rango1 As Range

Set rango1 = Documents(TargetDoc).Range

With rango1.Find
.ClearFormatting
.Font.Hidden = False
.Text = a

With .Replacement
If FormatSp = "AllUpper" Then
'StrConv permite transformar una cadena

.Text = StrConv(b, vbUpperCase) & "[[" & a & " (gr)]]"
.Font.Underline = wdUnderlineDouble
.Font.UnderlineColor = wdColorBlue
ElseIf FormatSp = "AllBold" Then

.Text = b & "[[" & a & " (md)]]"
.Font.Bold = True
.Font.Underline = wdUnderlineDouble
.Font.UnderlineColor = wdColorBlue
Else
.Text = b & "[[" & a & "]]"
.Font.Underline = wdUnderlineDouble
.Font.UnderlineColor = wdColorBlue

End If

End With
.Execute Format:=True, Replace:=wdReplaceAll
End With


With Documents(TargetDoc).Range.Find
.ClearFormatting
.Font.Hidden = False
.Text = "\[\[" & "*" & "\]\]"
With .Replacement
.Text = "^&"
With .Font
.Hidden = True
.Color = wdColorBrown
.Underline = wdUnderlineNone
End With

End With
.Execute Format:=True, Replace:=wdReplaceAll, MatchWildcards:=True
End With


End Sub

Sub deleteunderline()
Dim rango1 As Range

Set rango1 = ActiveDocument.Range

With rango1.Find
.Format = True
'.MatchWildcards = True
'.text = "\[\[*\]\]"
.Font.Underline = wdUnderlineDouble
.Font.UnderlineColor = wdColorBlue

With .Replacement
.Text = "^&"
'.Font.Color = wdColorDarkRed
.Font.Underline = wdUnderlineNone
'.Font.Hidden = True
End With

.Execute Replace:=wdReplaceAll
End With

End Sub

<end of code>
 
J

Jay Freedman

Hi Guillermo,

No, it is not possible. You must insert text into a document and then
format it. You can insert the whole string in the document and then
use the Selection or a Range object to select each part in turn and
format it. Alternatively, you can insert each part of the string,
format that part, then insert the next part, format that, etc.

--
Regards,
Jay Freedman
Microsoft Word MVP
Email cannot be acknowledged; please post all follow-ups to the
newsgroup so all may benefit.
 
G

Guillermo

Then please correct me if I'm wrong:

If I wanted to do that, I'd had to look for every single instance of x one by one and
then insert and format my text for each hit instead of performing a global search and
replace, right?

Thank-you very much,

Guillermo

Jay said:
Hi Guillermo,

No, it is not possible. You must insert text into a document and then
format it. You can insert the whole string in the document and then
use the Selection or a Range object to select each part in turn and
format it. Alternatively, you can insert each part of the string,
format that part, then insert the next part, format that, etc.

--
Regards,
Jay Freedman
Microsoft Word MVP
Email cannot be acknowledged; please post all follow-ups to the
newsgroup so all may benefit.

Hello everyone,

I'm sorry if I'm asking something obvious, but I'm at lost with this.

In the line

.Text = b & "[[" & a & " (md)]]"

below I'd like to apply different formats to b, "[[" and " (md)]]" and b (i. e.
different font colors, etc).

Is it possible to format this text before adding it to the document (before the
replace command is executed)?

Thanks,

Guillermo
 
J

Jay Freedman

Yes, that's correct. A "Replace All" operation can't handle multiple
formats within the replacement text.

The searching one by one can be done by creating a loop like this:

Set myRange = ActiveDocument.Range
With myRange.Find
.Text = b & "[[" & a & " (md)]]"
.Wrap = wdFindStop
' other Find parameters
Do While .Execute

' insert and replace the various pieces here

myRange.Collapse Direction:=wdCollapseEnd
Loop
End With

On each iteration of the loop, the .Execute method returns True if the
..Text was found, and myRange is redefined to cover only the found
text. The inserting and formatting is best done by declaring a second
Range object and initializing it to myRange.Duplicate immediately
after the Do While statement.

--
Regards,
Jay Freedman
Microsoft Word MVP
Email cannot be acknowledged; please post all follow-ups to the
newsgroup so all may benefit.

Then please correct me if I'm wrong:

If I wanted to do that, I'd had to look for every single instance of x one by one and
then insert and format my text for each hit instead of performing a global search and
replace, right?

Thank-you very much,

Guillermo

Jay said:
Hi Guillermo,

No, it is not possible. You must insert text into a document and then
format it. You can insert the whole string in the document and then
use the Selection or a Range object to select each part in turn and
format it. Alternatively, you can insert each part of the string,
format that part, then insert the next part, format that, etc.

--
Regards,
Jay Freedman
Microsoft Word MVP
Email cannot be acknowledged; please post all follow-ups to the
newsgroup so all may benefit.

Hello everyone,

I'm sorry if I'm asking something obvious, but I'm at lost with this.

In the line

.Text = b & "[[" & a & " (md)]]"

below I'd like to apply different formats to b, "[[" and " (md)]]" and b (i. e.
different font colors, etc).

Is it possible to format this text before adding it to the document (before the
replace command is executed)?

Thanks,

Guillermo
 

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