[Webinar] Streamline your web hosting managementRegister Today

x
?
Solved

Concatenate Word documents WITHOUT using the clipboard.

Posted on 2010-04-08
7
Medium Priority
?
852 Views
Last Modified: 2012-05-09
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.
0
Comment
Question by:WernerVonBraun
  • 5
7 Comments
 
LVL 77

Accepted Solution

by:
GrahamSkan earned 1600 total points
ID: 30123866
You can use the InsertFile method
    ActiveDocument.Bookmarks("\EndOfDoc").Range.InsertFile "C:\MyFolder\MyFile.doc"

Open in new window

0
 
LVL 4

Author Comment

by:WernerVonBraun
ID: 30134881
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
 
LVL 4

Author Comment

by:WernerVonBraun
ID: 30134898
sorry, it still looks recognisably like Doc B.
0
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
LVL 5

Assisted Solution

by:Leithauser
Leithauser earned 400 total points
ID: 30209459
Could you show your whole code, including declarations and such, just to make it clearer to us?
0
 
LVL 4

Author Comment

by:WernerVonBraun
ID: 30212059
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
 
LVL 4

Author Comment

by:WernerVonBraun
ID: 30212175
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
 
LVL 4

Author Closing Comment

by:WernerVonBraun
ID: 31712363
thanks
0

Featured Post

The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I’ve seen a number of people looking for examples of how to access web services from VB6.  I’ve been using a test harness I built in VB6 (using many resources I found online) that I use for small projects to work out how to communicate with web serv…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Suggested Courses
Course of the Month7 days, 22 hours left to enroll

607 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question