PDA

View Full Version : Changing Built-in Styles Names with VB



kamrad
07-01-2013, 12:52 AM
Hello fellows,

I am using Word 2010. I would like to change the names of the built-in styles in a document that I would then like to turn into a template. I found a script on the Web that is claimed to do that (sorry, not allowed post the link to this web page).

Sub ChangeStyleNames()
' The macro appends a * to all style names
' It thus changes built-in styles to ordinary styles
Dim myRange As Range
Dim MsgText
Dim myFileName

MsgText = "Cancel if you have not saved the file"
If MsgBox(MsgText, vbExclamation + vbOKCancel, "Danger") = vbCancel Then
End
End If

myFileName = ActiveDocument.Name
If InStr(1, myFileName, ".") > 0 Then
myFileName = Left$(myFileName, InStr(1, myFileName, ".")) & "RTF"
Else
myFileName = myFileName & ".RTF"
End If
ActiveDocument.SaveAs _
FileName:=myFileName, _
FileFormat:=wdFormatRTF
ActiveDocument.Close
Documents.Open _
FileName:=myFileName, _
ConfirmConversions:=False, _
Format:=wdOpenFormatText
Set myRange = ActiveDocument.Content
myRange.Find.Execute _
FindText:="{stylesheet*}}", _
MatchWildcards:=True
myRange.Find.Execute _
FindText:=";}", _
ReplaceWith:="*^&", _
MatchWildcards:=True, _
Replace:=wdReplaceAll
ActiveDocument.Save
ActiveDocument.Close
Documents.Open _
FileName:=myFileName
End Sub

According to the VB debugger, the faulty code part is the following:

myRange.Find.Execute _
FindText:="{stylesheet*}}", _
MatchWildcards:=True

Unfortunately, I am not familiar with VB. :dunno Could you please tell me what the problem is? The error message that I get says: "the Find What text contains a Pattern Match expression which is not valid".

Thank you for your help in advance! :hi:

Doug Robbins
07-01-2013, 02:32 AM
I believe that if you replace the following lines of code:

Set myRange = ActiveDocument.Content
myRange.Find.Execute _
FindText:="{stylesheet*}}", _
MatchWildcards:=True
myRange.Find.Execute _
FindText:=";}", _
ReplaceWith:="*^&", _
MatchWildcards:=True, _
Replace:=wdReplaceAll

with


Selection.Find.Execute_
FindText:="stylesheet",_
MatchWildcards:=False
Set myRange =Selection.Range
myRange.End =ActiveDocument.Range.End
myRange.End =myRange.Start + InStr(myRange, "heading 9;}") + 10
myRange.Text= Replace(myRange.Text, ";}", "*;}")

it will do the same thing as the part of the code that is failing.

kamrad
07-01-2013, 08:17 AM
Hi Doug,

First of all, thank you for your response!
I replaced the piece of code with the one you suggested but apparently anothor bug sneaked in. Now when I am run the macro, I get the following error:

"Error. Method or Data Member Not Found." The debugger marks the following lines in red:
FindText:="stylesheet",_
MatchWildcards:=False

Please, advise. Thank you in advance!:friends:


I believe that if you replace the following lines of code:

Set myRange = ActiveDocument.Content
myRange.Find.Execute _
FindText:="{stylesheet*}}", _
MatchWildcards:=True
myRange.Find.Execute _
FindText:=";}", _
ReplaceWith:="*^&", _
MatchWildcards:=True, _
Replace:=wdReplaceAll

with


Selection.Find.Execute_
FindText:="stylesheet",_
MatchWildcards:=False
Set myRange =Selection.Range
myRange.End =ActiveDocument.Range.End
myRange.End =myRange.Start + InStr(myRange, "heading 9;}") + 10
myRange.Text= Replace(myRange.Text, ";}", "*;}")

it will do the same thing as the part of the code that is failing.