我在C#中编写一个使用Microsoft Office互操作DLL来读取Word文档的应用程序。这很好。
我现在还想确定应用于我正在阅读的单词的格式 - 例如,检查它是粗体还是斜体还是带下划线。我怎么能这样做?
答案 0 :(得分:0)
您应该使用 Microsoft.Office.Interop.Word.Range <的粗体,斜体或下划线属性对象。
这是代码示例:
Microsoft.Office.Interop.Word.Application wordApp = new Microsoft.Office.Interop.Word.Application();
string fileName = @"C:\folder\document.docx";
Document wordDoc = wordApp.Documents.Open(fileName);
Microsoft.Office.Interop.Word.Range rng = wordDoc.Range();
foreach (Microsoft.Office.Interop.Word.Range word in rng.Words)
{
if (word.Bold == -1)
{
//Do wathever you want with the bold text
}
else if (word.Italic == -1)
{
//Do wathever you want with the italic text
}
else if ( ((WdUnderline) word.Underline) == WdUnderline.wdUnderlineSingle)
{
//Do wathever you want with the underline text
}
}
wordApp.Documents.Close();
wordApp.Quit();
请注意,属性word.Bold和word.Italic不会返回TRUE或FALSE,而是一个表示三种可能状态之一的整数。来自MSDN:
此属性返回True,False或wdUndefined(True和False的混合)。
在这些情况下,值-1对应于TRUE。 否则word.Underline属性是一个枚举,你可以在这里找到所有可能的值:WdUnderline enumeration