(Msg. 1) Posted: Tue Jul 08, 2008 1:51 am
Post subject: Userform for Macro "Find & Replace in all Documents" Add to elertz Archived from groups: microsoft>public>word>vba>userforms (more info?)
Hi,
I have the following macro code for finding & replacing text in all open
documents (Thanks Mr. fumei for the helpful code).
Sub FRinallDoc()
Dim aDoc As Document
Dim r As Range
For Each aDoc In Application.Documents
Set r = aDoc.Range
With r.Find
.Text = "yadda"
.Replacement.Text = "whatever"
.Execute Replace:=wdReplaceAll
End With
Next
End Sub
It is very cumbersome to change criterias in macro everytime I need to find
& replace text in open documents. I have no idea how to write a code for
userform of above macro.
I want to have two text boxes with labels to put text for find & replace and
three command buttons of Clear (for clearing text boxes), OK (for executing
the macro), & Cancel.
(Msg. 2) Posted: Tue Jul 08, 2008 10:42 am
Post subject: Re: Userform for Macro "Find & Replace in all Documents" Add to elertz [Login to view extended thread Info.] Archived from groups: per prev. post (more info?)
"shabutt" <shabutt RemoveThis @discussions.microsoft.com> wrote in message
news:DB79EFFF-6220-45AF-B22C-90C6D89FC2A1@microsoft.com...
> Hi,
>
> I want to have two text boxes with labels to put text for find & replace
> and
> three command buttons of Clear (for clearing text boxes), OK (for
> executing
> the macro), & Cancel.
>
> Please help me out.
Hi Shabutt
This article should get you started. If you have trouble adapting the ideas
in this to your specific requirements, then post back here
(Msg. 3) Posted: Tue Jul 08, 2008 10:42 am
Post subject: Re: Userform for Macro "Find & Replace in all Documents" Add to elertz [Login to view extended thread Info.] Archived from groups: per prev. post (more info?)
Thanks Mr. Jonathan,
I have looked into
http://www.word.mvps.org/FAQs/Userforms/CreateAUserForm.htm but that's
notmuch help. I have designed the userform but further than that I am blank.
I googled but could not find for the time being what I need for my particular
probelm.
"Jonathan West" wrote:
>
> "shabutt" <shabutt.TakeThisOut@discussions.microsoft.com> wrote in message
> news:DB79EFFF-6220-45AF-B22C-90C6D89FC2A1@microsoft.com...
> > Hi,
>
> >
> > I want to have two text boxes with labels to put text for find & replace
> > and
> > three command buttons of Clear (for clearing text boxes), OK (for
> > executing
> > the macro), & Cancel.
> >
> > Please help me out.
>
> Hi Shabutt
>
> This article should get you started. If you have trouble adapting the ideas
> in this to your specific requirements, then post back here
>
>
> How to create a Userform
> http://www.word.mvps.org/FAQs/Userforms/CreateAUserForm.htm >
>
> --
> Regards
> Jonathan West - Word MVP
> www.intelligentdocuments.co.uk > Please reply to the newsgroup
>
>
>
(Msg. 4) Posted: Tue Jul 08, 2008 8:15 pm
Post subject: Re: Userform for Macro "Find & Replace in all Documents" Add to elertz [Login to view extended thread Info.] Archived from groups: per prev. post (more info?)
On the user form, insert two text boxes, one of which you name txtFind and
the other you name txtReplacement
Then have a command button on the form that contains the following code:
Dim aDoc As Document
Dim r As Range
If Len(txtFind.Text) > 0 then
For Each aDoc In Application.Documents
Set r = aDoc.Range
With r.Find
.Text = txtFind.Text
.Replacement.Text = txtReplacement.Text
.Execute Replace:=wdReplaceAll
End With
Next
End If
Me.Hide
--
Hope this helps.
Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.
Doug Robbins - Word MVP
"shabutt" <shabutt.DeleteThis@discussions.microsoft.com> wrote in message
news:5665AF35-098D-4F87-84DE-2BC0C4586420@microsoft.com...
> Thanks Mr. Jonathan,
>
> I have looked into
> http://www.word.mvps.org/FAQs/Userforms/CreateAUserForm.htm but that's
> notmuch help. I have designed the userform but further than that I am
> blank.
> I googled but could not find for the time being what I need for my
> particular
> probelm.
>
> "Jonathan West" wrote:
>
>>
>> "shabutt" <shabutt.DeleteThis@discussions.microsoft.com> wrote in message
>> news:DB79EFFF-6220-45AF-B22C-90C6D89FC2A1@microsoft.com...
>> > Hi,
>>
>> >
>> > I want to have two text boxes with labels to put text for find &
>> > replace
>> > and
>> > three command buttons of Clear (for clearing text boxes), OK (for
>> > executing
>> > the macro), & Cancel.
>> >
>> > Please help me out.
>>
>> Hi Shabutt
>>
>> This article should get you started. If you have trouble adapting the
>> ideas
>> in this to your specific requirements, then post back here
>>
>>
>> How to create a Userform
>> http://www.word.mvps.org/FAQs/Userforms/CreateAUserForm.htm >>
>>
>> --
>> Regards
>> Jonathan West - Word MVP
>> www.intelligentdocuments.co.uk >> Please reply to the newsgroup
>>
>>
>>
(Msg. 5) Posted: Tue Jul 08, 2008 8:15 pm
Post subject: Re: Userform for Macro "Find & Replace in all Documents" Add to elertz [Login to view extended thread Info.] Archived from groups: per prev. post (more info?)
Dear Mr. Robbins,
I did what you instructed me to do but was unable to run the userform. I
have few more questions. Do I need to write any code in the module and how to
attach it to userform. Sorry, I am still clueless.
"Doug Robbins - Word MVP" wrote:
> On the user form, insert two text boxes, one of which you name txtFind and
> the other you name txtReplacement
>
> Then have a command button on the form that contains the following code:
>
> Dim aDoc As Document
> Dim r As Range
> If Len(txtFind.Text) > 0 then
> For Each aDoc In Application.Documents
> Set r = aDoc.Range
> With r.Find
> .Text = txtFind.Text
> .Replacement.Text = txtReplacement.Text
> .Execute Replace:=wdReplaceAll
> End With
> Next
> End If
> Me.Hide
>
>
> --
> Hope this helps.
>
> Please reply to the newsgroup unless you wish to avail yourself of my
> services on a paid consulting basis.
>
> Doug Robbins - Word MVP
>
> "shabutt" <shabutt DeleteThis @discussions.microsoft.com> wrote in message
> news:5665AF35-098D-4F87-84DE-2BC0C4586420@microsoft.com...
> > Thanks Mr. Jonathan,
> >
> > I have looked into
> > http://www.word.mvps.org/FAQs/Userforms/CreateAUserForm.htm but that's
> > notmuch help. I have designed the userform but further than that I am
> > blank.
> > I googled but could not find for the time being what I need for my
> > particular
> > probelm.
> >
> > "Jonathan West" wrote:
> >
> >>
> >> "shabutt" <shabutt DeleteThis @discussions.microsoft.com> wrote in message
> >> news:DB79EFFF-6220-45AF-B22C-90C6D89FC2A1@microsoft.com...
> >> > Hi,
> >>
> >> >
> >> > I want to have two text boxes with labels to put text for find &
> >> > replace
> >> > and
> >> > three command buttons of Clear (for clearing text boxes), OK (for
> >> > executing
> >> > the macro), & Cancel.
> >> >
> >> > Please help me out.
> >>
> >> Hi Shabutt
> >>
> >> This article should get you started. If you have trouble adapting the
> >> ideas
> >> in this to your specific requirements, then post back here
> >>
> >>
> >> How to create a Userform
> >> http://www.word.mvps.org/FAQs/Userforms/CreateAUserForm.htm > >>
> >>
> >> --
> >> Regards
> >> Jonathan West - Word MVP
> >> www.intelligentdocuments.co.uk > >> Please reply to the newsgroup
> >>
> >>
> >>
>
>
>
(Msg. 6) Posted: Tue Jul 08, 2008 8:15 pm
Post subject: Re: Userform for Macro "Find & Replace in all Documents" Add to elertz [Login to view extended thread Info.] Archived from groups: per prev. post (more info?)
"shabutt" wrote:
> Dear Mr. Robbins,
>
> I did what you instructed me to do but was unable to run the userform.
Why? What did you try exactly?
Vague statements of helplessness make it difficult to provide accurate and
useful replies.
It is always a good idea to take the few more minutes it takes to describe
what was attempted, the expected results and the observed results.
Meanwhile, try this:
In a standard module, use code like this:
(Assuming that your userform is named "UserForm1" and that one of the
buttons contains the code Doug Posted.)
Dim myForm As UserForm1
Set myForm = New UserForm1
myForm.Show
Unload myForm
Set myForm = Nothing
If you want to keep the userform on the screen to do many consecutive Find/
Replace, use the code I suggest above and modify Doug's code like this:
Use Doug's code for a button called "Repalce" but remove the
Me.Hide
statement from the code.
Create an "Exit" button with the code:
Me.Hide
If you want a "Clear" button use this code for this third button
With Me
.txtFind.Text = ""
.txtReplacement.Text = ""
.txtFind.SetFocus
End With
(Msg. 7) Posted: Tue Jul 08, 2008 8:26 pm
Post subject: Re: Userform for Macro "Find & Replace in all Documents" Add to elertz [Login to view extended thread Info.] Archived from groups: per prev. post (more info?)
I would have to agree about being very explicit, and clear, when you ask
things.
Here is a possible alternative to making a userform, although if you actually
want a Clear (although...why exactly?) button.
Do you know how to put a button on a toolbar? That may be the easiest. What
I mean is, HOW do you actually want to call the code below? You could, as I
said, put it as a button on a toolbar, then you can just click it and the
code runs. Or, you could have it as a keyboard shortcut.
In any case, if the issue is - and again, please be clear about what IS the
issue - simply an easy way to get a string (a word, or a phrase) that you
want to find, and a string (a word or phrase) to replace it with:
Sub FRinallDoc()
Dim aDoc As Document
Dim r As Range
Dim myFind As String
Dim myReplace As String
myFind = Inputbox("Enter text to find.")
myReplace = Inputbox("Enter replacement text.")
For Each aDoc In Application.Documents
Set r = aDoc.Range
With r.Find
.Text = myFind
.Replacement.Text = myReplace
.Execute Replace:=wdReplaceAll
End With
Next
End Sub
This displays an inputbox for the Find string, an inputbox for the Replace
string...and goes and does it.
Now if you want to be able to do multiple operations, then yes, absolutely, a
userform would be best. Again though, you need something to call, to display
(Show) the userform. This code can be attached to a button on the toolbar, a
menu item, or a keyboard shortcut.
Jean-Guy Marcil wrote:
>> Dear Mr. Robbins,
>>
>> I did what you instructed me to do but was unable to run the userform.
>
>Why? What did you try exactly?
>Vague statements of helplessness make it difficult to provide accurate and
>useful replies.
>It is always a good idea to take the few more minutes it takes to describe
>what was attempted, the expected results and the observed results.
>
>Meanwhile, try this:
>In a standard module, use code like this:
>(Assuming that your userform is named "UserForm1" and that one of the
>buttons contains the code Doug Posted.)
>
>Dim myForm As UserForm1
>
>Set myForm = New UserForm1
>
>myForm.Show
>
>Unload myForm
>
>Set myForm = Nothing
>
>If you want to keep the userform on the screen to do many consecutive Find/
>Replace, use the code I suggest above and modify Doug's code like this:
>
>Use Doug's code for a button called "Repalce" but remove the
> Me.Hide
>statement from the code.
>
>Create an "Exit" button with the code:
> Me.Hide
>
>If you want a "Clear" button use this code for this third button
>
>With Me
> .txtFind.Text = ""
> .txtReplacement.Text = ""
> .txtFind.SetFocus
>End With
(Msg. 8) Posted: Tue Jul 08, 2008 10:31 pm
Post subject: Re: Userform for Macro "Find & Replace in all Documents" Add to elertz [Login to view extended thread Info.] Archived from groups: per prev. post (more info?)
Dear Mr. Marcil (MVP),
Thank you very much for the code and detailed explanation as well. I am able
to run the macro successfully. It is wonderful to have a lot of options in
the userform and this is only possible due to your guidance.
Thanks are also due to Mr. Doug (MVP) because without his initial posting I
would have abandoned the thought of ever coming close to the solution.
Thank you guys for the wonderful help and support. I am really indebted.
Here is the userform code which has four buttons namely OK (for single Find
& Replace), REPLACE (for multiple Find & Replace ), CLEAR (for clearing the
textboxes), & CANCEL (for exiting/closing).
Private Sub OK_Click()
Dim aDoc As Document
Dim r As Range
If Len(txtFind.Text) > 0 Then
For Each aDoc In Application.Documents
Set r = aDoc.Range
With r.Find
.Text = txtFind.Text
.Replacement.Text = txtReplacement.Text
.Execute REPLACE:=wdReplaceAll
End With
Next
End If
Me.Hide
End Sub
Private Sub REPLACE_Click()
Dim aDoc As Document
Dim r As Range
If Len(txtFind.Text) > 0 Then
For Each aDoc In Application.Documents
Set r = aDoc.Range
With r.Find
.Text = txtFind.Text
.Replacement.Text = txtReplacement.Text
.Execute REPLACE:=wdReplaceAll
End With
Next
End If
End Sub
Private Sub CLEAR_Click()
With Me
.txtFind.Text = ""
.txtReplacement.Text = ""
.txtFind.SetFocus
End With
End Sub
Private Sub CANCEL_Click()
Me.Hide
End Sub
Here is the module code.
Sub FRinallDoc()
Dim myForm As UserForm1
Set myForm = New UserForm1
myForm.Show
Unload myForm
Set myForm = Nothing
End Sub
"Jean-Guy Marcil" wrote:
> "shabutt" wrote:
>
> > Dear Mr. Robbins,
> >
> > I did what you instructed me to do but was unable to run the userform.
>
> Why? What did you try exactly?
> Vague statements of helplessness make it difficult to provide accurate and
> useful replies.
> It is always a good idea to take the few more minutes it takes to describe
> what was attempted, the expected results and the observed results.
>
> Meanwhile, try this:
> In a standard module, use code like this:
> (Assuming that your userform is named "UserForm1" and that one of the
> buttons contains the code Doug Posted.)
>
>
> Dim myForm As UserForm1
>
> Set myForm = New UserForm1
>
> myForm.Show
>
> Unload myForm
>
> Set myForm = Nothing
>
>
> If you want to keep the userform on the screen to do many consecutive Find/
> Replace, use the code I suggest above and modify Doug's code like this:
>
> Use Doug's code for a button called "Repalce" but remove the
> Me.Hide
> statement from the code.
>
> Create an "Exit" button with the code:
> Me.Hide
>
>
> If you want a "Clear" button use this code for this third button
>
> With Me
> .txtFind.Text = ""
> .txtReplacement.Text = ""
> .txtFind.SetFocus
> End With
>
All times are: Eastern Time (US & Canada) (change) Goto page 1, 2
Page 1 of 2
You can post new topics in this forum You can reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum