find and replace styles

W

Willie

Greetings

I have a lot documents that have an old set of styles. recently, we
change all the stylrs to different names. I now have to apply these new
styles to the existing docs.

I would like to open a doc and run a macro to search the text for the
old style and replace with the new style. I have been writing macros
for a very short time, but have been developing software for many
years.

I have the old docs with the current new styles and I have been testing
but with little success!

Any advice on implementation, code snippets would be helpful

Regards
Willie
 
G

Greg

Willie,

Perhaps something along this line:

Sub ScratchMacro()
Dim oPar As Paragraph
For Each oPar In ActiveDocument.Range.Paragraphs
Select Case oPar.Style
Case Is = "Old Style1"
oPar.Style = "New Style2"
Case Is = "Old Style2"
oPar.Style = "New Style2"
Case Else
'Do Nothing
End Select
Next oPar
End Sub

Repeat the Case statement for each Old/New style pair.
 
J

Jezebel

If you have a large number of styles to deal with, it might be easier to
work with a collection, rather than a long Select Case sequence --

Dim pStyleList as collection
Dim pPar as Word.Paragraph
Dim pNewStyle as string

set pStyleList = New Collection
pStyleList.Add Item:="NewStyle 1", Key:="OldStyle 1"
pStyleList.Add Item:="NewStyle 2", Key:="OldStyle 2"
:
pStyleList.Add Item:="NewStyle n", Key:="OldStyle n"

on error resume next
For each pPar in ActiveDocument.Paragraphs
pPar.Style = pStyleList(pPar.Style) 'Ignore error if
style is not in the collection
Next
on error goto 0
 
G

Greg

Jezebel,

Thanks for showing this method. I see that it could be adapted to a
find and replace quite easily:

Sub ScratchMacro2()
Dim pFindWord As Collection
Dim pReplaceWord As Collection
Dim myRange As Range
Dim i As Long

Set pFindWord = New Collection
Set pReplaceWord = New Collection

pFindWord.Add "find word 1"
pReplaceWord.Add "replace word 1"
pFindWord.Add "find word 2"
pReplaceWord.Add "replace word 2"

Set myRange = ActiveDocument.Range
For i = 1 To pFindWord.Count
With myRange.Find
.Text = pFindWord(i)
.Replacement.Text = pReplaceWord(i)
.Execute Replace:=wdReplaceAll
End With
Next
End Sub

I am still not familiar enough with Classes to grasp whether or not a
Class should or could also be used. I am thinking along the lines of
having a class named Wordlist and then members (if that is the right
term) find words and replace words.

Please advise and if feeling generous show ;-)
 
J

Jezebel

The main value of a collection is the ability to look things up -- that was
the point of my example: you look up the old style to get the new style. In
your example, the collection is really just an unbounded array. Which is OK,
but I'm not sure that it helps very much. And the bug-catcher in me *really*
dislikes the reliance on the two collections remaining in synch. If youare
going to use a collection in this sort of way, then use just one, and make
each item in the collection store both words.

You could use a UDT as the collection item, or as you suggest, a class
(which I much prefer to UDTs) --

-- Class object: clsFindClass --
Public FindWord as string
Public ReplaceWord as string

-- Main code --

Dim pWordList as collection
Dim pFindClass as clsFindClass

set pWordList = new collection

set pFindClass = new clsFindClass
pFindClass.FindWord = "Find word 1"
pFindClass.ReplaceWord = "Replace word 1"
pWordList.Add pFindClass

set pFindClass = new clsFindClass
pFindClass.FindWord = "Find word 2"
pFindClass.ReplaceWord = "Replace word 2"
pWordList.Add pFindClass

:

For i = 1 To pFindWord.Count
With myRange.Find
.Text = pWordList(i).FindWord
.Replacement.Text = pWordList(i).ReplaceWord
.Execute Replace:=wdReplaceAll
End With
Next


In this particular case, I think an array would be better than a collection.
 
G

Greg Maxey

Jezebel,

Looks like I have some homework. UDT???

I'll give your code a try and see how I make out. Thanks.
 
J

Jezebel

UDT = User-defined type --

Public Type FindWords_Type
FindWord as string
ReplaceWord as string
End Type

:

Dim pMyFind as FindWords_Type
pMyFind.FindWord = "AAAA"

:


However, if you try the above and try to put pMyFind into a collection, you
get the glorious error message "Only user-defined types in public object
modules can be coerced to or from a variant or passed to late-bound
functions" ...
 
G

Greg

Jezebel,

OK, I was able to get this to work I assume that you meant:

For i = 1 To pWordList.Count

as I had to make that change to get it to work.

Yes, this seems a bit laborious.

I will study your other suggestions further. Right now I use a table
stored outside of work and create an array using it. Seems to work OK.
 
W

Willie

Greg,

Thanks for the quick reply, your suggested implementation worked fine,
I choose not to use the list due to lack of time and it was throw away
code.

Regards
Willie
 

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