Seaching a style using variable name doesn't work

  • Thread starter christophercbrewster via OfficeKB.com
  • Start date
C

christophercbrewster via OfficeKB.com

I wrote the following code, which seemed so obvious that I wasn't expecting
it to fail. But for some reason, the step that searches for a style fails.
The string value, such as "List Continue 2" matches a style name in the
template. If I use a literal string value (such as the one just shown), the
statement works. So the use of the string variable is causing a problem. One
more detail: the statement works correctly when the string variable is "List
Continue". So the problem is related to the use of the digit characters. Any
suggestions appreciated.

This routine is called from another routine...

Public sLevel As String
------------------------------------
Sub ReplaceContinues()
'
Dim sConLevel As String, sFindStyle As String
For nConLevel = 1 To 5
If Not nConLevel = 1 Then
sConLevel = " " + Str(nConLevel)
Else
sConLevel = ""
End If
Selection.Find.ClearFormatting
sFindStyle = "List Continue" + sConLevel
' This statement doesn't work:
Selection.Find.Style = ActiveDocument.Styles(sFindStyle)
Selection.Find.Replacement.ClearFormatting
Selection.Find.Replacement.Style = ActiveDocument.Styles("List
Continue" + sLevel)
With Selection.Find
.Text = ""
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindStop
.Format = True
End With
Selection.Find.Execute Replace:=wdReplaceAll
Next
End Sub

--
Christopher Brewster
Lockheed Martin, Eagan MN

Message posted via OfficeKB.com
http://www.officekb.com/Uwe/Forums.aspx/word-programming/200906/1
 
K

Klaus Linke

.... actually maybe one space too many: Str(x) will create a space before the
number, and you add another one.

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