Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
DiscussionsAccessExcelInfoPathOutlookPowerPointPublisherWord
DirectoryUser Groups
Related Topics
Outlook ExpressInternet ExplorerWindowsMS Server ProductsMore Topics ...

MS Office Forum / Word / Programming / March 2006

Tip: Looking for answers? Try searching our database.

How to format a string in a variable

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Guillermo - 01 Mar 2006 21:43 GMT
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>
Jay Freedman - 02 Mar 2006 03:47 GMT
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        FAQ: http://word.mvps.org
Email cannot be acknowledged; please post all follow-ups to the
newsgroup so all may benefit.

>Hello everyone,
>
[quoted text clipped - 13 lines]
>
>Guillermo
Guillermo - 02 Mar 2006 22:25 GMT
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

> Hi Guillermo,
>
[quoted text clipped - 28 lines]
>>
>> Guillermo
Jay Freedman - 03 Mar 2006 00:17 GMT
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        FAQ: http://word.mvps.org
Email cannot be acknowledged; please post all follow-ups to the
newsgroup so all may benefit.

>Then please correct me if I'm wrong:
>
[quoted text clipped - 38 lines]
>>>
>>> Guillermo
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.