2016-03-07 45 views
0

我嘗試改變的Word文件的文檔模板自動化的過程。如何文檔鏈接到不同的結構化模板

如果模板類似的結構,即它們都使用heading1,那麼當文檔被鏈接到新的模板,它的工作原理。

但是,模板結構完全不同,heading1不再使用,現在是section1。我如何使用代碼更改這些部分標題?沿線if(heading1) rename to section1;

我正在使用Interop.Word來執行這些操作。

下面是我使用的代碼:

public string UpdateDocumentWithNewTemplate(string document, string theme, string template, Word.Application wordApp) 
     { 
      try 
      { 
       object missing = System.Reflection.Missing.Value; 
       Word.Document aDoc = null; 
       object notReadOnly = false; 
       object isVisible = false; 
       wordApp.Visible = false; 
       // create objects from variables for wordApp 
       object documentObject = document; 

       // open existing document 
       aDoc = wordApp.Documents.Open(ref documentObject, ref missing, ref notReadOnly, ref missing, ref missing, 
        ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref isVisible, 
        ref missing, ref missing, ref missing, ref missing); 
       aDoc.Activate(); 

       // set template and theme to overwrite the existing styles 
       aDoc.CopyStylesFromTemplate(template); 
       aDoc.ApplyDocumentTheme(theme); 
       aDoc.UpdateStyles(); 

       // save the file with the changes 
       aDoc.SaveAs(ref documentObject, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, 
        ref missing, ref missing, ref missing, ref missing, ref missing, ref missing); 

       // close the document 
       aDoc.Close(ref missing, ref missing, ref missing); 
       if (aDoc != null) 
        System.Runtime.InteropServices.Marshal.ReleaseComObject(aDoc); 
       aDoc = null; 

       return documentObject.ToString(); 
      } 
      catch (Exception exception) 
      { 
       return "Error: " + exception; 
      } 
     } 

回答

1

對於您需要先從其他模板導入樣式的具體實例,然後做一個查找/替換以替換應用的樣式。我從你的代碼,你已經得到了第一部分(aDoc.CopyStylesFromTemplate(template); aDoc.ApplyDocumentTheme(theme); aDoc.UpdateStyles();)見。

很多人不知道有關Word的查找內容/替換功能,它也可以用格式化工作。獲得必要的語法的最佳方法是在宏中記錄一個成功的查找/替換,然後將VBA移植到C#。在UI:

  1. Ctrl + H鍵打開「查找內容」框中,單擊「更多」,然後「格式」,然後選擇「樣式」
  2. 選擇替換對話框
  3. 隨着光標該樣式的名稱要查找並替換
  4. 在框中單擊「替換爲」
  5. 使用格式/樣式,再次,選擇要使用
  6. 點擊式「全部替換」。

enter image description here

這裏的結果我得到:

Selection.Find.ClearFormatting 
Selection.Find.Style = ActiveDocument.styles("Heading 1") 
Selection.Find.Replacement.ClearFormatting 
Selection.Find.Replacement.Style = ActiveDocument.styles("section2") 
With Selection.Find 
    .Text = "" 
    .Replacement.Text = "" 
    .Forward = True 
    .wrap = wdFindContinue 
    .Format = True 
    .MatchCase = False 
    .MatchWholeWord = False 
    .MatchByte = False 
    .CorrectHangulEndings = False 
    .HanjaPhoneticHangul = False 
    .MatchWildcards = False 
    .MatchSoundsLike = False 
    .MatchAllWordForms = False 
End With 
Selection.Find.Execute Replace:=wdReplaceAll 

你應該使用範圍,而不是選擇。所以C#代碼看起來像下面的代碼塊。注意如何

  1. 我得到整個文檔的範圍
  2. 創建範圍的Find對象,並使用
  3. 要爲查找引用樣式;我表明兩種可能
  4. 可以使用Find.Execute之前列出的幾乎所有的查找屬性。這也將有可能爲每個這些object對象,只有一個必要的truefalse然後將這些「被裁判」在Find.Execute上市。據我所知,這只是個人喜好的問題。我通過這種方式將VBA轉換爲C#代碼。
  5. 無論如何,Find.Execute「記得」這些設置,因此ref missing可以用於所有沒有專門設置的參數。在這種情況下,該方法中只使用「全部替換」命令。

    Word.Document doc = wdApp.ActiveDocument; 
        Word.Range rngFind = doc.Content; 
        Word.Find fd = rngFind.Find; 
        fd.ClearFormatting(); 
        Word.Style stylFind = doc.Styles["Heading 1"]; 
        fd.set_Style(stylFind); 
        fd.Replacement.ClearFormatting(); 
        fd.Replacement.set_Style(doc.Styles["section2"]); 
        fd.Text = ""; 
        fd.Replacement.Text = ""; 
        fd.Forward = true; 
        fd.Wrap = Word.WdFindWrap.wdFindStop; 
        fd.Format = true; 
        fd.MatchCase = false; 
        fd.MatchWholeWord = false; 
        fd.MatchByte = false; 
        fd.CorrectHangulEndings = false; 
        fd.HanjaPhoneticHangul = false; 
        fd.MatchWildcards = false; 
        fd.MatchSoundsLike = false; 
        fd.MatchAllWordForms = false; 
        object replaceAll = Word.WdReplace.wdReplaceAll; 
        object missing = Type.Missing; 
        fd.Execute(ref missing, ref missing, ref missing, ref missing, ref missing, 
         ref missing, ref missing, ref missing, ref missing, ref missing, 
         ref replaceAll, ref missing, ref missing, ref missing, ref missing); 
    
+0

感謝您的詳細解釋。使用OpenXml和Interop是否是不好的做法?使用Interop時,我似乎無法刪除/應用標題。 – PurpleSmurph

+0

你可以使用什麼工作:-)我的代碼(根據你的情況調整)不起作用嗎?在我發佈之前,它在我的測試中確實有效。文檔是否受到任何保護? –

+0

我不知道有什麼問題,似乎沒有被保護,但我們確實使用共享驅動器和共享點,最後我創建了一個新文檔並將原始內容複製/格式粘貼到新的文檔中,然後應用模板。我測試了你的代碼,它的工作原理=),我將在稍後將它集成到新版本中。 – PurpleSmurph

相關問題