find and replace... but only certain ones

L

littleme

Have a question... Am new to vba in word. Am trying to create a
template for writing meeting minutes. All issues in the minutes have a
unique number, where the first number indicates whether its the first,
second, third meeting. Reading backwards, 1.1.2 is the indicates the
second point of discussion under the first header, at the first
meeting. Would like a userform to appear each time the document is
opened, asking what number the meeting has. Using the number that the
user fills in, would like to find and replace all the first number in
document. Have searched and found code for that I think. My problem is
how to tell Word, for the above example, to only replace the first 1
and not the second 1.

Any ideas anyone?

Grateful for help.
 
G

Greg

Just a stab and not tested, but why not do a wildcard search for:
1.([0-9].)([0-9])

for find all groupings starting with 1

and replace with

2.\1\2
 
L

littleme

I guess a wildcard search is what. Thank you. However, how do i
integrate it with a userform so that whatever digit the user fills in,
the macro will search for (digit-1) and replace with the digit?
 
G

Greg

"I guess a wildcard search is what"

Is that a statement or a question? The wildcard search above is
looking for a "1" a "." then a group "group number 1" of a range of
characters "[ ]" that range being "0" to "9" and a period and finally a
second group "group number 2" of a similiar range.

The replace is a "2" a "." then group 1 and group 2 that was found.

For your macro with a user form the following should do:

Note: .textbox1 and .textbox2 are the userform textboxes in your
userform. Do not Unload the form in the Userform command button code.

Sub ScratchMacro()
Dim myFrm As UserForm1
Dim oRng As Word.Range
Dim aTxt As String
Dim bTxt As String
Set myFrm = New UserForm1
myFrm.Show
aTxt = myFrm.TextBox1.Text
bTxt = myFrm.TextBox2.Text
Set oRng = ActiveDocument.Range
With oRng.Find
.ClearFormatting
.Replacement.ClearFormatting
.MatchWildcards = True
.Forward = True
.Text = aTxt & ".([0-9]{1,}.)([0-9]{1,})"
.Replacement.Text = bTxt & ".\1\2"
.Execute Replace:=wdReplaceAll
End With
Set oRng = Nothing
Unload myFrm
Set myFrm = Nothing
End Sub
 
L

littleme

Hope Christmas was goo =)

And sorry about the mispunctuation. Thoughts go faster than my fingers.

Tried using the macro above but cant get it to work. When the the
userform is filled in and OK is pressed, the form just empties itself
without replacing anything. Am using an Auto_Open to get the form to
pop up. Maybe I have a mistake there? Code there is as follows:

Private Sub Document_Open()

Dim myform As Info2
Set myform = New Info2
myform.Show
End Sub

Am confused when to set to nothing, and unload... what i oRng if its ok
to ask?=S
Also, if Im reading the code correctly, the user has to fill in what to
look for, and what to replace it with, no? Is it possible to search for
aTXT-1?

Anyway, grateful for any insight.
 
L

littleme

Hope Im counting hours correctly here..

Thank your answer. Changed to Me.Hide like you said and things make a
bit more sense now; however, macro still gets caught on the .execute
replace line so changed code to this:

Sub ScratchMacro()
Dim myFrm As UserForm1
Dim aTxt As String
Dim bTxt As String
Set myFrm = New UserForm1
myFrm.Show
aTxt = myFrm.TextBox1.Text
bTxt = myFrm.TextBox2.Text
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = aTxt & ".([0-9].)([0-9])"
.Replacement.Text = bTxt & ".\1\2"
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = True
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute Replace:=wdReplaceAll
Set oRng = Nothing
Unload myFrm
Set myFrm = Nothing
End Sub

It works but might it give me probelms later? Since already have
Userform popping up with Auto_new, where do i put this macro. Tried
changing it to Ok_click but that didnt work. Have a couple of bookmarks
that need to be updated using the same userform. Have code for that but
though could trigger both using OK-click.... Im really new to all of
this. Sorry. Would be really happy if you could help with this last
bit...
 
L

littleme

Hope Im counting hours correctly here.. My work day starts in the
middle of the night for you so wasnt really sure where to send my
question. Have this posted on the net as well, so whichever you prefer.


I really appreciate that you took the time to answer last time. Thank
you. you email address cracked me. Dont see that one too often. Reminds
me of when lived in japan and visited yakuska/ Kinnick. anyway...

Changed to Me.Hide like you said and things make a bit more sense now;
however, macro still gets caught on the .execute replace line so
changed code to this:

Sub ScratchMacro()
Dim myFrm As UserForm1
Dim aTxt As String
Dim bTxt As String
Set myFrm = New UserForm1
myFrm.Show
aTxt = myFrm.TextBox1.Text
bTxt = myFrm.TextBox2.Text
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = aTxt & ".([0-9].)([0-9])"
.Replacement.Text = bTxt & ".\1\2"
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = True
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute Replace:=wdReplaceAll
Set oRng = Nothing
Unload myFrm
Set myFrm = Nothing
End Sub

It works but might it give me probelms later? Since already have
Userform popping up with Auto_new, where do i put this macro. Tried
changing it to Ok_click but that didnt work. Have a couple of bookmarks
that need to be updated using the same userform. Have code for that but
though could trigger both using OK-click.... Im really new to all of
this. Sorry.

Best wishes for the new year
Nina
 
G

Greg

I don't know what caused your problem with the .Execute line. It works
fine on this end.

If you are going to eliminate the oRng variable then you can eliminate
the line:
Set oRng = Nothing.

<Where do I put this macro?

I suppose that you could call it from your Auto_New macro (I thought
this project was for editing existing minutes??).

Sub Auto_New()
Dim yourOtherForm as UserFormWhatever
Set yourOtherForm as New UserFormWhatever
yourOtherForm.Show
Unload yourOtherForm
Set yourOtherForm = Nothing
ScratchMacro 'Call the macro
'Anything else you want here
End Sub
 

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