# Macro Help: Losing formatting with replace() (MSWord)



## OffTopic (Sep 19, 2014)

I'm having a little trouble retaining formatting when replacing some text in a Word text box.


When I use this line I lose all the individual character formatting for the text range. 


Box.TextFrame.TextRange = Replace(Box.TextFrame.TextRange.Text, "replace this text, "with this text")


Box.TextFrame.TextRange is a text box that has been formatted character by character. Picking up attributes like bold, italic, size, font, etc. from a text box inside CorelDraw. After all this is done certain words within the textbox have to be replaced. Is there another way to do this?


Thanks for any help. (I know this isn't an Excel question but the problem would be similar if I were going from Excel to Word instead of CorelDraw to Word.)


----------



## RoryA (Sep 19, 2014)

Which version of Office?


----------



## OffTopic (Sep 19, 2014)

Hi Rory,
Both Office 2003 and 2013.


----------



## Macropod (Sep 19, 2014)

It really doesn't matter which Office version. The issue is that when you replace formatted content with a plain text string, the formatting is always lost. If you want to retain the character formatting, you'll have to replace the characters one at a time. Of course, that's only going to work properly if both strings are the same length. If they are the same length, you could use code like:

```
Sub Demo()
Application.ScreenUpdating = False
Dim i As Long, j As Long
Dim StrFnd As String, StrRep As String
StrFnd = "replace this text": StrRep = "with this text"
j = Len(StrFnd)
If Len(StrFnd) > Len(StrRep) Then j = Len(StrRep)
With ActiveDocument.Shapes(1).TextFrame.TextRange
  With .Find
    .ClearFormatting
    .Replacement.ClearFormatting
    .Format = False
    .Text = StrFnd
    .Replacement.Text = StrRep
    .Execute
  End With
  If .Find.Found Then
    With .Duplicate
      For i = 1 To j
        .Characters(i) = Mid(StrRep, i, 1)
      Next
    End With
  End If
End With
Application.ScreenUpdating = True
End Sub
```
To cater for different-length strings you could add code like the following after the 'Next':

```
If Len(StrFnd) > Len(StrRep) Then
        .Start = .Start + Len(StrRep)
        .Delete
      End If
      If Len(StrRep) > Len(StrFnd) Then
        .Start = .Start + Len(StrFnd)
        .InsertAfter Right(StrRep, Len(StrRep) - Len(StrFnd))
      End If
```


----------



## Macropod (Sep 20, 2014)

Cross-posted at: Macro Help: Losing formatting with replace() | Windows Secrets Lounge
For cross-posting etiquette, please read: Excelguru Help Site - A message to forum cross posters


----------



## OffTopic (Sep 20, 2014)

Macropod,
Cross-posting, noted, thank you.

As for your response, thank you again. I probably wasn't specific enough. I'm only replacing a short phrase with a single word. The short phrase will always be formatted uniformly and so the single word will only have one set of formatting attributes. I was able to get by with the following:


```
With Box.TextFrame.TextRange.Find
           .Text = "Replace this text"
           .Replacement.Text = "With this text"
           .Wrap = wdFindContinue
           .Execute Replace:=wdReplaceAll
          End With
```

Your example was incredibly enlightening. (In addition to .Find, Mid() and ScreenUpdating were new to me)

Do you (Macropod or anyone else) know how to set the MatchFuzzy options? Would this property compensate for slight misspellings in either the .Text or the .Replacement.Text strings? I appreciate any and all help.


----------



## Macropod (Sep 20, 2014)

AFAIK Word's MatchFuzzy Find/Replace settings are only for Japanese text. Is that what you're working with?


----------



## OffTopic (Sep 20, 2014)

No, I'm not working with Japanese text, just my own tendency to be inaccurate. Are there any VBA string functions that will compensate for a misplaced space or letter?


----------



## Macropod (Sep 20, 2014)

Not really. Although Word's Find/Replace can use wildcards, doing so presupposes that you know at least both which characters might be variable (perhaps even including their case). And, of course, you'd need to avoid the possibility of false matches. Even then, there's no way for it to 'correct' an errant replacement string.


----------



## OffTopic (Sep 22, 2014)

Thanks a lot for the help. The wildcards will suffice.


----------

