Inserting string into Word as find/replace

DblDocWhitaker

New Member
Joined
Mar 31, 2020
Messages
10
Office Version
  1. 2016
Platform
  1. Windows
I am not a programmer, but am trying to build a user interface in Excel that will facilitate my colleagues in auto completing letters and forms. Right now I have the interface moving the user thru a series of questions to identify the form too be used and the info to be inserted (info put into text box or drop down menu saved as strings). I also have it set to open the appropriate document/template. However, I cannot figure out how to to find text in the word document and replace it with the string entered into the text box. I can get the doc to open.

For example, I have:
Dim NameFull As String
NameFull = Me.txNameFull.Value
'open word doc code here

In the Document I have <<Full name>> that I want to replace with the string saved as NameFull. I tried controlcontent and had no luck - figured find/replace might be easier. So far not really.

Thanks!!
 

Excel Facts

Get help while writing formula
Click the italics "fx" icon to the left of the formula bar to open the Functions Arguments dialog. Help is displayed for each argument.
I use something like this:

Rich (BB code):
   With Selection.Find
      .ClearFormatting
      .replacement.ClearFormatting
      .Text = "<<Full name>>"
      .replacement.Text = NameFull
      .Forward = True
      .Wrap = wdFindContinue
      .MatchAllWordForms = False
      .Execute Replace:=wdReplaceAll
   End With
 
Upvote 0
Since you are doing this in Excel, if you are using late binding you will have to define these constants:

VBA Code:
Const wdFindContinue = 1
Const wdReplaceAll = 2
 
Upvote 0
I'm getting a similar error as before. It does not like:
With Selection.Find
Right now it is giving me
Run-time error 450
Wrong number of arguments or invalid property assignment.
Thought? I so much appreciate your help.
 
Upvote 0
I forgot you were doing this in Excel. I need to see all your code to see what binding you are using and how you have declared Word objects.
 
Upvote 0
Part of my code is below. I inserted your suggested "Const wdFindContinue = 1; Const wdReplaceAll = 2" before and inside the Find.Selection.

Thanks again!!


*******code below*********

Private Sub cmdCreate_Click()

Dim NameFull As String
NameFull = Me.txNameFull.Value
Dim Address1 As String
Address1 = Me.txAddress1.Value
' continue w remainder of blanks to fill in

'Select appropriate template; I've only been working on only 1 document for now
If BarDate <> "" And Type = "CRADA" Then
'MsgBox "BAR DATE CRADA"
ElseIf BarDate <> "" And Type = "FAR" Then
'MsgBox "BAR DATE FAR"
ElseIf BarDate = "" And Type = "CRADA" Then
'MsgBox "No bar date CRADA"
ElseIf BarDate = "" And Type = "FAR" Then
'MsgBox "No bar date FAR"
Set objDoc = objWord.Documents.Open("C:\ ... .docx")
'objWord.Visible = True
End If

With Selection.Find
.ClearFormatting
.replacement.ClearFormatting
.Text = "<<KFullName>>"
.replacement.Text = KNameFull
.Forward = True
.Wrap = wdFindContinue
.MatchAllWordForms = False
.Execute Replace:=wdReplaceAll
End With

End Sub
 
Upvote 0
I don't see where objWord is declared and set but I'll assume it's your Word application object.

Try it this way

With objWord.Selection.Find

If that doesn't work then

With objDoc.Selection.Find
 
Upvote 0
Change:
With Selection.Find
to:
With objDoc.Content.Find

A better way, IMHO, would be to bookmark the various ranges you want to replace and simply overwrite what's bookmarked with code like:
objDoc.Bookmarks("KFullName").Range.Text = KFullName
 
Upvote 0

Forum statistics

Threads
1,214,929
Messages
6,122,314
Members
449,081
Latest member
tanurai

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top