Link to home
Start Free TrialLog in
Avatar of Member_2_1348041
Member_2_1348041Flag for Ireland

asked on

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.
ASKER CERTIFIED SOLUTION
Avatar of GrahamSkan
GrahamSkan
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Member_2_1348041

ASKER

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?
sorry, it still looks recognisably like Doc B.
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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


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