【发布时间】:2019-05-22 04:30:40
【问题描述】:
我已经打开了一个 Word 文档(“Template1”)。我正在尝试编写 VBA 代码来打开 Word 文档文件(“FileWithData”)并将信息从“FileWithData”复制到“Template1”。 “FileWithData”可以是任何文件,这就是我使用 OpenDialog 的原因。
一切正常,但在激活“FileWithData”窗口以启动“复制和粘贴”过程时我被卡住了。
Dim intChoice As Integer
Dim strPath As String
Dim objWord As Object
Set objWord = CreateObject("Word.Application")
objWord.Visible = True
Application.FileDialog(msoFileDialogOpen).AllowMultiSelect = False
intChoice = Application.FileDialog(msoFileDialogOpen).Show
'if the user selects a file
If intChoice <> 0 Then
'get the path selected
strPath = Application.FileDialog( _
msoFileDialogOpen).SelectedItems(1)
'opens the document
objWord.Documents.Open (strPath)
FileName = objWord.ActiveDocument.Name
在这里它坏了:(
Windows(FileName).Activate
Selection.GoTo What:=wdGoToBookmark, Name:="Page1"
With ActiveDocument.Bookmarks
.DefaultSorting = wdSortByName
.ShowHidden = True
End With
Selection.Copy
Windows("Template1").Activate
Selection.GoTo What:=wdGoToBookmark, Name:="Page1"
With ActiveDocument.Bookmarks
.DefaultSorting = wdSortByName
.ShowHidden = True
End With
Selection.PasteAndFormat (wdFormatOriginalFormatting)
Selection.MoveLeft Unit:=wdCharacter, Count:=2
我收到以下错误: 运行时错误“5941”
请求的集合成员不存在。
【问题讨论】: