JohnHarveyCH
New Member
- Joined
- Mar 10, 2022
- Messages
- 2
- Office Version
- 2019
- Platform
- Windows
Hello good people of the Internet, I hope you can help save my sanity as I've been working on this for a while now to no avail!
I have a lot of Word files in a folder. These are named "Blue 1", "Blue 2", "Blue 3" etc. I have data that is stored in corresponding Excel files one folder down the structure (eg in "Sessions" contained in my main folder), each named "1", "2", "3" etc with variable-length data in them that needs to be pasted into the Word files.
My question is, how can I get a macro to open each Word file, open the corresponding numbered Excel file, grab all the data that's in there and paste it into my Word folder? I also have the data-to-be-pasted in a big list also containing my 1, 2, 3 "session codes" if that can be used rather than the individual files.
I create the Word files also from individual Excel files, running a macro that will create individual mailmerged DOCXs from everything that's in a specific folder. My thinking is that code to paste in data from other excel files could be included in this sub?
I have a lot of Word files in a folder. These are named "Blue 1", "Blue 2", "Blue 3" etc. I have data that is stored in corresponding Excel files one folder down the structure (eg in "Sessions" contained in my main folder), each named "1", "2", "3" etc with variable-length data in them that needs to be pasted into the Word files.
My question is, how can I get a macro to open each Word file, open the corresponding numbered Excel file, grab all the data that's in there and paste it into my Word folder? I also have the data-to-be-pasted in a big list also containing my 1, 2, 3 "session codes" if that can be used rather than the individual files.
I create the Word files also from individual Excel files, running a macro that will create individual mailmerged DOCXs from everything that's in a specific folder. My thinking is that code to paste in data from other excel files could be included in this sub?
VBA Code:
Sub PlanMerge()
'Set a reference to Microsoft Scripting Runtime by using
'Tools > References in the Visual Basic Editor (Alt+F11)
'Declare the variables
Dim objFSO As FileSystemObject
Dim objFolder As Folder
Dim objFile As File
Dim strPath As String
Dim intResult As Integer
Dim venue
venue = "Blue" 'find Excel files with Blue in the name
With Application.FileDialog(msoFileDialogFolderPicker)
.Show
strPath = .SelectedItems(1)
End With
'Specify the path to the folder
'Create an instance of the FileSystemObject
Set objFSO = CreateObject("Scripting.FileSystemObject")
'Get the folder
Set objFolder = objFSO.GetFolder(strPath)
'If the folder does not contain files, exit the sub
If objFolder.Files.Count = 0 Then
MsgBox "No files were found...", vbExclamation
Exit Sub
End If
'Turn off screen updating
Application.ScreenUpdating = False
'Loop through each file in the folder
For Each objFile In objFolder.Files
If InStr(objFile.Name, venue) Then
'opens file and relevant sheet
ActiveDocument.MailMerge.OpenDataSource Name:=objFile _
, ConfirmConversions:=False, ReadOnly:=False, LinkToSource:=True, _
AddToRecentFiles:=False, PasswordDocument:="", PasswordTemplate:="", _
WritePasswordDocument:="", WritePasswordTemplate:="", Revert:=False, _
Format:=wdOpenFormatAuto, Connection:="" _
, SQLStatement:="SELECT * FROM `Sheet1$`", SQLStatement1:="", SubType:= _
wdMergeSubTypeAccess
'mail merge options
With ActiveDocument.MailMerge
.Destination = wdSendToNewDocument
.SuppressBlankLines = True
With .DataSource
.FirstRecord = wdDefaultFirstRecord
.LastRecord = wdDefaultLastRecord
End With
.Execute Pause:=False
End With
'excel data added here?
'save
ChangeFileOpenDirectory strPath
ActiveDocument.SaveAs2 FileName:=objFile & ".docx", _
FileFormat:=wdFormatXMLDocument, LockComments:=False, Password:="", _
AddToRecentFiles:=True, WritePassword:="", ReadOnlyRecommended:=False, _
EmbedTrueTypeFonts:=False, SaveNativePictureFormat:=False, SaveFormsData _
:=False, SaveAsAOCELetter:=False, CompatibilityMode:=15
ActiveDocument.Close
End If
Next objFile
'Turn screen updating back on
Application.ScreenUpdating = True
End Sub