Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Appending or merging word docs via VBA - Help Plz!

Status
Not open for further replies.

Anna007

Programmer
Nov 29, 2004
38
CA
Hi,

I have 2 documents in word that I populate via vba (bookmarking) from Excel. But I need to append/merge them together at the end, when completed. This way, it's easier for the user to look for 1 final file instead of 2. How do I do this??

Thanx,
Anna
 
Have you tried the macro recorder? You can insert a file with the Insert menu. Try recording going to the end of file 1, and then inserting file 2. You can then see the code.

that being said, are both files open? Do you want to just append them?

Gerry
 
Hi,

I do this via code actually.. need it automated.
So, I have FileA that I populate and then close it.
Then I have FileB that I populate and close afterwards.
At the end, I need to save these and I ask the user for a SaveAsName. So, I need to append them or think of a way to do this.
I like to make it simple for the user so that they enter one name for SaveAsName and then these 2 get appended at the end, if possible.

Thanx,
Anita
 
OK, it seems a little weird.

File A is "populated" (and I am not going there for now) and is CLOSED.

File B is "populated and CLOSED.

"At the end" - sorry - explain that to me? At the end of what? You have closed both files. You do not state if is there is another file open. Is Word loaded with NO document open, a blank screen, as both files are closed?

This is why I asked if both file are open. This is DESIGN.

BETTER:
File A is open, and being populated. When it is completed, it disappears from the user. It is not closed. However, if you want to save it as a separate file before you append, that is not a problem. But it is gone as far as the user is concerned.

File B is open, and is populated. When it is completed, you can save it as a separate file it that is a requirement.

Both files are available (NOT closed). Grab the contents of File B, copy it to the end of File A, display an input box asking for a FileSaveAs name....do a FileSaveAs for the appended file.

Close File B.

Done.

Again, this is a DESIGN issue. Unless you have a particular reason for closing each file.....if so, please tell me what it may be.

Gerry
 
Oh thank you Gerry for your help and you are right. I was originally closing the files bc the requirement was that they were to be saved differently under different names but then this has changed and this is why I need to append them at the end.
So, no.. I don't need to close them since I need to append them at the end.
So, at the end .. File A and File B are both open and populated.. how do I grab the contents of the File A to append to File B?
I don't know how to do this via VBA code.. is there an append method.. I don't think so.. is it the insert method? but how? This is where I am stuck..
I tried to merge them together but ran into problems..

Note: at the end before appending them, I'll have 4 files open that Word is working with.. the 2 original documents that were bookmarked and 2 files that Word opens to populate (the copies of the original files).

Thanx so much for your help,
Anna
 
OK. it is probably better to make your documents in explicit objects.

So.

Code:
Sub AppendDocs()
Dim ThisDoc As Document
Dim ThatDoc As Document

' you have file A open
' make it an object
Set ThisDoc = ActiveDocument

' ...do what ever else it is you 
' .. are you doing, then save it
' it is still File A
ThisDoc.Save wdSaveChanges

' open file B
Documents.Open Filename:=[i]file B[/i]
' make IT an object

Set ThatDoc = ActiveDocument

' now you can use ThatDoc.whatever
' instead of ActiveDocument.whatever
' it is just shorter code
' do whatever you are doing with file B
' then save File B, as itself
ThatDoc.Save wdSaveChanges

' now grab the whole of File B (ThatDoc)
' and copy it
ThatDoc.Content.Copy

' activate File A again (ThisDoc)
      ThisDoc.Activate

' go to the end of the document
' and just in case, add a paragraph mark
      Selection.EndKey unit:=wdStory
      Selection.TypeParagraph

' and then paste the contents of File B
' File B is now appended to File A
      Selection.Paste

' now save the appended file using FileSaveAs
      ThisDoc.SaveAs FileName:="AppendedA_B.doc"

' destroy (release) the document objects
      Set ThatDoc = Nothing
      Set ThisDoc = Nothing
End Sub

You may also want to add a Close instructions as well, as you seem to finished.

Gerry
 
Thanx so much for your help.. I understand what you are trying to do and tried it.. but getting an error when I try to do this:

Selection.EndKey unit:=wdStory
Selection.TypeParagraph

This is not working.. what would be the code to insert a page in btwn and then paste it?

Thanx so much,
Anna
 
Hi Anna. Uh..."not working" is a bit vague. I do not know what that means.

Are you getting an error message?
If yes, please state what it is.

Is it doing something else weird?
If yes, what is it doing?

Is nothing happening at all - "not working"
If yes, please state that it does not go to the end of the file, does not add a paragraph, and it does not append the other file.

Or are you saying "not working" means the other file is not appended?

Sorry, but as I can not really look over your shoulder, you are going to have to actually tell me what is, or "not", happening.

Please post exactly what "not working" means.

Gerry
 
Hi,

Sorry for being vague.. I meant it's giving me an error when it gets to this line below and won't continue (after I activated the fileB:
Selection.EndKey unit:=wdStory

The error message is something like this is not supported .. so, then I checked the directory where the files are and the 2 files are just open with this format, as usual.. ~FileA and ~FileB.. since it couldn't get to the point where I set them to nothing...

I can't check the details of the error message now since not by the laptop. I need to figure this out soon though.. very soon!

Not sure why.. hope this helps a little more.

Thanx soooo much,
Anna
 
Wait a second. the Selection.EndKey is not actioning on FileB, it should be actioning on FileA.

' [COLOR=red yellow]activate File A again (ThisDoc)[/color red yellow]
ThisDoc.Activate

' go to the end of the document
' and just in case, add a paragraph mark
' [COLOR=red yellow]the following should be going to the end if FileA, adding a paragraph[/color red yellow]
Selection.EndKey unit:=wdStory
Selection.TypeParagraph

Try commenting this out. Does the rest of the code copy in (append) FileB to FileA?

I am still not clear on what is happening. Are both files being opened? Is whatever else you are doing functioning correctly?




Gerry
 
I have been having trouble with this but I'm having a work-around for now. Thank you, anyways.
What I really need to do now is this:

My doc is book-marked and I'm trying to do the following from Excel:

Mydoc2.Bookmarks("Equip_TotalPrice").Range.Text = Worksheets("Price").Range("ProposedEqptSellingPrice").Value

It keeps giving me an error for some reason!!
I want to get the value of this range mentioned above and put it into the Book-marked field of the word doc.

Thanx,
Anna
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top