# MS Word VBA select file & open file from folder



## daustin51 (Oct 22, 2021)

Hi All,hope all is well!

This is my first crack at MS Word VBA. 

I'm trying to do a simple macro where I have document A word doc that's opened, and want to write a MS word VBA code that allows me to select any word file in a directory path and opens the file where I can copy the contents from that selected document that opened and paste into document A and closes the file that was selected and keeping document A open. 

here is some of the code below:

I have the second part of instructions down on how to copy & paste, just need the 1st part on selecting a file & opening it from a folder

Sub Copy_Content()
   ''need code to to select file, than open file to select content on doc to paste into document a''
    Selection.WholeStory
    Selection.Copy
    Documents("documenta.docx").Activate
    Selection.EndKey wdStory
    Selection.PasteAndFormat Type:=wdFormatOriginalFormatting
    Documents("documentb.docx").Close
End Sub


----------



## daustin51 (Oct 25, 2021)

Ok, so I have an update to the above question. I created 2 macros, the 1st one to select any file from a specified folder.

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 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 so that I can copy contents from document B into document 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. What i'm trying to do is copy content from document B from 1st macro that opens the file and paste it into document a that's open in the 2nd macro below in which I attempted to call macro 1 to open B with no success. 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


----------



## daustin51 (Oct 26, 2021)

Nevermind,  was able to get it to work, will post code shortly.


----------

