【发布时间】: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 资源管理器而不是键入路径来选择文件的功能?
【问题讨论】:
-
Application.GetOpenFileName -
交叉发布于:mrexcel.com/board/threads/…。发帖礼仪请阅读:excelguru.ca/content.php?184