Ureader.com  
Microsoft software help and Community
   home   |   control panel login   |   archive   |  
 
Word
application.errors
conversions
docmanagement
drawing.graphics
formatting.longdocs
international
internet.assistant
mail
mailmerge.fields
menustoolbars
newusers
numbering
oleinterop
pagelayout
printingfonts
setup.networking
spelling.grammar
tables
vba.addins
vba.beginners
vba.customization
vba.general
vba.userforms
web.authoring
word6-7macros
word97vba
  
 
date: Fri, 11 Jul 2008 09:37:01 -0700,    group: microsoft.public.word.vba.beginners        back       


Styles Macro   
I need the ability to take an exsisting Style within a Doc. and have a macro 
to change the name of a specific style.

So for instance, I need Bold Header1 to be renamed Bold Header2

I have been looking at the replace feature in VB, but it does not seem to be 
working.  Here is the code I created to do that.


Selection.Find.ClearFormatting
    Selection.Find.Replacement.ClearFormatting
    With Selection.Find
        .Text = ""
        .Replacement.Text = ""
        .Forward = True
        .Wrap = wdFindContinue
        .Format = False
        .MatchCase = False
        .MatchWholeWord = False
        .MatchWildcards = False
        .MatchSoundsLike = False
        .MatchAllWordForms = False
    End With
    
  With Selection.Find
        .ClearFormatting
        .Replacement.ClearFormatting
        .Style = ActiveDocument.Styles("Body Text_ACSSC")
        .Text = ""
        .Replacement.Style = ActiveDocument.Styles("Body Text_ACS")
        .Execute Replace:=wdReplaceAll
date: Fri, 11 Jul 2008 09:37:01 -0700   author:   Krumrei

Re: Styles Macro   
"So for instance, I need Bold Header1 to be renamed Bold Header2"

I am not following.  When you say rename, do mean the name of the style?  Or,
are you trying to change the style used, from one to another?  As in:

Find all instances of Body Text_ACSSC style , and change that text to Body
Text_ACS style

This is easy, but BOTH styles must exist.

You can rename a style with something like:

ActiveDocument.Styles("MyNewOne").NameLocal = "Blah"

This would change "MyNewOne" to "Blah"

Krumrei wrote:
>I need the ability to take an exsisting Style within a Doc. and have a macro 
>to change the name of a specific style.
>
>So for instance, I need Bold Header1 to be renamed Bold Header2
>
>I have been looking at the replace feature in VB, but it does not seem to be 
>working.  Here is the code I created to do that.
>
>Selection.Find.ClearFormatting
>    Selection.Find.Replacement.ClearFormatting
>    With Selection.Find
>        .Text = ""
>        .Replacement.Text = ""
>        .Forward = True
>        .Wrap = wdFindContinue
>        .Format = False
>        .MatchCase = False
>        .MatchWholeWord = False
>        .MatchWildcards = False
>        .MatchSoundsLike = False
>        .MatchAllWordForms = False
>    End With
>    
>  With Selection.Find
>        .ClearFormatting
>        .Replacement.ClearFormatting
>        .Style = ActiveDocument.Styles("Body Text_ACSSC")
>        .Text = ""
>        .Replacement.Style = ActiveDocument.Styles("Body Text_ACS")
>        .Execute Replace:=wdReplaceAll

-- 
Message posted via http://www.officekb.com
date: Fri, 11 Jul 2008 17:14:24 GMT   author:   fumei via OfficeKB.com u37563@uwe

Re: Styles Macro   
This works ( ActiveDocument.Styles("MyNewOne").NameLocal = "Blah")

BUT, then how would I include Multiple stylechanges within that same string?

So if I have a doc that has to have 10 different names changes, I want to be 
able to have them all completed in one shot?\


Thanks for the info!






"fumei via OfficeKB.com" wrote:

> "So for instance, I need Bold Header1 to be renamed Bold Header2"
> 
> I am not following.  When you say rename, do mean the name of the style?  Or,
> are you trying to change the style used, from one to another?  As in:
> 
> Find all instances of Body Text_ACSSC style , and change that text to Body
> Text_ACS style
> 
> This is easy, but BOTH styles must exist.
> 
> You can rename a style with something like:
> 
> ActiveDocument.Styles("MyNewOne").NameLocal = "Blah"
> 
> This would change "MyNewOne" to "Blah"
> 
> Krumrei wrote:
> >I need the ability to take an exsisting Style within a Doc. and have a macro 
> >to change the name of a specific style.
> >
> >So for instance, I need Bold Header1 to be renamed Bold Header2
> >
> >I have been looking at the replace feature in VB, but it does not seem to be 
> >working.  Here is the code I created to do that.
> >
> >Selection.Find.ClearFormatting
> >    Selection.Find.Replacement.ClearFormatting
> >    With Selection.Find
> >        .Text = ""
> >        .Replacement.Text = ""
> >        .Forward = True
> >        .Wrap = wdFindContinue
> >        .Format = False
> >        .MatchCase = False
> >        .MatchWholeWord = False
> >        .MatchWildcards = False
> >        .MatchSoundsLike = False
> >        .MatchAllWordForms = False
> >    End With
> >    
> >  With Selection.Find
> >        .ClearFormatting
> >        .Replacement.ClearFormatting
> >        .Style = ActiveDocument.Styles("Body Text_ACSSC")
> >        .Text = ""
> >        .Replacement.Style = ActiveDocument.Styles("Body Text_ACS")
> >        .Execute Replace:=wdReplaceAll
> 
> -- 
> Message posted via http://www.officekb.com
> 
>
date: Fri, 11 Jul 2008 10:35:01 -0700   author:   Krumrei

Re: Styles Macro   
"Krumrei" wrote:

> This works ( ActiveDocument.Styles("MyNewOne").NameLocal = "Blah")
> 
> BUT, then how would I include Multiple stylechanges within that same string?
> 
> So if I have a doc that has to have 10 different names changes, I want to be 
> able to have them all completed in one shot?\
> 

You could try something like this:


Dim varOldArray As Variant
Dim varNewArray As Variant
Dim i As Long

Const strOldNames As String = "Name1|Name2|Name3|Name4|Name5|Name6"
Const strNewNames As String = 
"NewName1|NewName2|NewName3|NewName4|NewName5|NewName6"

varOldArray = Split(strOldNames, "|")
varNewArray = Split(strNewNames, "|")

For i = 0 To UBound(varOldArray)
    ActiveDocument.Styles(varOldArray(i)).NameLocal = varNewArray(i)
Next


But it is not really a "One" shot as you have to run the loop x number of 
times.

A true "one" shot is not possible in this case.
It might be easier just to have 10 
    ActiveDocument.Styles("MyNewOne").NameLocal = "Blah"
consecutive statements.
date: Fri, 11 Jul 2008 12:57:00 -0700   author:   Jean-Guy Marcil

Re: Styles Macro   
Ok,

One step back.....


What I need it to do it change one style to another through the entire 
document ( I will have both styles preloaded ) and it has to go through the 
header/footer and the body of the document and including table changes in the 
styles.

Any suggestions?


Thanks!




"Jean-Guy Marcil" wrote:

> "Krumrei" wrote:
> 
> > This works ( ActiveDocument.Styles("MyNewOne").NameLocal = "Blah")
> > 
> > BUT, then how would I include Multiple stylechanges within that same string?
> > 
> > So if I have a doc that has to have 10 different names changes, I want to be 
> > able to have them all completed in one shot?\
> > 
> 
> You could try something like this:
> 
> 
> Dim varOldArray As Variant
> Dim varNewArray As Variant
> Dim i As Long
> 
> Const strOldNames As String = "Name1|Name2|Name3|Name4|Name5|Name6"
> Const strNewNames As String = 
> "NewName1|NewName2|NewName3|NewName4|NewName5|NewName6"
> 
> varOldArray = Split(strOldNames, "|")
> varNewArray = Split(strNewNames, "|")
> 
> For i = 0 To UBound(varOldArray)
>     ActiveDocument.Styles(varOldArray(i)).NameLocal = varNewArray(i)
> Next
> 
> 
> But it is not really a "One" shot as you have to run the loop x number of 
> times.
> 
> A true "one" shot is not possible in this case.
> It might be easier just to have 10 
>     ActiveDocument.Styles("MyNewOne").NameLocal = "Blah"
> consecutive statements.
date: Tue, 15 Jul 2008 07:44:01 -0700   author:   Krumrei

Re: Styles Macro   
"Krumrei" wrote:

> Ok,
> 
> One step back.....
> 
> 
> What I need it to do it change one style to another through the entire 
> document ( I will have both styles preloaded ) and it has to go through the 
> header/footer and the body of the document and including table changes in the 
> styles.
> 

No need of VBA, why don't you simply use the Find/Replace feature in the 
Edit menu?
date: Thu, 17 Jul 2008 09:12:01 -0700   author:   Jean-Guy Marcil

Re: Styles Macro   
Are you changing the name of a style, or applying a different existing style 
with a similar name to some text?

It sounds more like the latter.

It's tricky to get at the paragraphs in the header and footer.  Those 
paragraphs are not part of the ActiveDocument.Paras collection.   You have to 
look at each storyRange in the document.

Good luck.  I'm working on something not too dissimilar at the moment 
(nothing that would actually be relevant to you).

-  Jessica
date: Thu, 17 Jul 2008 10:06:01 -0700   author:   Jessica Weissman

Re: Styles Macro   
Because we have to do this to over 5,000 documents and it would waste time on 
our end, I think I may have figured it out. But still would like more idea's

"Jean-Guy Marcil" wrote:

> "Krumrei" wrote:
> 
> > Ok,
> > 
> > One step back.....
> > 
> > 
> > What I need it to do it change one style to another through the entire 
> > document ( I will have both styles preloaded ) and it has to go through the 
> > header/footer and the body of the document and including table changes in the 
> > styles.
> > 
> 
> No need of VBA, why don't you simply use the Find/Replace feature in the 
> Edit menu?
date: Fri, 18 Jul 2008 05:19:01 -0700   author:   Krumrei

Re: Styles Macro   
"Krumrei" wrote:

> Because we have to do this to over 5,000 documents and it would waste time on 
> our end, I think I may have figured it out. But still would like more idea's
> 

See:
   http://word.mvps.org/faqs/macrosvba/BatchFR.htm

and instead of replacing text, replace the styles.

You may need to combine the batch code with the information here:
   http://word.mvps.org/faqs/macrosvba/FindReplaceAllWithVBA.htm
date: Fri, 18 Jul 2008 05:47:03 -0700   author:   Jean-Guy Marcil

Re: Styles Macro   
Thanks for the references.

One last question:

I have created this code, over and over repeating it for each Stlye in Word 
to have the name replaced. I have over 20 lines of code, one for each Style 
to be renamed.

However, how do I get it to continue the Macro, if the Name is not within 
the style list?
 If it is not in there, it just stops the Macro at the point of the unknown 
or not listed Style within the document.

Basically, I want the program to skip and go to the next Sub, IF it has not 
changed or recognized that the  style listed in the document.

Paul


ActiveDocument.Styles("Body Text_ACSSC").NameLocal = "Body Text_ACS"








"Jean-Guy Marcil" wrote:

> "Krumrei" wrote:
> 
> > Because we have to do this to over 5,000 documents and it would waste time on 
> > our end, I think I may have figured it out. But still would like more idea's
> > 
> 
> See:
>    http://word.mvps.org/faqs/macrosvba/BatchFR.htm
> 
> and instead of replacing text, replace the styles.
> 
> You may need to combine the batch code with the information here:
>    http://word.mvps.org/faqs/macrosvba/FindReplaceAllWithVBA.htm
date: Fri, 18 Jul 2008 08:32:00 -0700   author:   Krumrei

Re: Styles Macro   
"Krumrei" wrote:

> Thanks for the references.
> 
> One last question:
> 
> I have created this code, over and over repeating it for each Stlye in Word 
> to have the name replaced. I have over 20 lines of code, one for each Style 
> to be renamed.
> 
> However, how do I get it to continue the Macro, if the Name is not within 
> the style list?
>  If it is not in there, it just stops the Macro at the point of the unknown 
> or not listed Style within the document.
> 
> Basically, I want the program to skip and go to the next Sub, IF it has not 
> changed or recognized that the  style listed in the document.
> 

First, check which of the target custom styles exist, then use that to do 
the work.


Dim i As Long
Dim styCheck As Style
Dim strStyles As String
Dim varStyles As Variant

For i = 1 To ActiveDocument.Styles.Count
    Set styCheck = ActiveDocument.Styles(i)
    Select Case styCheck.NameLocal
    Case "Body Text_ACSSC", "Body Text_AC", _
        "Title_ACSSC", "Title_AC"
        If strStyles = "" Then
            strStyles = styCheck.NameLocal
        Else
            strStyles = strStyles & "|" & styCheck.NameLocal
        End If
    End Select
Next

If strStyles <> "" Then
    varStyles = Split(strStyles, "|")
    For i = LBound(varStyles) To UBound(varStyles)
        'Do something to each of these styles...
    Next
Else
    MsgBox "None of the target custom styles were found in this document."
End If
date: Fri, 18 Jul 2008 09:57:01 -0700   author:   Jean-Guy Marcil

Google
 
Web ureader.com


    COPYRIGHT 2007, YARDI TECHNOLOGY LIMITED, ALL RIGHT RESERVE  |   contact us