C# 无法操作互操作Word获取的字符串文本
我使用此代码获取标题C# 无法操作互操作Word获取的字符串文本,c#,ms-word,C#,Ms Word,我使用此代码获取标题 foreach (Paragraph paragraph in this.Application.ActiveDocument.Paragraphs) { Style style = paragraph.get_Style() as Style; string styleName = style.NameLocal; string text = paragraph.Range.Text; if( styleName == "Heading 1
foreach (Paragraph paragraph in this.Application.ActiveDocument.Paragraphs)
{
Style style = paragraph.get_Style() as Style;
string styleName = style.NameLocal;
string text = paragraph.Range.Text;
if( styleName == "Heading 1" )
{
myList.Add(text);
}
}
由于列表是字符串,而且获取的文本也是字符串,因此我应该能够执行所有字符串操作,但不能执行join、concat或任何其他操作。基本上,它看起来像一个字符串,但不起字符串的作用。最后用Replace(“\r”,”)解决了这个问题。 当使用段落.range.text读取文本时,它会在文本末尾添加\r(替换)。存储在字符串中时,只需按段落.Range.Text.Replace(“\r”,”)将其删除即可
感谢MethodMan为我提供解决方案。当您使用调试器逐步完成代码时。。
styleName
的值是多少?text的值是多少?您能提供更多的信息吗?..text=“Function R1\r”styleName=“Heading 1”