【问题标题】:Select Word Doc using Excel VBA使用 Excel VBA 选择 Word Doc
【发布时间】:2020-04-10 03:01:04
【问题描述】:

我想创建 Excel VBA 代码,要求用户打开一个预先存在的带有文本表单域的 Word 文档,并在这些表单域中输入现有的 Excel 数据。

我有将 Excel 数据写入 Word 文本表单字段的代码。

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

我不知道将Set wd= wdApp.Documents.Open("FilePath") 行转换为对话框。

是否存在用户可以通过单击 Windows 资源管理器而不是键入路径来选择文件的功能?

【问题讨论】:

标签: excel vba ms-word


【解决方案1】:

您希望用户输入 Word 文件的名称吗?你想要InputBox 方法吗?

    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)

如果我不明白你的问题,请告诉我。

【讨论】:

  • 是的,完全正确。是否存在用户可以通过单击 Windows 资源管理器而不是键入路径来选择文件的功能?
猜你喜欢
  • 2018-01-17
  • 1970-01-01
  • 2019-02-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多