2016-03-07 56 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