One Document with 10 pages - macro to insert those 10 pages to multiple files

S

Stuart

Hi There,

I have a word document containing 10 pages (might not always be the
case). In a single directory I have around 120 words documents which
need these 10 pages incorporated into them at the beginning.

Is there a macro that could help with this?

Thanks in advance

Stuart
 
J

Jay Freedman

See http://www.word.mvps.org/FAQs/MacrosVBA/BatchFR.htm for the general
technique of working with an entire folder full of documents. Instead of
running a Replace as in the example code, you can just do

Selection.HomeKey unit:=wdStory
Selection.InsertFile FileName:="C:\folder\filename.doc"

--
Regards,
Jay Freedman
Microsoft Word MVP
Email cannot be acknowledged; please post all follow-ups to the newsgroup so
all may benefit.
 
S

Stuart

Hi Jay,

I used the code given in the link.

This brings up a search and replace window.

This is not what I want to do.

Where can I place your code below in a macro?

Thanks

Stuart
 
D

Doug Robbins - Word MVP

Sub InsertintoAllDocumentsInaFolder()

Dim MyPath As String
Dim MyName As String
Dim Mydoc As Document

'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

'get files from the selected path
'and insert them into the doc
MyName = Dir$(MyPath & "*.*")
Do While MyName <> ""
Set Mydoc = Documents.Open(MyName)
Selection.HomeKey unit:=wdStory
Selection.InsertFile FileName:="C:\folder\filename.doc"
Mydoc.Save
Mydoc.Close
MyName = Dir
Loop

End Sub


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

Jay Freedman

I couldn't have said it better myself. :)

--
Regards,
Jay Freedman
Microsoft Word MVP
Email cannot be acknowledged; please post all follow-ups to the newsgroup so
all may benefit.
 
D

Doug Robbins - Word MVP

Thanks, Jay

I was (almost) repeating myself as I had 99% of it lying around.

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

Stuart

Hi Doug and Jay

This didnt work unfortunately.

It inserts the 10 pages into the first document then debugs itself when
trying to edit and save the next file.

Or am I doing something wrong?

Thanks

Stuart
 
J

Jay Freedman

Well, let's try a variation on the theme.

'---------------------------------
Sub InsertintoAllDocumentsInaFolder()

Dim MyPath As String
Dim MyName As String
Dim Mydoc As Document
Dim MyRange As Range

'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

'get files from the selected path
'and insert them into the doc
MyName = Dir$(MyPath & "*.*")
Do While MyName <> ""
Set Mydoc = Documents.Open(MyName)
Set MyRange = Mydoc.Range
MyRange.Collapse wdCollapseStart
MyRange.InsertFile FileName:="C:\folder\filename.doc"
Mydoc.Save
Mydoc.Close
MyName = Dir
Loop

End Sub
'---------------------------------

The difference is in using a Range instead of the Selection. Because
the Selection refers to the location of the cursor at the time,
opening and closing a series of documents could cause some confusion.

If you still run into the same behavior, also let us know which line
of the macro is highlighted when the debugger stops it, and the exact
text of any error message you see.

--
Regards,
Jay Freedman
Microsoft Word MVP
Email cannot be acknowledged; please post all follow-ups to the
newsgroup so all may benefit.
 
S

Stuart

Hi Jay,

Unfortunately it didnt work again,

The error message

Run time error '5174'
This file could not be found.
Try on or more of the following.
*Check the spelling of the name of the document.
*Try a different file name.
(Test File2.doc)

Hope you can help,

Kind Regards,

Stuart
 
J

Jay Freedman

Hi Stuart,

That message is a lot clearer than many you'll encounter in VBA. :) I'm
assuming that the highlight is in the line of code

MyRange.InsertFile FileName:="C:\folder\filename.doc"

Whatever you put inside the quotes (I guess from the error message that it
says "Test File2.doc") doesn't match any existing file. If you included a
full path inside the quotes, check the path for spelling errors, missing or
extra spaces, etc. If you didn't include a full path, do that -- the system
is probably looking in the wrong folder.

--
Regards,
Jay Freedman
Microsoft Word MVP
Email cannot be acknowledged; please post all follow-ups to the newsgroup so
all may benefit.
 
S

Stuart

Hi Jay,

The insertion of the pages works on the first file in the folder but
then that runtime error appears when trying to do the process on the
second file.

Hope this clarifies everything,

Thanks

Stuart
 
D

Doug Robbins - Word MVP

Exactly what line of code is highlighted when you click on debug after the
error occurs?

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

Stuart

Hi Doug,

it is debugging on this line,

MyRange.InsertFile FileName:="D:\test\File to insert.doc"

This is the file that I want to insert into the other documents.

It doesnt even work on a file at all now,

Should I post my posting elsewhere see if someone else may be able to
help?

Thanks for your time ,

Stuart
 
D

Doug Robbins - Word MVP

Well, when I set up a folder with some files to try this on, it bombed out
on the line

Set Mydoc = Documents.Open(MyName)

which seems to be because Word is getting confused about where the files
are. The following modified code overcame that problem however

Dim MyPath As String
Dim MyName As String
Dim Mydoc As Document
Dim MyRange As Range

'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

'get files from the selected path
'and insert them into the doc
MyName = Dir$(MyPath & "*.*")
Do While MyName <> ""
Set Mydoc = Documents.Open(MyPath & MyName)
Set MyRange = Mydoc.Range
MyRange.Collapse wdCollapseStart
MyRange.InsertFile FileName:="C:\New Folder\Text to insert.docx"
Mydoc.SaveAs MyName
Mydoc.Close
MyName = Dir
Loop


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

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