我想将预定义的样式应用到我的段落中(比如Heading2),这样我就可以更新我的内容表并自动填充它。
这是我的密码:
using Word = Microsoft.Office.Interop.Word;
object oMissing = System.Reflection.Missing.Value;
Word.Application oWord = new Word.Application();
Word.Document oDoc = oWord.Documents.Add(@"local path to a template",
ref oMissing, ref oMissing, ref oMissing);
object obrangePara = oDoc.Bookmarks[oEndOfDoc].Range;
var objpara = oDoc.Content.Paragraphs.Add(ref objrangePara);
objpara.set_Style(Word.WdBuiltinStyle.wdStyleHeading2);
objpara.Range.Text = "some text";

这在视觉上应用了样式,但是当我更新它时,它不会出现在内容表中。当我在Word中选择文本时,它表示它具有正常的文本样式,即使在视觉上它有Heading2样式。
如何确保预定义的样式被正确应用?
在这里,您可以看到样式在视觉上是OK的,但Word将其检测为普通文本:


完整代码清单:
object oMissing = System.Reflection.Missing.Value;
object oEndOfDoc = "\\endofdoc"; // endofdoc is a predefined bookmark
var oTemplate = @"C:\TestLab\SantiagoReport.dotx";
Word.Application oWord;
Word.Document oDoc;
oWord = new Word.Application();
oWord.Visible = true;
if(File.Exists(oTemplate))
{
oDoc = oWord.Documents.Add(oTemplate, ref oMissing, ref oMissing, ref oMissing);
Word.Table dateTable = findTable(oDoc, "Tests Date");
dateTable.Cell(2, 1).Range.Text = DateTime.Now.ToString();
Word.Table snTable = findTable(oDoc, "Serial Number");
snTable.Cell(2, 1).Range.Text = SerialNumber;
Word.Table userTable = findTable(oDoc, "User");
userTable.Cell(2, 1).Range.Text = User;
Word.Table timeTable = findTable(oDoc, "Total Elapsed Time");
timeTable.Cell(2, 1).Range.Text = String.Format("{0}h{1}m{2}s", StopWatch.Elapsed.Hours, StopWatch.Elapsed.Minutes, StopWatch.Elapsed.Seconds);
Word.Table summaryTable = findTable(oDoc, "Summary");
summaryTable.Cell(2, 2).Range.Text = nbLoadedTests.ToString();
summaryTable.Cell(3, 2).Range.Text = nbSelectedTests.ToString();
summaryTable.Cell(4, 2).Range.Text = nbPassedTests.ToString();
summaryTable.Cell(5, 2).Range.Text = nbFailedTests.ToString();
var testListBookmarkRange = oDoc.Bookmarks[oEndOfDoc].Range;
foreach (TestCategory category in TestList)
{
//category.ShouldExecuteTest
object objrangePara = oDoc.Bookmarks[oEndOfDoc].Range;
//object objrangPara2 = oDoc.Bookmarks[oEndOfDoc].Range;
//objrangePara.Select();
Word.Range rangetest = (Word.Range)objrangePara;
rangetest.set_Style(Word.WdBuiltinStyle.wdStyleHeading2);
var objpara = oDoc.Content.Paragraphs.Add(ref objrangePara);
//objpara.set_Style(Word.WdBuiltinStyle.wdStyleHeading2);
//objpara.Format = new Word.ParagraphFormat();
//objpara.Format.set_Style(Word.WdBuiltinStyle.wdStyleHeading2);
//objpara.Range.set_Style(Word.WdBuiltinStyle.wdStyleHeading2);
//objpara.Range.Text = String.Format("{0}: {1}{2}", category.ID, category.Name, Environment.NewLine);
rangetest.Text = String.Format("{0}: {1}{2}", category.ID, category.Name, Environment.NewLine);
//objpara.Range.set_Style(Word.WdBuiltinStyle.wdStyleHeading2);
//objpara.Format.set_Style(Word.WdBuiltinStyle.wdStyleHeading2);
foreach (Test test in category.TestList)
{
testListBookmarkRange = oDoc.Bookmarks[oEndOfDoc].Range;
Word.Table tbl = oDoc.Tables.Add(testListBookmarkRange, 3, 2);
tbl.Borders.InsideLineStyle = Word.WdLineStyle.wdLineStyleSingle;
tbl.Borders.OutsideLineStyle = Word.WdLineStyle.wdLineStyleSingle;
tbl.Cell(1, 1).Range.Text = test.ID.ToString();
tbl.Cell(1, 2).Range.Text = test.Name;
tbl.Cell(2, 1).Range.Text = "Result";
if (test.Result != null)
tbl.Cell(2, 2).Range.Text = test.Result.Pass ? "Pass" : "Fail";
tbl.Cell(3, 1).Range.Text = "Comment";
if (test.Result != null)
tbl.Cell(3, 2).Range.Text = test.Result.Message;
objrangePara = oDoc.Bookmarks[oEndOfDoc].Range;
objpara = oDoc.Content.Paragraphs.Add(ref objrangePara);
objpara.Range.Text = Environment.NewLine;
//test.TestItem.cbRunTest.Checked
}
}
oDoc.TablesOfContents[1].Update();
object pathtofile = @"C:\TestLab\test.docx";
oDoc.SaveAs2(ref pathtofile);
oDoc.Close();
oWord.Quit();
GC.Collect();
}发布于 2018-09-29 21:35:42
下面的方法对我有效。与问题中的代码相反,下面的示例
Word.Range对象InsertAfter("\n")插入新段落请注意,如果计划在以Normal以外的任何样式格式化的文本之后插入表,则应插入一个新段落并将其格式化为Normal样式,然后插入该表。否则,如果使用表样式,则表格式可能会出现问题。
object oCollapseEnd = Word.WdCollapseDirection.wdCollapseEnd;
foreach (string s in lst)
{
Word.Range rngEndOfDoc = oDoc.Bookmarks[oEndOfDoc].Range;
rngEndOfDoc.InsertAfter("\n");
rngEndOfDoc.Collapse(ref oCollapseEnd);
rngEndOfDoc.Text = s;
rngEndOfDoc.set_Style(Word.WdBuiltinStyle.wdStyleHeading2);
}发布于 2018-09-24 15:45:54
首先通过NuGet软件包管理器安装下面的软件包:
Install-Package Microsoft.Office.Interop.Word现在,您可以将自定义样式应用于任何选择范围。
在下面的示例代码中,标题1样式将适用于"Test.docx":document中的第一段。
// Define new application and open MS Word document.
Application app = new Application();
Document doc = app.Documents.Open(@"D:\Test.docx");
// Select first paragraph.
doc.Paragraphs[1].Range.Select();
// Define style as object and assign your custom style as string to it.
object style = "Heading 1";
// Set custom style to selected range
app.Selection.set_Style(ref style);
// Close document and pass true if you want save changes and finally quit from application.
doc.Close(true);
app.Quit();在这种方法中,任何更改,如将样式应用于任何选择范围,都会对视觉应用产生影响。
发布于 2021-02-14 07:38:44
不确定这个问题是否有解决方案,但我在将样式链接到段落时遇到了类似的问题。我在文本中用"\n“分隔的书写行也有问题。下面的代码适用于我,并正确地将样式应用于传递的文本,包括换行符,样式被链接到结果文档中的文本。
using Word = Microsoft.Office.Interop.Word;
public void WritePara(String pText, String pStyle) {
Word.Paragraph p1 = oDoc.Content.Paragraphs.Add();
object oStart = oDoc.Content.End - 1;
p1.Range.Text = pText;
object oEnd = oDoc.Content.End - 1;
Word.Range para = oDoc.Range(oStart, oEnd);
object oStyleName = pStyle;
para.set_Style(ref oStyleName);
}https://stackoverflow.com/questions/52444786
复制相似问题