PDA

View Full Version : User Form: Refer to Code Used Earlier in Form?



Arrow123
06-07-2012, 04:25 PM
I have made a user form that has several check boxes. When a particular check box is true, a series of searches and replaces is performed on the text of a document; then on the footnotes; then on the endnotes. Here is a sample.

If chkshortinclusnos = True Then
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = "( )([1-9])([0-9])(^=)([0-9])(,)"
.Replacement.Text = "\1\2\3\4\2\5\6"
.Forward = True
.Wrap = wdFindContinue
.MatchWildcards = True
End With
Selection.Find.Execute Replace:=wdReplaceAll
End If
If ActiveDocument.Footnotes.Count > 0 Then
ActiveWindow.View.SeekView = wdSeekFootnotes
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = "( )([1-9])([0-9])(^=)([0-9])(,)"
.Replacement.Text = "\1\2\3\4\2\5\6"
.Forward = True
.Wrap = wdFindContinue
.MatchWildcards = True
End With
Selection.Find.Execute Replace:=wdReplaceAll


and so on.

After switching to footnote view (or endnote view), is there any way to repeat the series of searches and replaces that was done in the text, without repeating the code itself? I am a complete novice at Visual Basic and don't have any idea how to proceed. I tried repeating

chkshortinclusnos = True

but it didn't work.

Thanks for any help. And sorry if this is something so simple I shouldn't even be asking.

Tinbendr
06-07-2012, 04:32 PM
Welcome to the board.

How about this? (http://word.mvps.org/FAQs/Customization/ReplaceAnywhere.htm)

Arrow123
06-07-2012, 06:04 PM
Thanks!

The code in step 2 might work fine for what I have in mind. My documents will be quite simple and will never have headers or footers.

What I had been hoping to do was to do something similar to calling a macro within another macro, by giving a name to the (huge, even though I showed only one in my example) group of searches and replaces that follow
If chkshortinclusnos = True Then


and then calling it rather than repeating the code. Is there any way at all to do this? Or is the best bet to implement something similar to the code in step 2?

Thanks.

Tinbendr
06-07-2012, 06:50 PM
Look at the very bottom Sub at the link. That's why I posted the link. They talk about story ranges and recursive code.

You'll have to pass the at least the range. Good time to get away from Selection. (Although, you technically can pass a Selection.Range)