Concatenate Word documents WITHOUT using the clipboard.

Is there a way to concatenate Word documents without using Copy and Paste using the Word Object Model?

At the moment I just select the Whole Story in one document:

        .Selection.WholeStory
        .Selection.Copy


and then activate the other document and paste ...

        .Selection.PasteAndFormat &H10 '&H10 = wdFormatOriginalFormatting


And I suspect that not only this is very SLOW, but that the Office Clipboard is causing problems

I would much rather NOT use the clipboard at all.
LVL 4
WernerVonBraunAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

GrahamSkanRetiredCommented:
You can use the InsertFile method
    ActiveDocument.Bookmarks("\EndOfDoc").Range.InsertFile "C:\MyFolder\MyFile.doc"

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
WernerVonBraunAuthor Commented:
Ugh.

Ok. That works BUT it makes a dog's dinner of the document's formatting. Is there a way I concatenate Doc B to Doc A and it still looks recognisably like Doc A?
0
WernerVonBraunAuthor Commented:
sorry, it still looks recognisably like Doc B.
0
The Ultimate Tool Kit for Technolgy Solution Provi

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy for valuable how-to assets including sample agreements, checklists, flowcharts, and more!

LeithauserCommented:
Could you show your whole code, including declarations and such, just to make it clearer to us?
0
WernerVonBraunAuthor Commented:
Well, I've since found out that it wasn't the clipboard that was causing the problem. But I have not yet been able to establish what the real cause of my original problem was. I was creating a huge word document by gathering several word documents together and concatenating them all. Occasionally, one of those word documents seemed to keep on being pasted all over the final document, for no apparent reason.

I asked this question, thinking that avoiding the clipboard would solve my problem. But then I noticed something peculiar. Some of the word documents I am concatenating are created from what were originally PDF or image files. Basically what I do is I use ghostscript to output those original documents to BITMAPS, and then I create a word document that holds each page of the original document as a bitmap.

What I found was that the offending document that kept on getting pasted in all over the place seemed to take the place of other documents that were similarly produced and that happened to have the SAME number of pages. Now note that I create the bitmaps by basically naming them note*.bmp, so a four page PDF would be rendered as note1.bmp, note2.bmp, note3.bmp and note4.bmp

This is when I started smelling a rat. I reckon what is happening is that Word, in order to avoid processing power, keeps internal "handles" saying "this Shape object represents an image that was located at %TEMP%\note1.bmp" and so on, even though I tell it to embed the objects. So when it is asked to open a document with four pages containing bitmaps note1.bmp to note4.bmp it goes "hey, I've already got those pages".

OR something of that nature. I don't know. In any case, I resolved it by making it generate bitmaps that aren't called note1.bmp etc, but note[nameoforiginaldocument]1.bmp etc.

Another possibility for what was causing the problem was due to how I handled appending new documents to the end of the document. I would navigate to the end of the document by going to its page number, and then navigate to the end of the page. I reckon that maybe sometimes it gets a bit confused as to where, exactly, it is, and that some of the problems I was experiencing were caused by me not pasting the new documents right at the end of the combined one, but a few characters earlier - for whatever reason.

I never knew that there was a standard bookmark named "\EndOfDoc" that could take you right to the end of the document without fail, and I must thank GrahamSkan for simply making me aware of this with his comment.

Another change I made was that I save the document every time I append another document to it. Now I do get the odd problem with Word claiming it has run out of memory, but I found that if I simply terminate all word processes at that stage and re-open the documents, the problem goes away. Everybody raise your hands and sing "MEMORY LEAK". Anyway, I re-wrote some of my code so it handled this and carried on. It has slowed down the process a little but not so badly that it's become unacceptable.


Leithauser,

The code really is straightforward. I create a Word. Application, and a new Word.Document by invoking Documents.Add to this. I then open a second Word.Document by invoking the .Open method. Once the existing Word.Document is Active, I do

        .Selection.WholeStory
        .Selection.Copy

on the Word.Application object. I then go back to the new word document and, again on the Word.Application object I do


0
WernerVonBraunAuthor Commented:
eek. I managed to submit before I was finished.

on the word.application object I do

 .Selection.PasteAndFormat &H10 '&H10 = wdFormatOriginalFormatting

and that copies the existing document into the new document.


Anyway. I'm sure that the suggestion made helped me a little to solve my problem so points all around.
0
WernerVonBraunAuthor Commented:
thanks
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic Classic

From novice to tech pro — start learning today.