简体   繁体   English

MS Word VBA 选择要从文件夹路径打开的任何单词文件进行复制/粘贴

[英]MS Word VBA to select any word file to open from folder path for copy/paste

I created 2 macros in MS Word VBA, the 1st one to select any docx file from a specified folder below as follows:我在 MS Word VBA 中创建了 2 个宏,第一个宏用于从下面的指定文件夹中选择任何 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

Then the 2nd word VBA macro that I'm working on is where I want to open the master document which is document A and then call the above macro to open document b that I selected from a directory path so that I can copy contents from document B into document a which is at the end of this post.然后我正在处理的第二个字 VBA 宏是我想打开主文档的地方,即文档 A,然后调用上面的宏打开我从目录路径中选择的文档 b,以便我可以从文档中复制内容B 进入本文末尾的文档 a。

However, the code is not working and been stuck on this for the past 8 hours and no luck finding the right combination anywhere online.但是,该代码不起作用,并且在过去 8 小时内一直停留在此问题上,并且在网上的任何地方都找不到正确的组合。 The 1st macro works fine as i'm able to select any docx file and it opens successfully.第一个宏工作正常,因为我可以选择任何 docx 文件并且它成功打开。 The second macro which is supposed to open the document a and then run the 1st macro which is call test and that works.第二个宏应该打开文档 a 然后运行第一个宏,它是 call test 并且可以工作。 but where the code is not working is after I run the call test macro, there is no copying & pasting happening such as I was under the impression that selection.whole & selection.copy would work once I run the call test macro that opens up the document b file.但是代码不起作用的地方是在我运行呼叫测试宏之后,没有发生复制和粘贴,就像我的印象是 selection.whole & selection.copy 一旦我运行打开的呼叫测试宏就会工作文档 b 文件。

so in the end, I want to open up document b from call test macro, select the data from document b to copy onto document a that's open as well.所以最后,我想从调用测试宏中打开文档 b,从文档 b 中选择数据以复制到同样打开的文档 a 上。

Any help would be greatly appreciate it and not that familiar with word vba and 1st time ever doing it.任何帮助都将不胜感激,并且对 vba 一词并不熟悉,并且第一次这样做。 Thanks in advance.提前致谢。

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

Since you are working within Word, you do not need to create a new Word.Application as you can use the existing instance.由于您在 Word 中工作,因此无需创建新Word.Application因为您可以使用现有实例。

I suggest you convert sub test to a function so that you can return the Document object that you opened in test back to your calling sub.我建议您将子test转换为函数,以便您可以将在test打开的Document对象返回给调用子。

I also recommend to work with the Range object rather than relying on Selection as it is unnecessary most of the time.我还建议使用Range对象而不是依赖Selection因为它在大多数情况下是不必要的。

Please try the code below:请尝试以下代码:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM