简体   繁体   English

C#Interop Word文档添加样式

[英]c# Interop Word Document add Style

I want to add style to a paragraph, but depending on the word language version I have to use a different name. 我想为段落添加样式,但是根据语言版本,我必须使用其他名称。 Now I'm creating an application for multiple users, and if they have an english version i can use "Header 1", but if they don't the style is not recognized. 现在,我正在为多个用户创建一个应用程序,如果他们有英文版本,我可以使用“标题1”,但是如果他们没有,则无法识别样式。 I was wondering what I should do, should I add a new style? 我想知道应该怎么做,是否应该添加新样式? I've been looking into this but I can't figure out how. 我一直在研究,但我不知道如何。

So on my dutch version i use: 因此,在荷兰语版本中,我使用:

selection.TypeParagraph();
object kop1 = "Kop1";
selection.set_Style(kop1);
selection.TypeText("test");

but english i need: 但是我需要英语:

selection.TypeParagraph();
object kop1 = "Header 1";
selection.set_Style(kop1);
selection.TypeText("test");

I actually need this to create a content table, I can't create a content table if I don't use the styles, right? 我实际上需要用它来创建内容表,如果不使用样式就不能创建内容表,对吗? Otherwise my content tables says it has no content. 否则我的内容表说它没有内容。

I make the content table using: 我使用以下方法制作内容表:

object start = adoc.Content.End - 1;
Range rangeForTOC = adoc.Range(ref start, ref missing);
TableOfContents toc = adoc.TablesOfContents.Add(rangeForTOC, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing);
adoc.TablesOfContents[1].TabLeader = WdTabLeader.wdTabLeaderDots;
adoc.TablesOfContents.Format = WdTocFormat.wdTOCClassic;
toc.Update();

Any ideas? 有任何想法吗?

You might want to use Word.WdBuiltinStyle enumeration instead of language specific strings. 您可能要使用Word.WdBuiltinStyle枚举而不是语言特定的字符串。 For example use Word.WdBuiltinStyle.wdStyleHeading1 in your above code. 例如,在上面的代码中使用Word.WdBuiltinStyle.wdStyleHeading1。

Another way to solve this issue is to add your own template and use the styles defined in that template. 解决此问题的另一种方法是添加您自己的模板并使用该模板中定义的样式。 In this case you supply your template in your distribution. 在这种情况下,您可以在分发中提供模板。

For using pre-defined table generation styles, also see this answer: Setting Word 2007 table style designs in code 有关使用预定义表生成样式的信息,另请参见以下答案: 在代码中设置Word 2007表样式设计

For one I would use DocX (no Com) 对于一个我会使用DocX (no Com)

Here is info about styling. 是有关样式的信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM