find formatted text

D

Dig-IT

Hi,

I need to do an automatic search/replace with a macro that i cannot record:

- i want to find all the occurrences where the space between a bold and
non-bold word is formatted as bold
- i want to change the formatting for that space as non-bold

e.g.:
<bold>someWord[space]</bold>someOtherword should become
<bold>someWord</bold>[space]someOtherword

<bold>someWord[space]someOtherword</bold> should remain intact

Any help would be highly appreciated!
Cheers,
Jos
 
G

Greg Maxey

This is pretty crude but might serve your purposes:

Sub Test()
Dim oWord As Word.Range
On Error Resume Next
For Each oWord In ActiveDocument.Range.Words
If Not oWord.Next.Font.Bold = True Then
oWord.Collapse Direction:=wdCollapseEnd
oWord.MoveStart Unit:=wdCharacter, Count:=-1
oWord.Font.Bold = False
End If
Next

End Sub
 
K

Klaus Linke

Hi Jos,

Another method I use a lot for stuff like this is to put in tags
temporarily.

In your case,
1)
Find what: ((leave empty, Format > Font: check "Bold", or use Ctrl+B))
Replace with: <b>^&</b> ((use Ctrl+B twice so it says "not bold"))
.... then "Replace all".
2)
Replace "<b> </b>" with a space. (("Clear formatting" if neccessary))
3)
With "Match wildcards" checked,
Find what: \<b\>(*)\</b\>
Replace with: \1 ((use Ctrl+B once so it says "bold"))

Looks a bit complicated, but if you've done replacements like this a few
times, it's a breeze.

Regards,
Klaus
 
D

Dig-IT

Hi Klaus,

Thanks a lot, this works perfectly _and_ i can understand it too (with
my limited VBA knowledge)

This method actually introduces new (formatted) spaces to the final
text, but they can be prevented by some intermediate search/replace
functions (see below).

FYI: I'm enhancing a Word2Wiki macro for automatic translation of a .doc
to the TWiki markup language:
http://twiki.org/cgi-bin/view/Plugins/MsWordToTWikiMLAddOn

Thanks again,
Jos

----------------reworked f&r sequence
- insert tags
find: (Bold empty)
repl: <b>^&</b> (not bold)

- remove all 'loose' formatted spaces
find: <b> </b>
repl: (Clear formatting)

-leading spaces
find: \<b\>( @)<
repl: <b>

-trailing spaces
find: (>)( @)(\</b\>)
repl: </b>

- add missing spaces
find: (>)\<b\>
repl: \1 <b>

find: \</b\>(<)
repl: </b> \1

- remove tags
find: \<b\>(*)\</b\>
repl: \1 (Bold)


Hi Jos,

Another method I use a lot for stuff like this is to put in tags
temporarily.

In your case,
1)
Find what: ((leave empty, Format > Font: check "Bold", or use Ctrl+B))
Replace with: <b>^&</b> ((use Ctrl+B twice so it says "not bold"))
... then "Replace all".
2)
Replace "<b> </b>" with a space. (("Clear formatting" if neccessary))
3)
With "Match wildcards" checked,
Find what: \<b\>(*)\</b\>
Replace with: \1 ((use Ctrl+B once so it says "bold"))

Looks a bit complicated, but if you've done replacements like this a few
times, it's a breeze.

Regards,
Klaus




Dig-IT said:
Hi,

I need to do an automatic search/replace with a macro that i cannot
record:

- i want to find all the occurrences where the space between a bold and
non-bold word is formatted as bold
- i want to change the formatting for that space as non-bold

e.g.:
<bold>someWord[space]</bold>someOtherword should become
<bold>someWord</bold>[space]someOtherword

<bold>someWord[space]someOtherword</bold> should remain intact

Any help would be highly appreciated!
Cheers,
Jos
 

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