PDA

View Full Version : Merging .dot from VBA



SpookyAwol
02-22-2010, 06:36 PM
Hi Guys, Ive had a search around but not sure how this is done.

A quick outline:

I have a word VBA application where between 1 and 10 .dot templates are used depending on the user selection. Its an application used to create a file about an event. Some templates can be used multiple times within the same application thus creating 10-20 actual output documents.

So once the user has imputed the information and pressed the submit button, all of the output .docs are created. This of course fills up the taskbar with many docs and I feel creates a performance issue if too many are created

Code

Currently I loop to create a new document for each unique record like so :


For x = 0 To v
Documents.Add Template:=strFileName + "template.dot"
With ActiveDocument
.Bookmarks("blah").Range.InsertBefore values
End With
Next x

Question

Is it possible to merge the 'x' documents created by this code to output a single page document rather than outputting them as many single page documents?

Thanks in advance!

fumei
02-23-2010, 12:36 PM
Sure.Dim FinalDoc As Document
Dim TempDoc As Document
Dim r As Range

' here is the "merged" final doc
Set FinalDoc = Documents.Add Template:=strFileName & "template.dot"
Set r = FinalDoc.Range
For x = 0 To v
Set TempDoc = Documents.Add Template:=strFileName & "template.dot"
With TempDoc
.Bookmarks("blah").Range.InsertBefore values
.Range.Copy
With r
.Collapse 0
.Paste
End With
.Close wdDoNotSaveChanges
End With
Set TempDoc = Nothing
Set r = FinalDoc.Range
Next x
What this does:

1.. makes two document objects - one for the final doc, another as Temp

2. makes Range object of the FinalDoc

For x = 0 To v

3. make a TempDoc object using the same template

4. do your stuff

5. copy the range of the TempDoc

6. paste that to the end of the FinalDoc

7. close the TempDoc

8. destroy the TempDoc object

9. reset the range object for FinalDoc

Repeat.

SpookyAwol
02-23-2010, 04:18 PM
Thanks for the response, that kind of does what I need.

The existing templates use headers and footers, and it seems that those are ignored though when using the copy/past command. I need to be able to duplicate the complete document?

fumei
02-24-2010, 11:15 AM
Huh? If the final doc is using the same template, will it not have the headers and footers? Please explain more.

SpookyAwol
02-24-2010, 01:11 PM
Not sure of the way to explain it easily, but Ill try :)
It obviously works well as it is, I just want to reduce the number of documents created and make the (current) several documents created from the one template into one document (as when printed I can separate into individual documents)

We need to use default company templates which have a header and footer.

The UserForm I am using creates a file of documents.
So depending on the user selection, there may be 1, 2 or 3 documents created from one template. Each document uses the full template

When I use my existing code, it creates one document as required for each selection.

Each document contains different bookmarks in both the header body and footer and are currently created as separate word documents. Each unique document needs to retain the header and footer

So its not a case of looping to add more text to one template, its a requirement to loop to create more than one full document. Each based on the template. The only difference is that each new page needs to be added to the other.

Did that kinda make sense? :)

SpookyAwol
02-24-2010, 01:21 PM
Currently it creates this:

Doc1.doc

------------
header
------------

------------
Footer
------------

Doc2.doc

------------
header
------------

------------
Footer
------------

And what I would like to do is :

Doc1.doc

------------
header
------------

------------
Footer
------------

{page break}

------------
header
------------

------------
Footer
------------

fumei
02-24-2010, 01:47 PM
As stated, the header/footer comes from the template, so the header/footer for the combined doc (which uses that template) should have the header/footer.

I would not do a page break. I would use a SECTION break between the "document" chunks.

You can NOT have multiple bookmarks with the same name in one document. Bookmarks MUST be unique.

SpookyAwol
02-24-2010, 01:59 PM
Hmm, good point. I guess it is more about what text I should be copy / pasting as the only change I need for each document is the bookmarked items. And as you say, they must be unique.
I guess that was where I was coming from originally - to be able to just 'add' or merge the 2+ unique documents into one document to avoid those problems

fumei
02-24-2010, 02:06 PM
Have you tried using the code I posted? If it did not work, what was wrong?

SpookyAwol
02-24-2010, 02:24 PM
Have you tried using the code I posted? If it did not work, what was wrong?

For sure I tried that thanks.

This is using a simple test file that uses one bookmark (persons name) in the header.

What happened was that the default text (outside of the header and footer) was cut and pasted into the end of the first document at the last paragraph and the 1st bookmark was not populated. The bookmark on the second page was (as obviously they are the same name)

So in reality for this to work, it needs to create a second (or more) page and just populate each pages book marks only

fumei
02-25-2010, 09:45 AM
"So in reality for this to work, it needs to create a second (or more) page and just populate each pages book marks only"

But it can not do this if the bookmark has the same name.

SpookyAwol
02-25-2010, 11:12 AM
Yes, and therein lies the problem :) Perhaps there is another way to go about it?

fumei
02-25-2010, 11:20 AM
Please explain what it is you are doing with the bookmarks. For example, if you using Bookmark("Whatever").Range.Text = "yaddayadda", then this deletes the bookmark. Therefore if it not longer exists (which it would not), then the fact multiple bookmarks started out with the same name becomes irrelevant.

SpookyAwol
02-25-2010, 03:58 PM
Please explain what it is you are doing with the bookmarks. For example, if you using Bookmark("Whatever").Range.Text = "yaddayadda", then this deletes the bookmark. Therefore if it not longer exists (which it would not), then the fact multiple bookmarks started out with the same name becomes irrelevant.

In its simplest form, each loop of code that creates one document, inserts a persons name at the bookmark.

If x = 0 then only one document is created.
If x = 1 (or more) then a new document is created for each x, each with a unique persons name.
So 'x' documents will all be the same except for the unique name in the header of the page.

Thats a simplification - some other templates in use would have many more unique bookmarks within the same document.

Do .variables need to be unique the same as bookmarks? or is it possible to loop using the same docvariable but using a .Fields.Update at the end of each template?

Forgive me if my understanding is not great, I used to code purely in vbscript and know not a lot about word and vba :)

fumei
03-01-2010, 02:01 PM
"Do .variables need to be unique the same as bookmarks? "

A DOCVARIABLE is a single thing. It is unique.

"inserts a persons name at the bookmark." Again, please post your exact code. Unless you are explicitly doing otherwise, "inserting" the text at a bookmark deletes the bookmark.

ActiveDocument.Bookmarks("Here").Range.Text = "yadda"

does indeed insert "yadda" at the bookmark "Here", but the bookmark is deleted by the process. There is simple code that gets around this, but unless you are using it, the bookmark is deleted.

So, if Doc_A is created from a template, and text inserted at bookmark "Here", then THAT bookmark is deleted. A new document cloned from the same template has the same thing happen.

Thus, there is NO bookmark, and the documents can be appended.