Error in simple macro

S

Sammy

Hi,

I am creating a macro from pieces of posts. I can't get this part to work:

Dim oRng As Range
Set oRng = ActiveDocument.Content

With oRng.Find
.ClearFormatting
.Text = "[boldon]^&[boldoff]"
.Forward = True
.Wrap = wdFindStop
.MatchWildcards = True
Do While .Execute
With oRng
.MoveEnd Unit:=wdCharacter, Count:=-1
.MoveStart Unit:=wdCharacter, Count:=1
.Font.Bold = True
.Collapse wdCollapseEnd
End With
Loop
End With

It's supposted to bold all characters between [boldon] and [boldoff]. I get
an error on the DO WHILE .EXECUTE line. I can't figure out why it doesn't
work. The following is the complete macro which adds the [boldon] [boldoff]
codes to bolded text, basically does a paste special unformatted to a new
clean doc and then replaces the bold and gets rid of the [boldon/off] codes.
Thanks for any suggestions or help.


Sub NewCleanUp()
'
'
If Documents.Count = 0 Then Exit Sub

'mark font formatting

Selection.HomeKey Unit:=wdStory
Selection.Find.ClearFormatting
Selection.Find.Font.Bold = True

Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = ""
.Replacement.Text = "[boldon]^&[boldoff]"

.Forward = True
.Wrap = wdFindContinue
.Format = True
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute Replace:=wdReplaceAll

'end mark font formatting

'start pastespecial text

Selection.EndKey Unit:=wdStory
Selection.Find.ClearFormatting
With Selection.Find
.Text = "^$"
.Replacement.Text = ""
.Forward = False
.Wrap = wdFindAsk
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute
Selection.MoveRight Unit:=wdCharacter, Count:=2
Selection.HomeKey Unit:=wdStory, Extend:=wdExtend
Selection.Cut
ActiveWindow.Close wdDoNotSaveChanges
Documents.Add DocumentType:=wdNewBlankDocument
Selection.PasteSpecial Link:=False, DataType:=wdPasteText, Placement:= _
wdInLine, DisplayAsIcon:=False
Selection.HomeKey Unit:=wdStory

'end original macro

'add back bold

Dim oRng As Range
Set oRng = ActiveDocument.Content

With oRng.Find
.ClearFormatting
.Text = "[boldon]^&[boldoff]"
.Forward = True
.Wrap = wdFindStop
.MatchWildcards = True
Do While .Execute
With oRng
.MoveEnd Unit:=wdCharacter, Count:=-1
.MoveStart Unit:=wdCharacter, Count:=1
.Font.Bold = True
.Collapse wdCollapseEnd
End With
Loop
End With


'get rid of <boldon/off> codes

Selection.HomeKey Unit:=wdStory
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = "[boldon]"
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute Replace:=wdReplaceAll

Selection.HomeKey Unit:=wdStory
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = "[boldoff]"
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute Replace:=wdReplaceAll
End Sub
 
J

Jay Freedman

Hi Sammy,

The code ^& is not valid in a .Text expression.

In a .Replacement.Text expression ^& means "the text that was matched by the
search", but it has no meaning in a search expression.

In the wildcard find/replace you're doing, the proper .Text value is

.Text = "[boldon]*[boldoff]"

See http://www.word.mvps.org/FAQs/General/UsingWildcards.htm for a table of
search and replace codes.

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

I am creating a macro from pieces of posts. I can't get this part to
work:

Dim oRng As Range
Set oRng = ActiveDocument.Content

With oRng.Find
.ClearFormatting
.Text = "[boldon]^&[boldoff]"
.Forward = True
.Wrap = wdFindStop
.MatchWildcards = True
Do While .Execute
With oRng
.MoveEnd Unit:=wdCharacter, Count:=-1
.MoveStart Unit:=wdCharacter, Count:=1
.Font.Bold = True
.Collapse wdCollapseEnd
End With
Loop
End With

It's supposted to bold all characters between [boldon] and [boldoff].
I get an error on the DO WHILE .EXECUTE line. I can't figure out why
it doesn't work. The following is the complete macro which adds the
[boldon] [boldoff] codes to bolded text, basically does a paste
special unformatted to a new clean doc and then replaces the bold and
gets rid of the [boldon/off] codes. Thanks for any suggestions or
help.


Sub NewCleanUp()
'
'
If Documents.Count = 0 Then Exit Sub

'mark font formatting

Selection.HomeKey Unit:=wdStory
Selection.Find.ClearFormatting
Selection.Find.Font.Bold = True

Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = ""
.Replacement.Text = "[boldon]^&[boldoff]"

.Forward = True
.Wrap = wdFindContinue
.Format = True
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute Replace:=wdReplaceAll

'end mark font formatting

'start pastespecial text

Selection.EndKey Unit:=wdStory
Selection.Find.ClearFormatting
With Selection.Find
.Text = "^$"
.Replacement.Text = ""
.Forward = False
.Wrap = wdFindAsk
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute
Selection.MoveRight Unit:=wdCharacter, Count:=2
Selection.HomeKey Unit:=wdStory, Extend:=wdExtend
Selection.Cut
ActiveWindow.Close wdDoNotSaveChanges
Documents.Add DocumentType:=wdNewBlankDocument
Selection.PasteSpecial Link:=False, DataType:=wdPasteText,
Placement:= _ wdInLine, DisplayAsIcon:=False
Selection.HomeKey Unit:=wdStory

'end original macro

'add back bold

Dim oRng As Range
Set oRng = ActiveDocument.Content

With oRng.Find
.ClearFormatting
.Text = "[boldon]^&[boldoff]"
.Forward = True
.Wrap = wdFindStop
.MatchWildcards = True
Do While .Execute
With oRng
.MoveEnd Unit:=wdCharacter, Count:=-1
.MoveStart Unit:=wdCharacter, Count:=1
.Font.Bold = True
.Collapse wdCollapseEnd
End With
Loop
End With


'get rid of <boldon/off> codes

Selection.HomeKey Unit:=wdStory
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = "[boldon]"
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute Replace:=wdReplaceAll

Selection.HomeKey Unit:=wdStory
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = "[boldoff]"
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute Replace:=wdReplaceAll
End Sub
 
S

Sammy

Thanks Jay, I replaced that code and gave it a try. It doesn't work, now
it's bolding lots of the text, not just the text in between the [boldon] and
[boldoff]. Here's the macro with your fix:

Dim oRng As Range
Set oRng = ActiveDocument.Content

With oRng.Find
.ClearFormatting
.Text = "[boldon]*[boldoff]"
.Forward = True
.Wrap = wdFindStop
.MatchWildcards = True
Do While .Execute
With oRng
.MoveEnd Unit:=wdCharacter, Count:=-1
.MoveStart Unit:=wdCharacter, Count:=1
.Font.Bold = True
.Collapse wdCollapseEnd
End With
Loop
End With

I don't get an error however. All I want it to do is bold the text between
the [boldon] and [boldoff] tags in my document. Thanks again for your help!

Jay Freedman said:
Hi Sammy,

The code ^& is not valid in a .Text expression.

In a .Replacement.Text expression ^& means "the text that was matched by the
search", but it has no meaning in a search expression.

In the wildcard find/replace you're doing, the proper .Text value is

.Text = "[boldon]*[boldoff]"

See http://www.word.mvps.org/FAQs/General/UsingWildcards.htm for a table of
search and replace codes.

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

I am creating a macro from pieces of posts. I can't get this part to
work:

Dim oRng As Range
Set oRng = ActiveDocument.Content

With oRng.Find
.ClearFormatting
.Text = "[boldon]^&[boldoff]"
.Forward = True
.Wrap = wdFindStop
.MatchWildcards = True
Do While .Execute
With oRng
.MoveEnd Unit:=wdCharacter, Count:=-1
.MoveStart Unit:=wdCharacter, Count:=1
.Font.Bold = True
.Collapse wdCollapseEnd
End With
Loop
End With

It's supposted to bold all characters between [boldon] and [boldoff].
I get an error on the DO WHILE .EXECUTE line. I can't figure out why
it doesn't work. The following is the complete macro which adds the
[boldon] [boldoff] codes to bolded text, basically does a paste
special unformatted to a new clean doc and then replaces the bold and
gets rid of the [boldon/off] codes. Thanks for any suggestions or
help.


Sub NewCleanUp()
'
'
If Documents.Count = 0 Then Exit Sub

'mark font formatting

Selection.HomeKey Unit:=wdStory
Selection.Find.ClearFormatting
Selection.Find.Font.Bold = True

Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = ""
.Replacement.Text = "[boldon]^&[boldoff]"

.Forward = True
.Wrap = wdFindContinue
.Format = True
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute Replace:=wdReplaceAll

'end mark font formatting

'start pastespecial text

Selection.EndKey Unit:=wdStory
Selection.Find.ClearFormatting
With Selection.Find
.Text = "^$"
.Replacement.Text = ""
.Forward = False
.Wrap = wdFindAsk
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute
Selection.MoveRight Unit:=wdCharacter, Count:=2
Selection.HomeKey Unit:=wdStory, Extend:=wdExtend
Selection.Cut
ActiveWindow.Close wdDoNotSaveChanges
Documents.Add DocumentType:=wdNewBlankDocument
Selection.PasteSpecial Link:=False, DataType:=wdPasteText,
Placement:= _ wdInLine, DisplayAsIcon:=False
Selection.HomeKey Unit:=wdStory

'end original macro

'add back bold

Dim oRng As Range
Set oRng = ActiveDocument.Content

With oRng.Find
.ClearFormatting
.Text = "[boldon]^&[boldoff]"
.Forward = True
.Wrap = wdFindStop
.MatchWildcards = True
Do While .Execute
With oRng
.MoveEnd Unit:=wdCharacter, Count:=-1
.MoveStart Unit:=wdCharacter, Count:=1
.Font.Bold = True
.Collapse wdCollapseEnd
End With
Loop
End With


'get rid of <boldon/off> codes

Selection.HomeKey Unit:=wdStory
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = "[boldon]"
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute Replace:=wdReplaceAll

Selection.HomeKey Unit:=wdStory
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = "[boldoff]"
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute Replace:=wdReplaceAll
End Sub
 
K

Klaus Linke

Hi Sammy,

The [square brackets] have special meaning in wildcard searches.
If you want to search for them, you have to "escape" them with a backslash:
.Text = "\[boldon\]*\[boldoff\]"

If you want to get rid of the tags while doing the replacement, you could
put the part you want to keep in round brackets:


Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting

Selection.Find.Replacement.Font.Bold = True
With Selection.Find
.Text = "\[boldon\](*)\[boldoff\]"
.Replacement.Text = "\1"
.Forward = True
.Wrap = wdFindContinue
.Format = True
.MatchWildcards = True
End With
Selection.Find.Execute Replace:=wdReplaceAll

Regards,
Klaus
 

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