VBA Open and Insert User Form Textbox Inputs into Word from Excel

jyablinsky

New Member
Joined
Dec 29, 2008
Messages
33
I have a user form that enters information into a data sheet but also need to enter some of the inputs into a Word document. The word document is about three pages long, but I only need to add certain inputs into certain parts of the Word document. Is it possible to open a specific word document from my desktop and add userform inputs into certain locations in the document? What would the code look like?

Any help is greatly appreciated. Thanks in advance.
 

Excel Facts

Whats the difference between CONCAT and CONCATENATE?
The newer CONCAT function can reference a range of cells. =CONCATENATE(A1,A2,A3,A4,A5) becomes =CONCAT(A1:A5)
There are 2 methods that come to mind: Formfields and Bookmarks in your doc file. See this link for an example.
 
Upvote 0
Thanks so much for the quick reply. I couldn't get the form fields to work. Below is my code. I need to find "<1>" and replace it with CustomerName.Input from a userform in Excel. I tried doing a find and replace but couldn't get that to work either. I really really appreciate any help!

Dim objWord As Object, wd As Object
Set objWord = CreateObject("Word.Application")

Set wd = ObjectWord.Documents.Open("W:\Accrual Programs\QPP & SA Contracts. Contract Traveler\FOR EXCEL USE ONLY.doc")
ObjectWord.Visible = True
ObjectWord.Selection.Find.ClearFormatting
ObjectWord.Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = "<1>"
.Replacement.Text = CustomerNameInput.Value
End With
Selection.Find.Execute Replace:=ObjectWord.ReplaceAll
 
Upvote 0
Oh, so you want to do it in a 3rd way? Ok, the best way is to start by recording a macro in MSWord just as we would in Excel. As you can see from this example, you need to Execute the Find/Replace.
Code:
Selection.Find.ClearFormatting
    Selection.Find.Replacement.ClearFormatting
    With Selection.Find
        .Text = "<1>"
        .Replacement.Text = "Hobson"
        .Forward = True
        .Wrap = wdFindContinue
        .Format = False
        .MatchCase = False
        .MatchWholeWord = False
        .MatchWildcards = False
        .MatchSoundsLike = False
        .MatchAllWordForms = False
    End With
    Selection.Find.Execute Replace:=wdReplaceAll

Since you are doing it in MSWord, you will have to prefix Selection with your wd object for the document. The wdReplaceAll will need a prefix, wdreplace, wdreplace.wdReplaceAll.

Tip: Use the Tag property for your textbox controls. This will let you iterate all the textbox controls in a loop.
e.g.
Code:
If CustomerNameInput.Tag <> "" then
  .Text = "<" & CustomerNameInput.Tag & ">"
  .Replacement.Text = CustomerNameInput.Value
End If

The first step would be to get one textbox value inserted. We can always work on the Tag loop later.

Tip2: You can use early binding to make coding easier. e.g.
Code:
Dim w As Word.Application
Dim d As Word.Document
 
Last edited:
Upvote 0

Forum statistics

Threads
1,214,599
Messages
6,120,449
Members
448,966
Latest member
DannyC96

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