Append a document onto an existing document

H

HeatherO

I was just wondering I have a document that I have manipulated in a macro and
now I want to open another document and then append the new document onto the
original. Kind of like a mail merge I guess but manually since they are 2
different documents and I might need to append others onto the document. I
know in excel there are sheets but is there such a thing in word?
Any suggestions are appreciated.
TIA
Heather
 
J

Jean-Guy Marcil

HeatherO was telling us:
HeatherO nous racontait que :
I was just wondering I have a document that I have manipulated in a
macro and now I want to open another document and then append the new
document onto the original. Kind of like a mail merge I guess but
manually since they are 2 different documents and I might need to
append others onto the document. I know in excel there are sheets
but is there such a thing in word?
Any suggestions are appreciated.

Look up the INCLUDETEXT field... It might be what you need.

Otherwise, insert a new page section break, then open the second document,
select all the content (or use a range object), copy it, then paste it after
the section break, close the second document. In later version of Word, you
can open the second one "invisible", so it runs faster and the users will
not see it.

--
Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
(e-mail address removed)
Word MVP site: http://www.word.mvps.org
 
H

HeatherO

This can be done in a macro running behind the scenes? Sorry for sounding so
novice but I'm not really familiar with the word commands in a macro that can
cut and paste or even do a section break. I'm not a microsoft programmer
just trying to learn so I see the codes like wd... and INCLUDETEXT and stuff
and I am also not really understanding the ranges in a word document. Like a
range when you state a range does it go from row 1 col1 on the page to
another row col is that how word splits up the document. Sorry this word
programming is very hard, or maybe I'm just not getting it. Anyways can you
show me what code would copy or paste or create a section break?? Thanks
though for your response I'll try to see what I can do.
Heather
 
J

Jean-Guy Marcil

HeatherO was telling us:
HeatherO nous racontait que :
This can be done in a macro running behind the scenes? Sorry for
sounding so novice but I'm not really familiar with the word commands
in a macro that can cut and paste or even do a section break. I'm
not a microsoft programmer just trying to learn so I see the codes
like wd... and INCLUDETEXT and stuff and I am also not really
understanding the ranges in a word document. Like a range when you

A range is simply a "stretch" of the document, from a given character to
another one.
state a range does it go from row 1 col1 on the page to another row
col is that how word splits up the document. Sorry this word
programming is very hard, or maybe I'm just not getting it. Anyways
can you show me what code would copy or paste or create a section
break?? Thanks though for your response I'll try to see what I can
do.

Here is a little someting to get you going:

'_______________________________________
Dim SourceDoc As Document
Dim TargetDoc As Document

Dim SourceRge As Range
Dim TargetRge As Range

Set SourceDoc = ActiveDocument
Set TargetDoc = Documents.Open(FileName:="X:\test.doc", Visible:=False)

Set SourceRge = SourceDoc.Range
Set TargetRge = TargetDoc.Content
TargetRge.Copy

With SourceRge
.Collapse wdCollapseEnd
.InsertBreak wdSectionBreakNextPage
.Collapse wdCollapseEnd
End With

SourceRge.Paste

TargetDoc.Close

Set SourceDoc = Nothing
Set TargetDoc = Nothing
Set SourceRge = Nothing
Set TargetRge = Nothing
'_______________________________________

--
Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
(e-mail address removed)
Word MVP site: http://www.word.mvps.org
 
H

HeatherO

Hi again, it's me.
Ok so I have been trying that and it is not working. Here's what I think
my problem is the targetdoc does not actually exist at first. What I am
basically doing is running a macro that loads data into tables and at
specific bookmarks and then what I want to do is copy the document with all
the data on it into a new fresh document and then as I go through the data it
will update the original document of which I just want to copy and paste the
updated document into a new document and append it after the other one. Kind
of like a mail merge but done by my macro. So I think it's because I don't
have the actual document created at first so this is what I have done to the
code which it does not seem to like at all since it creates document2 not the
save as that I specify here's my code so far:

Sub copy_doc(counter)
Dim SourceRge As Range
Dim TargetRge As Range
Dim targetDoc As Document
Dim sourcedoc As Document

'at the beginning of the data then open new document to save updated forms
If counter = 2 Then
Documents.Add.SaveAs FileName:="C\Model Pilot\testZ.doc"
Documents("C:\Model Pilot\testZ.doc").Close
End If

Set sourcedoc = ActiveDocument
Set targetDoc = Documents.Open(FileName:="C:\Model Pilot\testZ.doc",
Visible:=True)

Set SourceRge = sourcedoc.Range
Set TargetRge = targetDoc.Content
TargetRge.Copy

With SourceRge
.Collapse wdCollapseEnd
.InsertBreak wdSectionBreakNextPage
.Collapse wdCollapseEnd
End With

SourceRge.Paste

targetDoc.Close

Set sourcedoc = Nothing
Set targetDoc = Nothing
Set SourceRge = Nothing
Set TargetRge = Nothing
End Sub

I'm sorry but I think I just want it to work so badly and am at a complete
loss of understanding. I've looked even at other people's post which
actually just got me alot more confused since I don't know what pTracetodopy
or casetracemerge. I think after this I will just stick to simpler things.
Any help is again appreciated.
Heather
 
J

Jean-Guy Marcil

HeatherO was telling us:
HeatherO nous racontait que :
Hi again, it's me.
Ok so I have been trying that and it is not working. Here's what I
think my problem is the targetdoc does not actually exist at first.
What I am basically doing is running a macro that loads data into
tables and at specific bookmarks and then what I want to do is copy
the document with all the data on it into a new fresh document and
then as I go through the data it will update the original document of
which I just want to copy and paste the updated document into a new
document and append it after the other one. Kind of like a mail
merge but done by my macro. So I think it's because I don't have the
actual document created at first so this is what I have done to the
code which it does not seem to like at all since it creates document2
not the save as that I specify here's my code so far:

Sorry, I don't quite get what you are trying to do.

But in your code:
Sub copy_doc(counter)
Dim SourceRge As Range
Dim TargetRge As Range
Dim targetDoc As Document
Dim sourcedoc As Document

'at the beginning of the data then open new document to save updated
forms If counter = 2 Then

This creates a new document and saves it right away with the name
"testZ.doc" (By the way, you are missing the ":" after the "C"
Documents.Add.SaveAs FileName:="C\Model Pilot\testZ.doc"
Documents("C:\Model Pilot\testZ.doc").Close
End If

Set sourcedoc = ActiveDocument
Set targetDoc = Documents.Open(FileName:="C:\Model Pilot\testZ.doc",
Visible:=True)

The above could all be replaced by
Set sourcedoc = ActiveDocument
Set targetDoc = Documents.Add
targetDoc.SaveAs FileName:="C:\Model Pilot\testZ.doc"

Then fill target doc with the info you want, then you can copy its content
in the other doc.

--
Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
(e-mail address removed)
Word MVP site: http://www.word.mvps.org
 
H

HeatherO

Ok, that was pretty simple. I don't doubt that I mispelled the path. I had
another spelling error that caused me all kinds of trouble too. Just one
more question, what I am trying to do is kind of like a mail merge except
that it's on an account basis so there might be more then a few things that I
have to enter into a table that's why I've been creating this macro and have
to append documents after each other because there are 2 different documents
one french and one english. What I also wanted to do is if it's english get
the document fill in the necessary info then copy it to another document and
the next customer is french grab the french document and fill it in and
append it to the english document so they all print as one with the info on
it. However I think since my macro only resides in the english that I might
need to make the macro a little more global.
Thanks for your help
Heather
 
H

HeatherO

Hi Jean-Guy,
I tried your suggestions and it creates the other document but it does not
copy any of the words or tables from the source document. So let me try to
explain myself better.
I have document A on document A I have the date: client account: address:
then I have a table containing data and then a second table containing data
and then I have a underline with the client's name underneath. (The date,
account address and data in the table I am grabbing from an excel file).
What I am trying to do is based on the client's account create this source
document. When I am finished client A's account form I want to open the
testZ document and copy the source document to testZ with all of client A's
information.
Then I am going to get client B and dump his info into the source document
and then copy the source document back to testZ document after client A's
information so when I print it finally it will print all the information. So
in a way it's kind of like a mail merge. Right now I get all the data dumped
on top of each other in the source document and I haven't even tested it to
see what it prints. Any suggestions??
TIA
Heather
 
J

Jean-Guy Marcil

HeatherO was telling us:
HeatherO nous racontait que :
Hi Jean-Guy,
I tried your suggestions and it creates the other document but it
does not copy any of the words or tables from the source document.
So let me try to explain myself better.
I have document A on document A I have the date: client account:
address: then I have a table containing data and then a second table
containing data and then I have a underline with the client's name
underneath. (The date, account address and data in the table I am
grabbing from an excel file). What I am trying to do is based on the
client's account create this source document. When I am finished
client A's account form I want to open the testZ document and copy
the source document to testZ with all of client A's information.
Then I am going to get client B and dump his info into the source
document and then copy the source document back to testZ document
after client A's information so when I print it finally it will print
all the information. So in a way it's kind of like a mail merge.
Right now I get all the data dumped on top of each other in the
source document and I haven't even tested it to see what it prints.
Any suggestions??

Heather, is English your mother tongue?
If not, then you need to learn to write short simple sentences that are not
full of irrelevant details. Also, you need to organize your thoughts so that
they make sense to your reader.

If it is, you need to brush up on writing techniques.

Please, read your paragraph again as if you were doing so for the first time
(as it was the case for me):
I have document A on document A I have the date: client account:
address: then I have a table containing data and then a second table
containing data and then I have a underline with the client's name
underneath. (The date, account address and data in the table I am

Finally, end of the first sentence.
It is difficult to figure out what you are talking about. "I have document A
on document A..." ???
Is the content of Document A relevant to the problem you are trying to
describe? As I found out later, after trying to understand what this content
was, it is absolutely not relevant. Then don't mention it!
grabbing from an excel file). What I am trying to do is based on the
client's account create this source document. When I am finished

Here it seems that document A is what you call the "source document". If you
are building it on the fly from Excel, then it is hardly a source document.
Before the macro started, it did not exist. It seems to be more like some
kind of temporary document.
client A's account form I want to open the testZ document and copy
the source document to testZ with all of client A's information.

I guess that testZ already exists? What does it contain? Or is it a blank
document you are also creating on the fly?
Then I am going to get client B and dump his info into the source
document and then copy the source document back to testZ document

What source document? Document A from before? Or another new document you
created on the fly?
after client A's information so when I print it finally it will print
all the information. So in a way it's kind of like a mail merge.
Right now I get all the data dumped on top of each other in the
source document and I haven't even tested it to see what it prints.

So, if I understand correctly, you build a temporary document and dump the
data from this temp document into a final document. Then you build another
temporary document and append this document to the same final document. And
son for x numbers of temporary document. Right?

Then the code I provided earlier will work. I think you just have to sit
down, relax and figure out which document is which.
Do you need to keep those temporary documents (Document A from =your text)?
If not, it is even easier. Build it from a template, append its range to the
final document and discard it. Create a new one, append it to the final
document before discarding it, etc.:

'_______________________________________
Dim TempDoc As Document '"Document A"
Dim TargetDoc As Document 'testZ

Dim TempRge As Range
Dim TargetRge As Range

'Start some kind of loop to cycle through your Excel data chunks
Set TargetDoc = Documents("testZ.doc") 'Already opened by some earlier code
Set TargetRge = TargetDoc.Content

Set TempDoc = Documents.Add("C:\MyTemplate.dot")
'Fill TempDoc with its data...

Set TempRge = SourceDoc.Range
TempRge.Copy

'Add some code to handle the first insertion so as to
'avoid a section break before the first data chunk
With TargetRge
.Collapse wdCollapseEnd
.InsertBreak wdSectionBreakNextPage
.Collapse wdCollapseEnd
End With

TargetRge.Paste

TempDoc.Close wdDoNotSaveChanges
'loop

Set TempDoc = Nothing
Set TargetDoc = Nothing
Set TempRge = Nothing
Set TargetRge = Nothing
'_______________________________________

Please, read what Jezebel had to say in response to your later post in this
group.
--
Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
(e-mail address removed)
Word MVP site: http://www.word.mvps.org
 
H

HeatherO

Jean-Guy, thanks for your contructive comments. I'm glad it made you feel
better to attack me with comments like "is english my mother tongue". I have
no use for this kind of help. I believe someone once said "there are no dumb
questions". However I am obviously at the wrong place for that.
Heather
 
J

Jean-Guy Marcil

HeatherO was telling us:
HeatherO nous racontait que :
Jean-Guy, thanks for your contructive comments. I'm glad it made you
feel better to attack me with comments like "is english my mother
tongue". I have no use for this kind of help. I believe someone
once said "there are no dumb questions". However I am obviously at
the wrong place for that.
Heather

Sorry you took it the wrong way. How/where did you get the impression It
made me feel better to write what I did? Believe me, if I felt I needed to
relieve myself in some way, I would have been way more aggressive and would
not have even tried to help. In fact, my whole post was about helping. I
tried to show you why it is important to consider your reader in a forum
such as this one, and then tried to provide help regarding your problem.

I tried to be constructive, no just negative. I did not assume anything
about your mother tongue. Read my post again. I did write that it may have
been one way or the other. I did not imply that it was, but tried to attack
you by writing that it may not have been, thus belittling you. In these
newsgroups we get people from all over the world, and they often use aliases
so we cannot trust the name alone, and for a lot of them English is a second
language (as it is for me), so it is not uncalled for to question whether
English is your mother tongue or not. Also, since you posted from a web
interface, I could not infer where you were from.
I did the best I could. I tried to show you why I thought what I wrote by
commenting directly from what you wrote, not what I thought you meant or
what I thought it was worth. The only time I wrote about what I thought what
your post meant was when I was actually trying to really help you with your
problem by restating your case in my own words, hoping you would confirm if
I got it right or not.

So, I am sorry you did not like my comments regarding your writing style,
but what I wrote remains true. What you wrote was difficult to follow and
made it more difficult to help you. I hope you read the whole post because I
did not just "attack" you as you seem to be thinking I did.
I did provide help and code sample and never inferred that your question was
too dumb to be asked. Please, show me where I wrote that your question was
dumb, and so was not worthy of my time (which is what I think you were
inferring when you wrote " I am obviously at the wrong place for that").

Finally, I do not understand why you took offence at such a realistic
question given the nature of the medium through which we communicate, but
did not find the following comment offensive at all, considering the way you
reacted to my post, you should have reacted in a similar way to this one as
well (Note that I am in no way commenting on the
value/worthiness/quality/nature of the text I am quoting):
<quote>
If you leapt in and starting writing code with an objective as ill-defined
as the spiel you posted, then it's no wonder you and the client are at odds:
you're not clear on where you want to end up; they're not clear on what
<end quote>
You in fact praised it:
<quote>
Thanks for the constructive comments. I'll try to focus and it a little
better.
<end quote>

If you want to keep on writing in a way that makes it very difficult to
understand what it is you mean, you are right, you do not need help such as
the kind of help I tried to provide.

Should you need more help regarding coding, I will be happy to oblige, but
since you do not like comments about your writing style, I will disregard
this aspect of your posts and hopefully be able to provide the actual help
you need.

Good luck to you and your project.
--
Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
(e-mail address removed)
Word MVP site: http://www.word.mvps.org
 

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