MS Word VBA select file & open file from folder

daustin51

New Member
Joined
Oct 22, 2021
Messages
5
Office Version
  1. 2016
Platform
  1. Windows
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
 

Excel Facts

How can you turn a range sideways?
Copy the range. Select a blank cell. Right-click, Paste Special, then choose Transpose.
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
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,181
Members
453,022
Latest member
Mohamed Magdi Tawfiq Emam

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top