Prev: User Form to Replace Master & SubDocument
Next: Microsoft Responds to the Evolution of Online Communities
From: jerem on 4 May 2010 23:21 Hi Jay, I meant to get back to you sooner with this, but the code below is what I ultimately ended up with which works to replace the styles and does indeed recognize aliases. Just one other simple question (simple for you, not me, of course). I need to be liberated from these darn quotes. . . I now just want VBA code to find a style and just select it (as it would if you did this manually with a find) and nothing more, but I can't use the With Selection.Find, .Style = "stylename" statement because it restricts me to hardcoding the style name in the quotes. I want to be able to get the "stylename" from an Input Box, similar to the code below. I've tried tweaking the code below, eliminating the For Each - Next code (because I don't want to do this finding and replacing business) but I do like the the idea that the style name Change is liberated from the quotes in the statement If myPara.Style = Change, but I have tried different things with no success (and have gotten messages like Method does not exist for this Object, Collection -- or something or other). Every search I've done on Google involves either a With Selection.Find and those pesky quoted stylenames or a Range method with those pesky quoted stylenames. Ugh! Anyway -- if you are in the liberating mood, can you tell me how to find a style that is gotten from an Input Box? Thanks for your help. Sub ReplaceOneStyleWithAnother() 'Works with Aliases Dim myPara As Paragraph Dim oRng As Range Dim Change As Style Dim csty As String Dim Replace As Style Dim rsty As String csty = InputBox("What Style Do You Want To Change?") rsty = InputBox("What Style Do You Want To Replace it With?") On Error GoTo Done Set Change = ActiveDocument.Styles(csty) Set Replace = ActiveDocument.Styles(rsty) For Each myPara In ActiveDocument.Paragraphs If myPara.Style = Change Then myPara.Style = Replace End If Next Done: End Sub "Jay Freedman" wrote: > Try replacing your If statement with this: > > If (objStyle.NameLocal = strStyleName) Or _ > (InStr(objStyle.NameLocal, "," & strStyleName) > 0) Or _ > (InStr(objStyle.NameLocal, strStyleName & ",") = 1) Then > > This says that strStyleName will be found if it's equal to the whole > ..NameLocal, or if strStyleName with a comma before it or after it is found > anywhere within .NameLocal. The last two parts handle an alias or the "real" > name of a style that has an alias, respectively. > > Depending on how you assign style aliases in your documents and templates, > this might not be quite accurate. For example, it would tell you that a > style with an alias "p" exists even though every alias that begins with "p" > also has a number after it. Be careful how you apply the code. > > -- > Regards, > Jay Freedman > Microsoft Word MVP FAQ: http://word.mvps.org > Email cannot be acknowledged; please post all follow-ups to the newsgroup so > all may benefit. > > jerem wrote: > > Jay, > > > > You're right - it is demonstrably not true! It's also demonstrably > > true that I have to be more careful with my terminology - should have > > said it "seems" like it is not hitting the Available List (just for > > future reference: don't trust a single thing I say - I'm operating > > with very limited knowledge!). Never used that Immediate window > > before which is quite helpful. Turns out once watching this window > > that it was indeed going through the list. Problem was it wasn't > > finding it because I am using aliases. If I put in the entire name - > > voila! It works perfectly, which leads to the next question (one > > question always "seems" to spawn another) how can I get this to > > accept aliases? > > > > "Jay Freedman" wrote: > > > >> I think your "realization" is demonstrably not true. Try an > >> experiment: add the line > >> > >> Debug.Print objStyle.NameLocal > >> > >> immediately after the For Each objStyle statement. Open the Immediate > >> window (Ctrl+G) and start the macro. The list of style names being > >> compared to strStyleName will appear in the Immediate window, and it > >> will probably include all of the built-in styles (which cannot be > >> deleted). If that is not the case, then the document you're working > >> with is highly unusual, if not completely broken. > >> > >> -- > >> Regards, > >> Jay Freedman > >> Microsoft Word MVP FAQ: http://word.mvps.org > >> Email cannot be acknowledged; please post all follow-ups to the > >> newsgroup so all may benefit. > >> > >> On Tue, 16 Mar 2010 18:18:01 -0700, jerem > >> <jerem(a)discussions.microsoft.com> wrote: > >> > >>> I'm trying to use the code below to replace some styles and have > >>> the user dictate which style to find and replace, however, before I > >>> resolve the user input part I'm having a problem with the Public > >>> Function code at the " If objStyle.NameLocal = strStyleName Then > >>> StyleExists = True". This statement never rings true despite the > >>> fact that there is a p2 style amongst the Styles and Formatting, > >>> Available Formatting List and p2 is contained in the strStyleName. > >>> At first this was perplexing as to why p2 was not being found in > >>> the Style list and then I realized that it was looking to the wrong > >>> list (or should I say not the list I want it to look to). It is > >>> looking in the Bullets and Numbering List Styles which only > >>> contains 3 styles in there (as opposed to many styles in the > >>> "Available Formatting List" which p2 is amongst) which are > >>> 1.1/1.1.1/1.1.1.1 and then two other levels and, of course, because > >>> it doesn't find p2 it makes no replacements. > >>> > >>> I have tried to look up a replacement for .NameLocal (one that is > >>> equalivalent to look to the "Available Formatting List" not the > >>> Bullets and Numbering style list) with no luck. Any one know how > >>> to point the search in the direction I need? > >>> > >>> As always, thanks for your help. > >>> > >>> > >>> Sub ReplaceStyles() > >>> 'FindStyleName = InputBox("What style do you want to find?") > >>> 'ReplaceStyleName = InputBox("What style do you want to replace it > >>> with?") > >>> > >>> Dim objRange As Range > >>> Set objRange = ActiveDocument.Range > >>> With objRange.find > >>> .ClearFormatting > >>> .Replacement.ClearFormatting > >>> If StyleExists("p2") = True Then > >>> .Style = "p2" > >>> .Replacement.Style = "p4" > >>> .Execute Replace:=wdReplaceAll > >>> End If > >>> 'The following code is if you want to do more finds and replaces > >>> ' If StyleExists("Name2") = True Then > >>> ' .Style = "Name2" > >>> ' .Replacement.Style = "NewName2" > >>> ' .Replacement.ParagraphFormat.SpaceBefore = 0 > >>> ' .Execute Replace:=wdReplaceAll > >>> ' End If > >>> '' Etc., etc. > >>> End With > >>> Set objRange = Nothing > >>> > >>> End Sub > >>> > >>> Public Function StyleExists(strStyleName As String) As Boolean > >>> > >>> ' Determines whether or not the target style exists in the > >>> ' active document. > >>> > >>> Dim objStyle As Style > >>> StyleExists = False > >>> For Each objStyle In ActiveDocument.Styles > >>> If objStyle.NameLocal = strStyleName Then > >>> StyleExists = True > >>> Exit For > >>> End If > >>> Next objStyle > >>> > >>> End Function > >> . > > > . > |