inserting filenames in concatenate-files macro

S

steve.breslin

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.
 
D

Doug Robbins - Word MVP

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.

--
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
 
S

steve.breslin

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
}
 
D

Doug Robbins - Word MVP

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.

--
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
 
S

steve.breslin

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
 
S

steve.breslin

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top