0
votes

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 have code that writes the Excel data into the Word text form field.

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

I am lost as to converting the Set wd= wdApp.Documents.Open("FilePath") line into a dialog box.

Does a function exist where the user can select the file by clicking through Windows Explorer as opposed to typing the path?

1
Application.GetOpenFileName - Tim Williams
Cross-posted at: mrexcel.com/board/threads/…. For cross-posting etiquette, please read: excelguru.ca/content.php?184 - macropod

1 Answers

0
votes

Do you want the user to input the name of a Word file? Do you want the InputBox method?

    Dim strWord As String
    strWord = InputBox(prompt:="Type the file path and name of the Word file.", title:="Which file?", default:="C:\Path\File.docx")
    Set wd = wdApp.Documents.Open(strWord)

Tell me if I didn't understand your question.