Home |
Search |
Today's Posts |
#10
Posted to microsoft.public.word.mailmerge.fields
|
|||
|
|||
Mailmerge field result to insert word document
Ideally, your data source would actually store the complete pathname of
each file, rather than the "filename" part. How to do that would depend very much on your data source and your ability to program it - you might for example be able to add a column to the data source and run a macro to populate it with all the relevant file pathnames prior to merging. An alternative that /may/ work given that your file names are very simple is a. to create a document - let's call it "c:\a\list.htm" - that lists each file path, bookmarked with the filename in the format needed by the merge. b. to use another INCLUDETEXT to translate the filename into the filepath, e.g. like this: { QUOTE { INCLUDETEXT "{ INCLUDETEXT "c:\\a\\list.htm" "{ MERGEFIELD EntryID }doc" }" } So let's assume that you have an EntryID of 12345, and the corresponding file is at c:\folder1\folder2\12345.doc Before the merge you would run a macro that would create a file which contained a list of pathnames, including c:\\folder1\\folder2\\12345.doc This pathname would be bookmarked with the name "12345doc" Then you do the merge. For the record where EntryID is 12345, "{ MERGEFIELD EntryID }doc" is 12345doc "{ INCLUDETEXT "c:\\a\\list.htm" "12345doc" }" is "c:\\folder1\\folder2\\12345.doc" so we then { QUOTE { INCLUDETEXT "c:\\folder1\\folder2\\12345.doc" } } which should do what you need. A macro that can build such a list is as follows. However, it depends on your filenames being simple and consistent - e.g., all the files have to have the same extension, and must convert easily into valid bookmark names. You'll find notes on how to install macros etc. on Graham's page at http://www.gmayor.com/installing_macro.htm Sub createFileList() ' creates and populates ' an HTML file that can be used to map ' file names to file pathnames for a ' Word MailMerge ' This is not a general-purpose routine! ' For it to work, the file names have to be ' simple enough that removing "." characters ' turns them into valid bookmark names. ' In the VB Editor, with this macro open, ' use Tools-References to locate and check ' "Microsoft Scripting Runtime" ' You could produce the output ' using the "File System Object" if you ' prefer that to the old VB OPEN/PRINT/CLOSE ' commands. ' Also needs error handling! ' for example, a file called ' c:\folder1\folder2\filename.doc ' will be listed as ' pa name="filenamedoc"c:\\folder1\\folder2\\filename. doc/a/p ' In the mailmerge, if the "filename" part ' of the name is in a column called "fn", ' all the files to be included are ".doc" files ' and the HTML file is called c:\a\list.htm, ' it should be possible to include that file ' using the following nested field codes: ' { INCLUDETEXT "{ INCLUDETEXT "c:\\a\\list.htm" "{ MERGEFIELD fn }doc" } } ' HTML was chosen as the output format because it ' is easier to produce than a Word .doc file ' Set this to the pathname of the ' top folder in your folder tree Const topFolderName As String = "c:\a" ' Set this to the pathname of the ' HTML file that your Mail Merge ' Main document will reference. Const listFileName As String = "c:\a\list.htm" Dim lngFileNumber As Long Dim objFile As Scripting.File Dim objFiles As Scripting.Files Dim objFolder As Scripting.Folder Dim objFSO As Scripting.FileSystemObject Set objFSO = CreateObject("Scripting.FileSystemObject") Set objFolder = objFSO.GetFolder(topFolderName) lngFileNumber = FreeFile Open listFileName For Output As lngFileNumber Print #lngFileNumber, "htmlbody" Set objFiles = objFolder.Files For Each objFile In objFiles Call addFileToList(objFile, lngFileNumber) Next Set objFiles = Nothing Call processSubFolders(objFolder, lngFileNumber) finish: Print #lngFileNumber, "/body/html" Close #lngFileNumber Set objFolder = Nothing Set objFSO = Nothing End Sub Sub processSubFolders(objFolder As Scripting.Folder, lngFileNumber As Long) Dim objFile As Scripting.File Dim objFiles As Scripting.Files Dim objFolders As Scripting.Folders Dim objSubFolder As Scripting.Folder Set objFolders = objFolder.SubFolders For Each objSubFolder In objFolders Set objFiles = objSubFolder.Files For Each objFile In objFiles Call addFileToList(objFile, lngFileNumber) Next Call processSubFolders(objSubFolder, lngFileNumber) Next Set objFolders = Nothing End Sub Sub addFileToList(objFile As Scripting.File, _ lngFileNumber As Long) Print #lngFileNumber, "pa name=""" & Replace(objFile.Name, ".", "") & """" & Replace(objFile.Path, "\", "\\") & "/a/p" End Sub FWIW, the only other way I could think of that had some potential to do this would be to manually drag links (Windows shortcuts) for every document to a single folder, then include all the files from that folder, adjusting the file name as necessary (e.g. instead of 12345.doc, you might need to include 12345.doc.lnk ) Unfortunately, when I tried this, Word seems to treat the .lnk files or the linked files as text files and does not open them as Word files. Peter Jamieson http://tips.pjmsn.me.uk On 18/12/2009 13:46, Primus wrote: Partly solved: The following is now working for me as long at the { MERGEFIELD EntyrID}.doc resided in the Q:\\BookData\\Editor\\Entries\\ folder { INCLUDETEXT "Q:\\BookData\\Editor\\Entries\\{ MERGEFIELD EntryID }.doc" \* MERGEFORMAT } However I need the INCLUDETEXT command to look at all the Subfolders contained within the Entries folder and there are hundreds of these! |
Thread Tools | |
Display Modes | |
|
|
Similar Threads | ||||
Thread | Forum | |||
Formatting a field result in Word | Mailmerge | |||
limiting mailmerge result | Mailmerge | |||
Can you insert a mailmerge field but not print it? | Mailmerge | |||
copy a field result into a footer of a document | Microsoft Word Help | |||
Result in form field to generate comment in next field | Microsoft Word Help |