Home > Back-end >  MS Word VBA to select any word file to open from folder path for copy/paste
MS Word VBA to select any word file to open from folder path for copy/paste

Time:10-26

I created 2 macros in MS Word VBA, the 1st one to select any docx file from a specified folder below as follows:

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.

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. The 1st macro works fine as i'm able to select any docx file and it opens successfully. The second macro which is supposed to open the document a and then run the 1st macro which is call test and that works. 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.

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.

Any help would be greatly appreciate it and not that familiar with word vba and 1st time ever doing it. 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

CodePudding user response:

Since you are working within Word, you do not need to create a new Word.Application as you can use the existing instance.

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.

I also recommend to work with the Range object rather than relying on Selection as it is unnecessary most of the time.

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
  • Related