我想将预定义的样式应用到我的段落中(比如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-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();在这种方法中,任何更改,如将样式应用于任何选择范围,都会对视觉应用产生影响。
https://stackoverflow.com/questions/52444786
复制相似问题