2010-10-01 110 views
1

我支持的應用程序是通過將一個文檔中的幾個Word文檔彼此相鄰地複製來創建一個合併的Word文檔。從另一個word文檔的副本創建word文檔時遇到格式問題

的問題是在一些被附加汞混合的文件中改變文件的字段的格式,而被混合的文件是AppendDocument副本(想象一下,如果我們有一個文件被重組文件中複製)

的Word文檔的第一線和第二線的樣子:

來源: X先生要:加里Ÿ

地址ESS: NorkYork 日期: 2010/05/01

來源:要:地址:日期:大膽和大小10在AppendDocument而聯軍文檔中它們是大膽,但他們的大小變爲12!

我很困惑我不確定爲什麼這4個項目的尺寸改變了,即使它們的實際值有相同的尺寸!

請參閱下面的代碼,哪2個文件路徑通過。第一個是BaseDocuemnt或合併文件,第二個是附加的文件。

Private Sub DocumentAppend(ByVal strBaseDocument As String, ByVal strAppendDocument As String) 
    Dim FirstDocument As Boolean 
    Dim fleBaseDocument As File 
    Dim wrdRange As Word.Range 
    Dim wrdAppendDocument As Word.DocumentClass 
    wrdAppendDocument = New Word.DocumentClass() 
    Dim AmalgamatedDocument As Word.DocumentClass 
    AmalgamatedDocument = New Word.DocumentClass() 
    Dim wrdApp As Word.ApplicationClass 
    wrdApp = AmalgamatedDocument.Application 
    Dim AmalgamatedDocumentRange As Word.Range 
    Try 
     wrdApp.Visible = True 
     If fleBaseDocument.Exists(strBaseDocument) Then 
      FirstDocument = False 
      AmalgamatedDocument = wrdApp.Documents.Open(strBaseDocument) 
     Else 
      FirstDocument = True 
      AmalgamatedDocument = wrdApp.Documents.Add() 
     End If 

     AmalgamatedDocumentRange = AmalgamatedDocument.Content 
     AmalgamatedDocumentRange.Collapse(Word.WdCollapseDirection.wdCollapseEnd) 
     If Not FirstDocument Then 
      AmalgamatedDocumentRange.InsertBreak (Word.WdBreakType.wdSectionBreakNextPage) 
     End If 
     ''# get the document to be appended 
     wrdAppendDocument = wrdApp.Documents.Open(strAppendDocument) 
     wrdAppendDocument.Activate() 
     wrdAppendDocument.Select() 
     ''# +++++++++++++++++++++++ 
     wrdApp.Selection.Copy() 
     wrdApp.Selection.CopyFormat() 
     AmalgamatedDocument.Activate() 
     wrdRange = AmalgamatedDocument.Content 
     wrdRange.Collapse(Word.WdCollapseDirection.wdCollapseEnd) 
     wrdRange.Paste() 
     ''# New 
     wrdApp.Selection.PasteFormat() 
     ''# +++++++++++++++++++++++ 
     wrdAppendDocument.Close() 
     ''# save the new document 
     AmalgamatedDocument.SaveAs(FileName:=strBaseDocument) 
     AmalgamatedDocument.Close() 
End Sub 

任何意見將不勝感激!

回答

0

那麼,首先,我會先嚐試避免使用SELECTION對象,當你不想實際操作Word中的活動屏幕文檔時。

我也建議看看Range.InsertFile方法。基本上,您打開或創建您的「目標」文檔,然後獲取CONTENT的範圍對象,將其摺疊到最後,最後調用INSERTFILE在該點插入文件。

像這樣的事情

 dim rngend = Doc.Content 
     rngend.Collapse(WdCollapseDirection.wdCollapseEnd) 
     rngend.InsertFile(File, ConfirmConversions:=False, Link:=False, Attachment:=False) 

這將通常忠實地保存格式,雖然我已經遇到過一些情況下它並不完全100%