Log in

View Full Version : Solved: Advisability of Using Styles to Search for Replacement Text



lwildernorva
06-15-2006, 08:24 AM
I have a question rather than a problem (I hope!). I use a combination of Word 2003 and Dragon NaturallySpeaking Legal. I have created a number of voice commands in Dragon that fire Word macros.

I have designed a Word macro that allows me to remove language that is in a new document, based on a template, if the language is not necessary in a particular case. In this template, two paragraphs in the document refer to an award of an attorney's fee. Those paragraphs are unnecessary if the person is unrepresented. At some point, it occurred to me that Word's Find and Replace functions can search for styles as well as particular words so I decided to create and apply a special style for these two paragraphs, called "Fee Language," and then, by a Dragon voice command, call the macro to search for and delete the text in any paragraphs in the document based on those styles. FYI, "Fee Language" has no formatting attributes other than those in Word's "Normal" style.

I realize, by the way, that I could have effectively reached the same result by omitting these two paragraphs from the template, inserting a bookmark, and then calling a macro that would search for the bookmark and either insert AutoText or insert a named file that would include the two paragraphs if necessary. At least at this point, I'll chalk this up to the enjoyment of finding more than one correct solution to a problem.

For reference, here's the code:
Sub DeleteFee()
Selection.Find.ClearFormatting
Selection.Find.Style = ActiveDocument.Styles("Fee Language")
Selection.Find.Replacement.ClearFormatting
Selection.Find.Execute Replace:=wdReplaceAll
End Sub The code works well. All I do is voice a command in Dragon to call the Word macro, and the language disappears immediately. I've used this method to create one macro and one template. It seems to work so well, in fact, that I am tempted to broaden its use.

I know, however, that sometimes a practice that looks good in limited circumstances becomes a nightmare when applied in more general circumstances. For instance, when I started writing code, I stored all my macros in Normal. Both through my own problems and from reading the advice in this forum, I understand that one or two macros stored in Normal may not mean much--but put hundreds in and you're asking for trouble. So I now store my macros in a special template created for that purpose and keep Normal clean.

I want to make sure I'm not about to fall into a similar trap by this use of styles. I'm interested in opinions about creating a number of styles solely for the purpose I've described here. Assuming I am not including all of these styles in Normal but simply storing the styles in the other templates I've created and that the nature of the documents I've created does not require more than one or two additional styles per template, is my method an efficient way to write code, or am I playing with fool's gold?

As I said, a question, but I'm willing to be told about any problems that I'm creating for myself.

Thanks!

fumei
06-15-2006, 08:42 AM
Bravo!!!!!!!

As those who know me here will not be surprised, I am wholeheartedly endorsing this.

The use of Styles for identification / action is a VERY good thing. I think it is a very efficient use of Word. The use of bookmarks can also be extremely efficient. Let's walk a little here.

Say you have four chunks with "Fee" language. Using Bookmarks, you could bookmark the chunks - but you will need four bookmarks. Say, Fee1, Fee2, Fee3, Fee4.Dim oBM As Word.Bookmark
For Each oBM In ActiveDocument.Bookmarks
If Left(oBM.Name, 3) = "Fee" Then
oBM.Range.Delete
End If
Nextwould remove all bookmarks with the first three letters of "Fee". The code would loop through all existing bookmarks in the document.


call the macro to search for and delete the text in any paragraphs in the document based on those styles.
Your code uses the Search function to...well search. It can be done differently.Sub RemoveFeeLanguage()
Dim oPara As Word.Paragraph
For Each oPara In ActiveDocument
If oPara.Style = "Fee Language" Then
oPara.Range.Delete
End If
Next
End SubYour code uses the Selection object. This means the result of your search cause that text to be selected. It is actually Selected (highlighted). This is not needed. The above code runs invisibly to the user - no movement of the Selection happens. The paragraphs with the style are simply deleted.

You are definitely on the right track though.

lwildernorva
06-15-2006, 11:32 AM
Thanks for the advice, including alternate techniques and some indepth background on the Selection object. Although I've written a fair amount of code, I sometimes simply borrow what has already worked rather than looking for the more efficient way. This was one of my forays into trying something more efficient. I was hoping I wasn't walking into a quagmire with this but sometimes, just because you can do a thing doesn't mean you should do a thing.


I see I still have a ways to go though.

lwildernorva
06-15-2006, 02:44 PM
One revision to your code. It initially did not work and returned error message 438: Object doesn't support this property or method. It took me awhile to locate the part of the code that needed revision, but I finally determined that the addition of a reference to the Paragraphs property of ActiveDocument would make the code work.

So:
Sub RemoveFee()
Dim oPara As Word.Paragraph
For Each oPara In ActiveDocument.Paragraphs
If oPara.Style = "Fee Language" Then
oPara.Range.Delete
End If
Next
End Sub
You were right. The code acts invisibly, and I assume, much more efficiently

Thanks again!

lwildernorva
06-15-2006, 03:20 PM
By the way, I'll mark this one as solved, although I welcome any other comments regarding the use of styles.

fumei
06-15-2006, 04:42 PM
Oooops!!!! My goodness...I can't believe I forgot that! :doh: yeah....an object would be sort of nice to work with....

lwildernorva
06-15-2006, 08:17 PM
I just figured you wanted me to do a little more work and learn a little bit more. No matter whether that was your plan or not, I learned some valuable lessons. Once I figured it out, it worked perfectly and gave me a more efficient code to use in the future.

Thanks.

fumei
06-16-2006, 08:07 PM
I am glad my - hmmmm - omission helped.

lwildernorva
06-17-2006, 07:35 AM
Well, it did make it a lot easier to do this:
Sub SaveOrder()
Dim ThisDoc As Document
Dim oPara As Word.Paragraph
Dim rngParagraph As Range
Set ThisDoc = ActiveDocument
For Each oPara In ThisDoc.Paragraphs

If oPara.Style = "VWC File No." Then
Set rngParagraph = oPara.Range
With rngParagraph
rngParagraph.End = rngParagraph.End - 1
rngParagraph.Start = rngParagraph.Start + 17
End With
End If
Next
ThisDoc.SaveAs FileName:="G:\LEW Work\" & rngParagraph & " Order"
ThisDoc.Close
Set ThisDoc = Nothing
Set rngParagraph = Nothing
End Sub
I had been trying, in my spare time, for several months to develop a macro that would allow me to save orders. Our documents have standard length file numbers (xxx-xx-xx) and are always saved to the same location, but there were other issues that made me think that using styles to assist in the identification of the specific text would help me out. Thinking about your code, with its use of ranges rather than selections, and its omission, got me the rest of the way home.

fumei
06-17-2006, 03:48 PM
Bravo again.