![]() |
|
#1
|
||||
|
||||
![]()
Try the following macro. It should be saved in a new macro-enabled (docm) document in the same folder as the original mailmerge main documents. When you've created the Excel Project Data File for the project (assumed to be named 'ProjectDataFile.xlsx' for all projects), simply run the macro and let it create the 100 or so project documents with the merged output in the same folder that Excel file is in. The macro includes a folder browser, so all you need do is select the folder containing the Excel file.
With this approach, there's no need to copy all 100 or so mailmerge main documents, individually link them to the Excel file, merge to a new file, and so on. All you might need to do after running the macro is to delete any unwanted output documents for the particular project. Code:
Sub GenerateProjectDocuments() Application.ScreenUpdating = False Application.DisplayAlerts = wdAlertsNone Dim strFolder As String, StrSrc As String, strPath As String, strFile As String Dim strDocNm As String, wdDoc As Document, FlFmt As Long strDocNm = ActiveDocument.FullName: strPath = ActiveDocument.Path strFolder = GetFolder: If strFolder = "" Then Exit Sub StrSrc = strFolder & "\ProjectDataFile.xlsx" strFile = Dir(strPath & "\*.doc", vbNormal) While strFile <> "" If strPath & "\" & strFile <> strDocNm Then Set wdDoc = Documents.Open(FileName:=strPath & "\" & strFile, AddToRecentFiles:=False, Visible:=False) With wdDoc FlFmt = .SaveFormat With .MailMerge .MainDocumentType = wdFormLetters .Destination = wdSendToNewDocument .OpenDataSource Name:=StrSrc, ReadOnly:=True, AddToRecentFiles:=False, LinkToSource:=False, _ Connection:="Provider=Microsoft.ACE.OLEDB.12.0;User ID=Admin;" & _ "Data Source=StrSrc;Mode=Read;Extended Properties=""HDR=YES;IMEX=1"";", _ SQLStatement:="SELECT * FROM `Sheet1$`", SQLStatement1:="", SubType:=wdMergeSubTypeAccess .DataSource.ActiveRecord = wdFirstRecord .DataSource.LastRecord = .DataSource.ActiveRecord .Execute Pause:=False End With .Close SaveChanges:=False End With With ActiveDocument .SaveAs2 FileName:=strFolder & "\" & strFile, FileFormat:=FlFmt, AddToRecentFiles:=False .Close SaveChanges:=False End With End If strFile = Dir() Wend Set wdDoc = Nothing Application.DisplayAlerts = wdAlertsAll Application.ScreenUpdating = True End Sub Function GetFolder() As String Dim oFolder As Object GetFolder = "" Set oFolder = CreateObject("Shell.Application").BrowseForFolder(0, "Choose a folder", 0) If (Not oFolder Is Nothing) Then GetFolder = oFolder.Items.Item.Path Set oFolder = Nothing End Function
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#2
|
|||
|
|||
![]()
That's fantastic. Thank you. I'll give it a try now.
In terms of the deleting, that part doesn't matter as it's really only the physical printing of the documents that we pick and choose. We generally mail merge them all at the start just in case, as jobs can change mid-way. I'll come back shortly after testing. Thank you. |
#3
|
|||
|
|||
![]()
Ok, a few quick notes for this one.
- I grabbed x20 documents and put them in a folder. - Assigned one of the documents (the index file) as the macro host file. - Renamed the XLSX file to the same as was in the script just to make it easy. - Ran the macro. The result was that it did about x5 of the x20 files. The other ones are still looking for a link back to the original XLSX filename as that's what we linked each mail merge to. Do I need to clear out the mail merge linking back to the original XLSX filename? If so, how do you go about clearing any link for Mail Merge? (I think the x5 files that worked were ones that I hadn't opened, clicked on 'Use An Existing List' and then saved without actually Finalising). The second part is in regards to the saving location, etc. It looks like the 'merged' files are saving into the same location as the XLSX document, not the actual macro file location. So I guess the best way to make this work would be create a new folder with the 'Project Name' and drop the XLSX document into that folder. Go back into the TEMPLATE folder, run the macro, when prompted with the folder browser, point it at the new Project Folder with the XLSX document and then it should save all the documents in there? EDIT: Sorry, you did mention the files would save to the same location as the XLSX document ![]() As an expansion of the above, is there a way to make this all work with subfolders? The x100 files are in about 15 different folders so I'm trying to keep it as neat as possible. Thank you again. |
![]() |
|
![]() |
||||
Thread | Thread Starter | Forum | Replies | Last Post |
![]() |
alistair_ | Excel Programming | 5 | 04-10-2017 08:27 AM |
![]() |
terrydennis | Word | 4 | 11-11-2015 05:04 PM |
![]() |
mit | Excel | 1 | 06-14-2011 10:15 AM |
Printing multiple page worksheet with watermark | zany | Excel | 2 | 11-27-2009 01:33 AM |
Outlook XP (2002) printing multiple attachments | couriced | Outlook | 0 | 10-13-2005 05:36 AM |