Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
DiscussionsAccessExcelInfoPathOutlookPowerPointPublisherWord
DirectoryUser Groups
Related Topics
Outlook ExpressInternet ExplorerWindowsMS Server ProductsMore Topics ...

MS Office Forum / Word / Long Documents / March 2008

Tip: Looking for answers? Try searching our database.

inserting filenames in concatenate-files macro

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
steve.breslin@gmail.com - 03 Mar 2008 01:11 GMT
I have a bunch of DOC files that I need to combine into one master
file. That's no problem, but I need to include the filename before the
insertion of each file. So the master doc should look like this:

--

file1.doc

file1's contents inserted here.

file2.doc

file2's contents inserted here.

--

Please help. :)
Doug Robbins - Word MVP - 03 Mar 2008 09:36 GMT
I assume that you are doing this manually rather than through code (because
if the code was not problem for you, the filename should not be either)

If that is the way you are doing it, insert a { FILENAME } field at the top
of each the document - Use Ctrl+F9 to insert a pair of field delimiters { }
inside of which you type FILENAME (or filename, it doesn't matter), then
press Alt+F9 to toggle off the display of field codes, then select the field
and press F9 if necessary to cause the filename to be displayed, then use
Ctrl+Shift+F9 to unlink the field.  The last step will convert the field
result to ordinary text so that it will be preserved when you combine the
documents.

Signature

Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP

>I have a bunch of DOC files that I need to combine into one master
> file. That's no problem, but I need to include the filename before the
[quoted text clipped - 13 lines]
>
> Please help. :)
steve.breslin@gmail.com - 03 Mar 2008 10:04 GMT
On Mar 3, 4:36 am, "Doug Robbins - Word MVP" <d...@REMOVECAPSmvps.org>
wrote:
> I assume that you are doing this manually rather than through code (because
> if the code was not problem for you, the filename should not be either)

Clumsily asked question, sorry. -- I do need to do this in the code.
(I found code to combine the material on the web, and there's a recent
discussion of this topic on this newsgroup as well. I tried to figure
out how to modify it to insert filenames, but I didn't get very far.)

I figured such an algorithm would work something like this:

open dialogue to select files to insert.
iterate over the files to insert {
1. insert filename
2. insert file
}
Doug Robbins - Word MVP - 03 Mar 2008 12:13 GMT
It will be easier to tell you how to go about this if you post the code that
you already have do the insertion of the files.

Signature

Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP

> On Mar 3, 4:36 am, "Doug Robbins - Word MVP" <d...@REMOVECAPSmvps.org>
> wrote:
[quoted text clipped - 14 lines]
> 2. insert file
> }
steve.breslin@gmail.com - 03 Mar 2008 12:43 GMT
> It will be easier to tell you how to go about this if you post the code that
> you already have do the insertion of the files.

Thank you. It's copied below:

Public sBoilerFolder As String

Sub StartBoiler()
' Version 8 - 15 November 2007
' Macro originally created 05/01/97 by Woody Leonhard
' with modifications by Graham Mayor 2006 & 2007
' and by Greg Maxey 2007

Dim fDialog As FileDialog
Dim i As Long
Set fDialog = Application.FileDialog(msoFileDialogFolderPicker)
With fDialog
   .Title = "Select Folder containing the documents to be inserted
and click OK"
   .AllowMultiSelect = False
   If .Show <> -1 Then
       Exit Sub
   End If
End With

start:
On Error GoTo error

If Left(sBoilerFolder, 1) = Chr(34) Then
   sBoilerFolder = Mid(sBoilerFolder, 2, Len(sBoilerFolder) - 2)
End If
frmBoilerMain.Show
Exit Sub

error:
If Err = 4248 Then
Documents.Add
GoTo start
End If
End Sub
steve.breslin@gmail.com - 03 Mar 2008 23:45 GMT
A little borrowing from other posts on this newsgroup, plus a little
trial and error, and I hacked together something that works. It's
probably not the correct way to do it, but I've copied it below in
case people are interested. Thanks Doug for the help!

By the way, what's the best way to become competent with this kind of
stuff for Word and Excel? Is one of the books I've seen considered
better than the rest? Or is there a particularly good in-depth online
tutorial?

Sub combiner()
'
' combiner Macro
' Macro created 3/3/2008 by breslin
'
   Dim DestDoc As Document
   Dim oRg As Range
   Dim MyPath As String
   Dim MyName As String

   'let user select a path
   With Dialogs(wdDialogCopyFile)
       If .Display() <> -1 Then Exit Sub
       MyPath = .Directory
   End With

   'strip quotation marks from path

   If Len(MyPath) = 0 Then Exit Sub

   If Asc(MyPath) = 34 Then
       MyPath = Mid$(MyPath, 2, Len(MyPath) - 2)
   End If

   Set DestDoc = Documents.Add
   DestDoc.Save ' pop up SaveAs dialog

   Set oRg = DestDoc.Range
   oRg.Collapse wdCollapseEnd

   'get files from the selected path
   'and insert them into the doc
   MyName = Dir$(MyPath & "*.doc")
   Do While MyName <> ""
       On Error Resume Next
       oRg.InsertFile FileName:=MyPath & MyName, _
           ConfirmConversions:=False, Link:=False
       oRg.InsertAfter MyName & vbCr
       If Err.Number = 0 Then
           DestDoc.Save
           Set oRg = DestDoc.Range
           oRg.Collapse wdCollapseEnd
       End If
       MyName = Dir
   Loop

End Sub

Rate this thread:






 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.