merging word docs

M

Marcos

Hi,
I am using vb to walk through a word doc. when it encounters a tag it either
replaces the tag with content or opens up the document that the tag
references. In this new document, it looks for tags and continues the
process.

After processing a document, I would like to return the formatted, modified
content of the back to the calling function to be inserted into the word doc
that had the tag.

I am not sure how to pass the modified content back to the calling function.
I was thinking that I could copy the word doc's content to the clipboard
and paste it into the original document, but the content could become very
big.

Another option would be to save the modified doc, then the calling function
would open it and insert the contents where the tag was before. But I am not
sure how to do that.

This may be a bit confusing. Basically I have a recursive function that
walks through a word doc and at times may open another doc, process it and
return the contents for insertion into the first doc.

any help is appreciated.
-Marcos
 
C

Cindy M -WordMVP-

Hi Marcos,

PUtting aside the fact that we don't really know what these "tags" are...

As I see it, you have two choices. One would be what you described: save the
document (or an extract), then use the InsertFile method to pull it into the
other document.

More efficient might be to make a copy of the range in question directly into
the "tag" range. Word has the FormattedText property of the Range object. That
should transfer directly. Here's some sample, "pseudo" code to show you what I
mean

Dim doc1 as Word.Document
Dim doc2 as Word.Document
Dim rng1 as Word.Range
Dim rng2 as Word.Range

Set doc1 = activeDocument
Set rng1 = doc1.Range
rng1.Find.Execute "tag1"
Set doc2 = Documents.Open("tag1.doc")
Set rng2 = doc2.Range
rng1.FormattedRange = rng2.FormattedRange

I am using vb to walk through a word doc. when it encounters a tag it either
replaces the tag with content or opens up the document that the tag
references. In this new document, it looks for tags and continues the
process.

After processing a document, I would like to return the formatted, modified
content of the back to the calling function to be inserted into the word doc
that had the tag.

I am not sure how to pass the modified content back to the calling function.
I was thinking that I could copy the word doc's content to the clipboard
and paste it into the original document, but the content could become very
big.

Another option would be to save the modified doc, then the calling function
would open it and insert the contents where the tag was before. But I am not
sure how to do that.

This may be a bit confusing. Basically I have a recursive function that
walks through a word doc and at times may open another doc, process it and
return the contents for insertion into the first doc.

Cindy Meister
INTER-Solutions, Switzerland
http://homepage.swissonline.ch/cindymeister (last update Jun 8 2004)
http://www.word.mvps.org

This reply is posted in the Newsgroup; please post any follow question or
reply in the newsgroup and not by e-mail :)
 
M

Marcos

Thank you Cindy, this is exactly what I wanted. I tried it with the
InsertFile and that seems to work ok, but it gives me a paragraph tag before
the insert. maybe with the copying directly to the range that problem will
go away.

I have another question.
if the text I insert in the range causes the doc to go to the next page, I
want to insert some text at the top of the new page.
I was thinking that I can keep the current page count and after copying the
text into the range, look at the page count again. if it is different, undo
the copy into the range, insert a page break before my "tag" and then copy
the text back into the tag range
something like this:

intInitialPageCount = Selection.Information(wdNumberOfPagesInDocument)
Set doc1 = activeDocument
Set rng1 = doc1.Range
rng1.Find.Execute "tag1"
Set doc2 = Documents.Open("tag1.doc")
Set rng2 = doc2.Range
rng1.FormattedRange = rng2.FormattedRange
intCurrentPageCount = Selection.Information(wdNumberOfPagesInDocument)
If intCurrentPageCount > intInitialPageCount Then
'somehow undo the previous copy then
'somehow go to the start of the range then
rng1.InsertBreak (wdPageBreak)
'go to after the pagebreak
rng1.insertBefore ("this is at the start of a new page")
'somehow go to the text that the range was at before then do the copy
again
Set rng2 = doc2.Range
intInitialPageCount = intCurrentPageCount
End If

I am not sure if this is the right way to do this. The text at the top will
be different for each page.
Am I on the right track?
 
C

Cindy M -WordMVP-

Hi Marcos,
I have another question.
if the text I insert in the range causes the doc to go to the next page, I
want to insert some text at the top of the new page.
I was thinking that I can keep the current page count and after copying the
text into the range, look at the page count again. if it is different, undo
the copy into the range, insert a page break before my "tag" and then copy
the text back into the tag range
something like this:
I am not sure if this is the right way to do this. The text at the top will
be different for each page.
Am I on the right track?
I'd say yes, although I'd try to avoid using a manual page break. Rather, I'd
apply the "Page Break Before" character formatting to the paragraph you want
to start on the next page. Then you wouldn't have to "undo" anything, either
:)

Cindy Meister
INTER-Solutions, Switzerland
http://homepage.swissonline.ch/cindymeister (last update Jun 8 2004)
http://www.word.mvps.org

This reply is posted in the Newsgroup; please post any follow question or
reply in the newsgroup and not by e-mail :)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top