vba: Textbox value to a MS word text

Xlacs

Board Regular
Joined
Mar 31, 2021
Messages
105
Office Version
  1. 2016
Platform
  1. Windows
Hello Everyone!

I have this simple problem and I'm wondering if there is a way to get a textbox value to a Ms word template.
For example my textbox contains the current date and I wanted to get that value to my ms word.

I wanted to replace my MS word #Date# to the textbox value and so on..

I have provided a screenshot and code.

Hoping someone can help me solve this one. Thank you!

VBA Code:
FilePath = "D:\" & Me.ComboBox1 & "_" & Me.TextBox4 & ".pdf"
On Error Resume Next
Kill FilePath
DocFile = "D:\PHOTO\Sample.docx" 'Template File
Dim WordApp As Word.Application
Dim WordDoc As Word.Document



Set WordApp = New Word.Application
Set WordDoc = WordApp.Documents.Open(DocFile, False)
Word.Application.Visible = True

With WordDoc.Content.Find
    .Text = "#" & Me("Label").Caption & "#"
    .Replacement.Text = Me("Textbox").Value
    .Execute Replace:=wdReplaceAll
End With


WordDoc.ExportAsFixedFormat OutputFileName:=FilePath, ExportFormat:=wdExportFormatPDF
WordDoc.Close saveChanges:=False
WordApp.Quit
Set WordApp = Nothing
Set WordDoc = Nothing

MsgBox "Success!"
 

Attachments

  • ss1.png
    ss1.png
    6.2 KB · Views: 41
  • ss2.png
    ss2.png
    12.5 KB · Views: 40

Excel Facts

Square and cube roots
The =SQRT(25) is a square root. For a cube root, use =125^(1/3). For a fourth root, use =625^(1/4).
With WordDoc.Content.Find .Text = "#" & Me("Label").Caption & "#" .Replacement.Text = Me("Textbox").Value .Execute Replace:=wdReplaceAll End With
Excel Formula:
With WordDoc.Content.Find
    .Text = "#" & left(Me("Label").Caption,len(Me("Label").Caption)-1) & "#"
    .Replacement.Text = Me("Textbox").Value
    .Execute Replace:=wdReplaceAll
End With
 
Upvote 0
With WordDoc.Content.Find .Text = "#" & left(Me("Label").Caption,len(Me("Label").Caption)-1) & "#" .Replacement.Text = Me("Textbox").Value .Execute Replace:=wdReplaceAll End With

Thank you.

But it seems nothing has changed.
 
Upvote 0
@Xlacs - please do not mark a post as a solution if it doesn't contain an answer or a method that solves the problem in order to help future readers.

Regarding your question. I would stay away from Replace&Find in such cases, instead, I would select the #Date# section on the Word document and create a bookmark with a certain name, then access its range to set the text property easily.

1645121786101.png

Select a range and add a Bookmark in Word

  1. Select #Date# range on the Word document.
  2. Click Insert tab on Ribbon and click Bookmark.
  3. Enter the bookmark name, "datefield" (or whatever you like).
  4. Click Add, then the Bookmark dialog is closed.
Now you have a named range in the Word document that you can use instead of struggling with Find&Replace.

Replace the folllowing code section in the original code in Excel:
VBA Code:
With WordDoc.Content.Find
    .Text = "#" & Me("Label").Caption & "#"
    .Replacement.Text = Me("Textbox").Value
    .Execute Replace:=wdReplaceAll
End With

With the following line:
VBA Code:
WordDoc.Bookmarks("datefield").Range.Text = Me("Textbox").Value

And test it to see if it will work as you need.
 
Upvote 0
@Xlacs - please do not mark a post as a solution if it doesn't contain an answer or a method that solves the problem in order to help future readers.

Regarding your question. I would stay away from Replace&Find in such cases, instead, I would select the #Date# section on the Word document and create a bookmark with a certain name, then access its range to set the text property easily.

View attachment 58093
Select a range and add a Bookmark in Word

  1. Select #Date# range on the Word document.
  2. Click Insert tab on Ribbon and click Bookmark.
  3. Enter the bookmark name, "datefield" (or whatever you like).
  4. Click Add, then the Bookmark dialog is closed.
Now you have a named range in the Word document that you can use instead of struggling with Find&Replace.

Replace the folllowing code section in the original code in Excel:
VBA Code:
With WordDoc.Content.Find
    .Text = "#" & Me("Label").Caption & "#"
    .Replacement.Text = Me("Textbox").Value
    .Execute Replace:=wdReplaceAll
End With

With the following line:
VBA Code:
WordDoc.Bookmarks("datefield").Range.Text = Me("Textbox").Value

And test it to see if it will work as you need.

Hi, just wondering, I do have a listbox in userform.

When I used the above codes, only the first text of first column is showing. Any idea why?
 
Upvote 0

Forum statistics

Threads
1,215,026
Messages
6,122,738
Members
449,094
Latest member
dsharae57

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