2012-04-19 5 views
2

.net 4.0/vs2010、office 2007を使用してください。複数のドキュメントをWord.Application(COM interop)と組み合わせると、フォーマットが正しく行われません。

私は最終結果を達成するために他の方法には開いています。私はWordに結婚していませんが、ライセンスされた第三者ツール(Asposeなし)を含めることができないという制限があり、ユーザーのマシン上のインストールを最小限に抑えたいと思います。これは企業環境なので、私はいくつかのコントロールを持っています。

2つまたは3つの単語の文書を1つの文書として印刷する必要があります。私のコードに従って、それは動作します。ただし、2番目のドキュメントの書式設定は常に少しずつ(段落の間隔、フォント、マージン)になります。奇妙なことに、3番目のドキュメント(ページの向きが違う)がうまくいきます。ユーザーが文書をFAXサーバーに送信する必要がある場合もあるため、最初のページはFAXのカバーシートになります。

私はどこで失敗しましたか?メソッドKillCom(オブジェクトo)は、System.Runtime.InteropServices.Marshal.ReleaseComObject(o)を呼び出します。私は元の書式を維持するためにPasteAndFormat機能を使用し、また、各ドキュメントの余白や向きを維持するためにページのプロパティをコピー

public void CombineMultipleDocuments(string coverSheetPath, string[] documentPaths, string destinationFile, bool makeVisible) 
     { 
      List<string> docs = new List<string>(); 

      if (string.IsNullOrWhiteSpace(destinationFile)) 
       throw new ArgumentException("The destinationfile is required"); 
      try 
      { 
       //strip invalid paths 
       foreach (string p in documentPaths) 
       { 
        if (!string.IsNullOrWhiteSpace(p) && File.Exists(p)) 
        { 
         docs.Add(p); 
        } 
       } 

       if (docs.Count == 0) 
        throw new ArgumentException("There are no documents to print"); 
       //open the cover sheet 
       wrdApp = new Word.Application(); 
       wrdApp.Visible = makeVisible; 

       //if there is no cover sheet open the first document and append the remaining docs to it 
       if (string.IsNullOrWhiteSpace(coverSheetPath)) 
       { 
        coverSheetPath = docs[0]; 
        docs.RemoveAt(0); 
       } 
       wrdDoc = wrdApp.Documents.Open(coverSheetPath, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing 
        , ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing 
        , ref oMissing, ref oMissing); 
       wrdRange = wrdDoc.Content; 

       //a little space 
       wrdRange.InsertParagraphAfter(); 
       //attach the rest of the documents 
       foreach (string path in docs) 
       { 
        //open the new source document to be inserted 
        Word._Document newDoc = wrdApp.Documents.Open(path, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing 
        , ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing 
        , ref oMissing, ref oMissing); 

        //continues page break 
        wrdRange.Collapse(Word.WdCollapseDirection.wdCollapseEnd); 
        wrdRange.InsertBreak(Microsoft.Office.Interop.Word.WdBreakType.wdSectionBreakNextPage); 

        //create a new section for our new content 
        Word.Section sec = wrdRange.Sections.Add(ref oMissing, ref oMissing); 

        //copy the source document's styles, fonts, etc 
        sec.PageSetup.Orientation = newDoc.PageSetup.Orientation; 

        sec.Range.Font = newDoc.Content.Font; 

        //unlink footer and headers 
        sec.Footers[Word.WdHeaderFooterIndex.wdHeaderFooterPrimary].LinkToPrevious = false; 
        sec.Footers[Word.WdHeaderFooterIndex.wdHeaderFooterPrimary].Range.FormattedText = newDoc.Sections[1].Footers[Word.WdHeaderFooterIndex.wdHeaderFooterPrimary].Range.FormattedText; 

        sec.Headers[Word.WdHeaderFooterIndex.wdHeaderFooterPrimary].LinkToPrevious = false; 
        sec.Headers[Word.WdHeaderFooterIndex.wdHeaderFooterPrimary].Range.FormattedText = newDoc.Sections[1].Headers[Word.WdHeaderFooterIndex.wdHeaderFooterPrimary].Range.FormattedText; 

        wrdRange.FormattedText = newDoc.Content.FormattedText; 


        //close 
        newDoc.Saved = true; 
        newDoc.Close(ref oFalse, ref oMissing, ref oMissing); 
        newDoc = null; 
        KillCOM(newDoc); 

       } 

       //the diary function needs these variables to be set 
       Word.Variables wrdVars = wrdDoc.Variables; 

       wrdVars.Add("Letter", WrdPropLETTER_NAME); 
       wrdVars.Add("FileKey", WrdPropFILE_KEY); 
       wrdVars.Add("LetterTo", WrdPropLETTER_TO); 
       wrdVars.Add("LetterFirstName", WrdPropLETTER_FNAME); 
       wrdVars.Add("LetterLastName", WrdPropLETTER_LNAME); 
       wrdVars.Add("LetterCompany", WrdPropLETTER_COMPANY); 
       wrdVars.Add("LetterCategory", WrdPropLETTER_CATEGORY); 
       wrdVars.Add("CurrentHeaderDoc",WrdPropCUR_HEADERDOC); 
       wrdVars.Add("CurrentDataDoc", WrdPropCUR_DATADOC); 


       wrdDoc.Protect(Word.WdProtectionType.wdAllowOnlyFormFields, ref oMissing, "mojojojo"); 
       wrdDoc.SaveAs(destinationFile, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing 
        , ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing 
        , ref oMissing, ref oMissing); 

       //close if hidden 
       if (!makeVisible) 
       { 
        wrdDoc.Saved = true; 
        wrdDoc.Close(ref oFalse, ref oMissing, ref oMissing); 

        wrdApp.Quit(ref oFalse, ref oMissing, ref oMissing); 
        wrdDoc = null; 
        wrdApp = null; 
       } 

       //clean up 
       if (File.Exists(coverSheetPath)) 
        File.Delete(coverSheetPath); 
       foreach (string path in documentPaths) 
       { 
        if (File.Exists(path)) 
         File.Delete(path); 
       } 
      } 
      catch (Exception e) 
      { 
       string error = e.ToString(); 
      } 
      finally 
      { 
       wrdRange = null; 
       wrdDoc = null; 
       wrdApp = null; 
       KillCOM(wrdRange); 
       KillCOM(wrdDoc); 
       KillCOM(wrdApp); 

      } 

     } 
+0

異なるスタイル定義をマージしている可能性があります。 – SLaks

答えて

0

void mergeDocumentsTest(List<string> documentFiles) 
{ 

     _Application oWord = new Microsoft.Office.Interop.Word.Application(); 
     _Document oDoc = oWord.Documents.Add(); 
     Selection oSelection = oWord.Selection; 

     foreach (string documentFile in documentFiles) 
     { 
      _Document oCurrentDocument = oWord.Documents.Add(documentFile); 
      copyPageSetup(oCurrentDocument.PageSetup, oDoc.Sections.Last.PageSetup); 
      oCurrentDocument.Range().Copy(); 
      oSelection.PasteAndFormat(WdRecoveryType.wdFormatOriginalFormatting); 
      if (!Object.ReferenceEquals(documentFile, documentFiles.Last())) 
       oSelection.InsertBreak(WdBreakType.wdSectionBreakNextPage); 
     } 

     oDoc.SaveAs("testdoc.doc"); 
     oDoc.Close(); 

     //TODO: release objects, close word application 

    } 

    void copyPageSetup(PageSetup source, PageSetup target) 
    { 
     target.PaperSize = source.PaperSize; 

     //target.Orientation = source.Orientation; //not working in word 2003, so here is another way 
     if (!source.Orientation.Equals(target.Orientation)) 
      target.TogglePortrait(); 

     target.TopMargin = source.TopMargin; 
     target.BottomMargin = source.BottomMargin; 
     target.RightMargin = source.RightMargin; 
     target.LeftMargin = source.LeftMargin; 
     target.FooterDistance = source.FooterDistance; 
     target.HeaderDistance = source.HeaderDistance; 
     target.LayoutMode = source.LayoutMode; 
    } 
関連する問題