MS Word VBA 到 select 从 copy/paste 的文件夹路径打开的任何 word 文件
MS Word VBA to select any word file to open from folder path for copy/paste
我在 MS Word VBA 中创建了 2 个宏,第一个 select 来自以下指定文件夹的任何 docx 文件,如下所示:
Macro 1
Sub test()
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)
End If
End Sub
然后我正在处理的第二个字 VBA 宏是我要打开文档 A 的主文档的地方,然后调用上面的宏来打开我 select 从目录路径中编辑,以便我可以将文档 B 中的内容复制到位于此 post.
末尾的文档 a 中
但是,该代码无法正常工作,过去 8 小时一直停留在该代码上,并且没有运气在网上任何地方找到正确的组合。第一个宏工作正常,因为我能够 select 任何 docx 文件并成功打开。
第二个宏应该打开文档 a,然后 运行 第一个宏调用 test 并且可以正常工作。但是代码不起作用的地方是在我 运行 调用测试宏之后,没有复制和粘贴发生,例如我的印象是 selection.whole & selection.copy 一旦我就可以工作运行打开文档b文件的调用测试宏
所以最后,我想通过调用测试宏打开文档 b,select 将文档 b 中的数据复制到也打开的文档 a 中。
任何帮助将不胜感激,但对单词 vba 不是很熟悉并且第一次这样做。提前致谢。
Sub test6()
Application.ScreenUpdating = False
Dim strFile As String
strFile = "C:\Users\test\Desktop\tar sheet test\documenta.docx"
If Dir(strFile) <> "" Then
Documents.Open strFile
End If
Call test
Selection.WholeStory
Selection.Copy
Documents("documenta.docx").Activate '
Selection.EndKey wdStory '
Selection.PasteAndFormat wdPasteDefault
End Sub
由于您是在 Word 中工作,因此不需要创建新的 Word.Application
,因为您可以使用现有的实例。
我建议您将 sub test
转换为一个函数,这样您就可以 return 您在 test
中打开的 Document
对象返回到您的调用 sub。
我还建议使用 Range
对象而不是依赖 Selection
,因为它在大多数情况下是不必要的。
请尝试以下代码:
Sub test6()
Dim strFile As String
strFile = "C:\Users\test\Desktop\tar sheet test\documenta.docx"
Dim destDocument As Document
If Dir(strFile) <> "" Then
Set destDocument = Application.Documents.Open(strFile)
Dim srcDocument As Document
Set srcDocument = test
If Not srcDocument Is Nothing Then
srcDocument.Content.Copy
destDocument.Range(destDocument.Range.End - 1).PasteAndFormat wdPasteDefault
End If
End If
End Sub
Function test() As Document
Dim intChoice As Integer
Dim strPath As String
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
Set test = Application.Documents.Open(strPath)
End If
End Function
我在 MS Word VBA 中创建了 2 个宏,第一个 select 来自以下指定文件夹的任何 docx 文件,如下所示:
Macro 1
Sub test()
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)
End If
End Sub
然后我正在处理的第二个字 VBA 宏是我要打开文档 A 的主文档的地方,然后调用上面的宏来打开我 select 从目录路径中编辑,以便我可以将文档 B 中的内容复制到位于此 post.
末尾的文档 a 中但是,该代码无法正常工作,过去 8 小时一直停留在该代码上,并且没有运气在网上任何地方找到正确的组合。第一个宏工作正常,因为我能够 select 任何 docx 文件并成功打开。 第二个宏应该打开文档 a,然后 运行 第一个宏调用 test 并且可以正常工作。但是代码不起作用的地方是在我 运行 调用测试宏之后,没有复制和粘贴发生,例如我的印象是 selection.whole & selection.copy 一旦我就可以工作运行打开文档b文件的调用测试宏
所以最后,我想通过调用测试宏打开文档 b,select 将文档 b 中的数据复制到也打开的文档 a 中。
任何帮助将不胜感激,但对单词 vba 不是很熟悉并且第一次这样做。提前致谢。
Sub test6()
Application.ScreenUpdating = False
Dim strFile As String
strFile = "C:\Users\test\Desktop\tar sheet test\documenta.docx"
If Dir(strFile) <> "" Then
Documents.Open strFile
End If
Call test
Selection.WholeStory
Selection.Copy
Documents("documenta.docx").Activate '
Selection.EndKey wdStory '
Selection.PasteAndFormat wdPasteDefault
End Sub
由于您是在 Word 中工作,因此不需要创建新的 Word.Application
,因为您可以使用现有的实例。
我建议您将 sub test
转换为一个函数,这样您就可以 return 您在 test
中打开的 Document
对象返回到您的调用 sub。
我还建议使用 Range
对象而不是依赖 Selection
,因为它在大多数情况下是不必要的。
请尝试以下代码:
Sub test6()
Dim strFile As String
strFile = "C:\Users\test\Desktop\tar sheet test\documenta.docx"
Dim destDocument As Document
If Dir(strFile) <> "" Then
Set destDocument = Application.Documents.Open(strFile)
Dim srcDocument As Document
Set srcDocument = test
If Not srcDocument Is Nothing Then
srcDocument.Content.Copy
destDocument.Range(destDocument.Range.End - 1).PasteAndFormat wdPasteDefault
End If
End If
End Sub
Function test() As Document
Dim intChoice As Integer
Dim strPath As String
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
Set test = Application.Documents.Open(strPath)
End If
End Function