Editing Word Doc using Excel VBA

newtotheforum2019

New Member
Joined
Dec 17, 2019
Messages
1
Office Version
  1. 365
Platform
  1. Windows
Hi,

New to VBA so please bear with me. I want to create Excel VBA code that asks the user to open a pre-existing Word document with text form fields and input existing Excel data in these form fields. I currently have code that writes the Excel data into the Word text form field, but am lost as to converting the "Set wd= wdApp.Documents.Open("FilePath") line into a dialog box. Any assistance would be appreciated.

Thanks.


Sub NewMacro()

Dim wdApp As Object, wd As Object, ac As Long, ws As Worksheet
Set ws = Sheets("Tables")

On Error Resume Next

Set wdApp = GetObject(, "Word.Application")
If Err.Number <> 0 Then
Set wdApp = CreateObject("Word.Application")
End If
On Error GoTo 0

Set wd = wdApp.Documents.Open("C:\Test\Test.docx")
wdApp.Visible = True

With wd
.FormFields("CustomerName").Result = ws.Range("D4").Value

End With
Set wd = Nothing
Set wdApp = Nothing
End Sub
 

Excel Facts

Format cells as date
Select range and press Ctrl+Shift+3 to format cells as date. (Shift 3 is the # sign which sort of looks like a small calendar).
Try this

VBA Code:
Sub NewMacro()
  
  Dim wdApp As Object, wd As Object, ac As Long, ws As Worksheet
  Set ws = Sheets("Tables")
  
  On Error Resume Next
  Set wdApp = GetObject(, "Word.Application")
  If Err.Number <> 0 Then
    Set wdApp = CreateObject("Word.Application")
  End If
  On Error GoTo 0
  
  With Application.FileDialog(msoFileDialogFilePicker)
    .Title = "Select doc file"
    .Filters.Clear
    .Filters.Add "Docs files", "*.doc*"
    .AllowMultiSelect = False
    .InitialFileName = ThisWorkbook.Path & "\"
    If .Show Then
      Set wd = wdApp.Documents.Open(.SelectedItems.Item(1))
      wdApp.Visible = True
    Else
      Exit Sub
    End If
  End With
  
  With wd
    .FormFields("CustomerName").Result = ws.Range("D4").Value
  End With
  Set wd = Nothing
  Set wdApp = Nothing
End Sub
 
Upvote 0
Hi @newtotheforum2019, welcome to the board!

Please review the rules about cross posts.
Copy and paste here the links where you have asked the same question.
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,182
Members
453,020
Latest member
Mohamed Magdi Tawfiq Emam

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