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 / Programming / November 2006

Tip: Looking for answers? Try searching our database.

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

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Stuart - 31 Oct 2006 18:05 GMT
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
Jay Freedman - 31 Oct 2006 19:09 GMT
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"

Signature

Regards,
Jay Freedman
Microsoft Word MVP        FAQ: http://word.mvps.org
Email cannot be acknowledged; please post all follow-ups to the newsgroup so
all may benefit.

> Hi There,
>
[quoted text clipped - 7 lines]
>
> Stuart
Stuart - 31 Oct 2006 19:55 GMT
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

> 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
[quoted text clipped - 21 lines]
> >
> > Stuart
Doug Robbins - Word MVP - 31 Oct 2006 20:21 GMT
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

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

> Hi Jay,
>
[quoted text clipped - 36 lines]
>> >
>> > Stuart
Jay Freedman - 31 Oct 2006 22:34 GMT
I couldn't have said it better myself. :-)

Signature

Regards,
Jay Freedman
Microsoft Word MVP        FAQ: http://word.mvps.org
Email cannot be acknowledged; please post all follow-ups to the newsgroup so
all may benefit.

> Sub InsertintoAllDocumentsInaFolder()
>
[quoted text clipped - 43 lines]
>>
>> Stuart
Doug Robbins - Word MVP - 01 Nov 2006 06:46 GMT
Thanks, Jay

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

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 couldn't have said it better myself. :-)
>
[quoted text clipped - 45 lines]
>>>
>>> Stuart
Stuart - 01 Nov 2006 22:04 GMT
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

> Thanks, Jay
>
[quoted text clipped - 64 lines]
> >>>
> >>> Stuart
Jay Freedman - 02 Nov 2006 01:43 GMT
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        FAQ: http://word.mvps.org
Email cannot be acknowledged; please post all follow-ups to the
newsgroup so all may benefit.

>Hi Doug and Jay
>
[quoted text clipped - 77 lines]
>> >>>
>> >>> Stuart
Stuart - 02 Nov 2006 12:17 GMT
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
> Well, let's try a variation on the theme.
>
[quoted text clipped - 132 lines]
> >> >>>
> >> >>> Stuart
Jay Freedman - 02 Nov 2006 17:36 GMT
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.

Signature

Regards,
Jay Freedman
Microsoft Word MVP        FAQ: http://word.mvps.org
Email cannot be acknowledged; please post all follow-ups to the newsgroup so
all may benefit.

> Hi Jay,
>
[quoted text clipped - 150 lines]
>>>>>>>
>>>>>>> Stuart
Stuart - 02 Nov 2006 18:16 GMT
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

> Hi Stuart,
>
[quoted text clipped - 170 lines]
> >>>>>>>
> >>>>>>> Stuart
Doug Robbins - Word MVP - 02 Nov 2006 20:02 GMT
Exactly what line of code is highlighted when you click on debug after the
error occurs?

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

> Hi Jay,
>
[quoted text clipped - 186 lines]
>> >>>>>>>
>> >>>>>>> Stuart
Stuart - 02 Nov 2006 21:05 GMT
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

> Exactly what line of code is highlighted when you click on debug after the
> error occurs?
[quoted text clipped - 197 lines]
> >> >>>>>>>
> >> >>>>>>> Stuart
Doug Robbins - Word MVP - 02 Nov 2006 22:35 GMT
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

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

> Hi Doug,
>
[quoted text clipped - 225 lines]
>> >> >>>>>>>
>> >> >>>>>>> Stuart

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.