A soulation for loss the PageSetUp porperty after InsertFile or merge two files in Interp.Word

After so many times testing and coding, I find the soulation finally. Thanks God!

First, the reason of problem occur:

Reference :  http://word.mvps.org/FAQS/Formatting/WorkWithSections.htm

How Word sections work

Section breaks store the following information:
1. The Headers and Footers (and their properties) for the section.

2. The Page Setup for the section.

3. The Columns settings for the section.


When you delete a section break, or move an entire section to another part of the document, you get what seem to be very strange results. For instance, deleting a Continuous section break causes the preceding Next Page section break to convert to a Continuous one, or deleting a section break causes an important Header to disappear from the document, or causes the entire document to become landscape..

I agree it's confusing, but it's “by design”. These are the rules to remember:
1. A section break stores the formatting (page setup, header/footers etc.) of the preceding section.

2. The final paragraph of the document contains an invisible section break

3. When you delete a section break, the properties stored in the section break are deleted, and the text which formerly preceded the section break takes on the properties stored in the next section break.

4. A section break displays the “Continuous” or “Next Page” property of the following section!


So let's say you have 3 sections.
  
Section 1 has “Section Start Continuous” defined under Page Setup. The properties of section 1 are stored in the first section break. So the section break at the end of Section 1 stores the information “Section Start Continuous”.

  
Section 2 has “Section Start Next Page” defined under Page Setup. So the section break at the end of Section 1 displays the information “Section Break Next Page”. Meanwhile the section break at the end of section 2 stores the information “Section Start Next Page”.

  
Section 3 has “Section Start Continuous” defined under Page Setup. So the section break at the end of Section 2 displays the information “Section Break Continuous” and the invisible section break at the end of the document stores the information “Section Start Continuous”.


Now if you delete the second section break, the text which preceded it will take on the formatting of the next section (formerly Section 3, now Section 2), which has “Section Start Continuous” defined. So the first section break will now display “Section Break Continuous” whereas before it displayed “Section Break Next Page”.
Preserving section formatting when pasting between documents

The secret to preserving Headers and Footers, Next Page information, etc. when copying and pasting between documents is to temporarily add a section break at the end of the text you are going to paste or insert.  

Copy up to and including the temporary section break, which thus preserves the section formatting of the text preceding it. Now paste into the other document. Close the first document without saving.


  
If you really want to delete the temporary section break ...

Unfortunately, you can't then delete the “temporary” section break(s) from the document you pasted into, or you'll still lose the formatting. This can sometimes lead to a section break being the next-to-last character in the document, which can be awkward.

If you want to get rid of it, you first have to make sure that the section formatting of the final section is identical to that of the preceding one. To do this:

1. Go to the very end of the document, and go into the final section's header. If it's a continuous section break, you will first need to temporarily create a page break at the end of the document, so that you don't go into the previous section's Header.

2. Make sure that both the Header and Footer are set to “Same as Previous”. If they aren't, use the Header/Footer toolbar to set it to this. Then return to the main document.

3. Go to the penultimate section, select File + Page Setup... and press Return. (This makes Word “Remember” all the settings in the dialog).

4. Go to the final section and press F4 (repeat last command). This applies the “remembered” settings to the final section.

5. If there are differences in the column formatting between the two sections, you'll also need to use the F4 trick with the Format + Columns... dialog.

6. You can now safely delete the final section break (and the manual page break, if you inserted one).

Preserving section formatting when using Insert + File

The rules section breaks follow when using Insert + File are even more Alice-in-Wonderland than elsewhere; but the fix is straightforward: the files you plan to insert must contain a continuous section break at the start of the document, as well as at the end. (Alternatively, just stick to copy & paste, which works more logically).

If you're using Insert + File, Word inserts the saved version of the document, so you would need to save the file you're inserting, having inserted the extra section breaks, in order to have the temporary section breaks included when the file is inserted. You can, if you want, subsequently delete the temporary breaks and save the file again.

Here is some information that a source of mine at Microsoft found in the Office 2000 bug database regarding this
  
“We preserve the last section of the destination's section properties by copying them to the first section of the source. The workaround will work if the source document starts with a continuous section break. We can't fix this bug without breaking another scenario. I say we let it lie instead of reverting back to Word '95 behavior and breaking something else”.


I wrote back to him: “The workaround works, but I'm still trying to get my head around it! ”. He replied:  “Don't bother trying to figure out the reasoning. I'm of the opinion that it really should work the way that you were originally trying to do it. There's just no way of getting a program manager to agree with me and change it now... :-)”

The problem you will get if you don't use this fix can be reproduced as follows:
1. Create a new document (Doc1) and add a next page section break.

2. Set up section 1 with 1" margins and section 2 with 2" margins. Save and
close.

3. Create a second new document (Doc2) and give it 3" margins.

4. Select Insert + File, and insert Doc1 into Doc2.


What one would expect to get is as follows:
1. The final paragraph mark of Doc2 originally contains section formatting
of 3" margins, so the final section of Doc2 should still have 3" margins
following the InsertFile.

2. The section break inserted into Doc2 from Doc1 contains the section
formatting of 1" margins, so section 1 following the insertion should have
1" margins.


In fact, after inserting Doc2 into Doc1, section 2 has 2" margins, and section 1 has 3" margins.

Inserting a section break at the end of Doc1 doesn't help. What happens then is that after inserting Doc1 into Doc2, section 1 has 3" margins, section 2 has 2" margins, and section 3 has 2" margins.

In other words, whatever you do, the section formatting of section 1 in Doc1 is lost when inserted into Doc2.

If you insert a section break into Doc2 prior to inserting Doc1, it makes no difference – the section formatting of Section 1 is lost whatever you do. And you don't get these problems if you copy and paste.

As previously mentioned, the only fix is to insert a continuous section break at the start of the document you want to insert.

If the target document is completely blank, however, see also: How is it possible to copy an entire document into another document without bringing across the header and footer?


Second : The soulation what you most need

Reference:   http://www.experts-exchange.com/Programming/Programming_Languages/Q_21289408.html

OK, this seems to be the final solution, I tend to think that copying and pasting is a kludge, but it works so ....

Public Function UniteDocsIII(strDoc1 As String, strDoc2 As String, strResult As String) As Boolean
'Unites two documents as one
'Doc2 is placed at the end of Doc1
'The new document is saved as strResult

Dim fso As New FileSystemObject
Dim wApp As Word.Application
Dim wDoc As Word.Document
Dim wTDoc As Word.Document
Dim wSRng As Word.Range   'source Range
Dim wDRng As Word.Range   'Destination range


On Error GoTo proc_err

If fso.FileExists(strResult) Then
    fso.DeleteFile (strResult)
End If

SetStatus "Combining documents...."

Set wApp = New Word.Application
Set wDoc = wApp.Documents.Add

Set wTDoc = wApp.Documents.Open(FileName:=strDoc1)
Set wSRng = wTDoc.Content
wSRng.Copy
wDoc.Range(0, 0).Paste
wTDoc.Close

Set wDRng = wDoc.Content

wDRng.Collapse wdCollapseEnd
wDRng.InsertBreak Type:=wdSectionBreakNextPage
wDRng.Collapse wdCollapseEnd

Set wTDoc = wApp.Documents.Open(FileName:=strDoc2)
Set wSRng = wTDoc.Content
wSRng.Copy

wDRng.Paste
wTDoc.Close
wDoc.SaveAs FileName:=strResult
wDoc.Close

Set wApp = Nothing

UniteDocsIII = True

proc_exit:
Exit Function

proc_err:

Select Case ErrHand()
Case ErrAbort
    UniteDocsIII = False
    Resume proc_exit
Case ErrRetry
    Resume
Case ErrIgnore
    Resume Next
End Select

End Function


Or write used C#

http://www.cnblogs.com/zwwon/archive/2007/01/05/612010.html

 1public void MergeFileToWorddoc()
 2        {
 3            object missing = System.Reflection.Missing.Value;
 4
 5            object file1 = "c:/1.doc";        
 6            object file2 = "c:/2.doc";        
 7
 8            Word.ApplicationClass oWordApp = new Word.ApplicationClass();
 9            Word.Document oWordDoc = oWordApp.Documents.Open(ref file1,ref missing,ref missing,
10                ref missing,ref missing,ref missing,ref missing,ref missing,ref missing,ref missing,
11                ref missing,ref missing,ref missing,ref missing,ref missing,ref missing);    
12
13                        
14
15            object DocumentEnd = oWordDoc.Content.End-1;                            
16            Word.Range InsertRange = oWordDoc.Range(ref DocumentEnd,ref DocumentEnd);
17
18            //Add the 2.doc in the 1.doc's new page 
19            object PageBreak = Word.WdBreakType.wdPageBreak;        
20            //This is important(if you unstand the above text)
21            object wdSectionBreakN = Word.WdBreakType.wdSectionBreakNextPage;    
22            InsertRange.InsertBreak(ref PageBreak);
23            InsertRange.InsertBreak(ref wdSectionBreakN);
24
25            //Collapses the range to the ending position.
26            object CollEnd = Word.WdCollapseDirection.wdCollapseEnd;                     
27            InsertRange.Collapse(ref CollEnd);
28                    
29            Word.Document InsertDocument = oWordApp.Documents.Open(ref file2,ref missing,ref missing,
30                ref missing,ref missing,ref missing,ref missing,ref missing,ref missing,ref missing,
31                ref missing,ref missing,ref missing,ref missing,ref missing,ref missing);    
32                
33            Word.Range range = InsertDocument.Content;
34            range.Copy();
35            InsertRange.Paste();
36
37            InsertDocument.Close(ref missing,ref missing,ref missing);    
38            
39            oWordDoc.Save();
40            oWordDoc.Close(ref missing,ref missing,ref missing);
41            oWordApp.Quit(ref missing,ref missing,ref missing);
42        }


Thanks! Wish to help you!

posted on 2007-01-05 01:18  广思  阅读(829)  评论(0编辑  收藏  举报

导航