C# 如何将docx文件中的所有单词放到听写器中
我想从docx(WORD)文件中提取所有单词,并将它们放入字典,字典中有所有值,因为我试图将它们作为键放入,但有时在docx文件中有相同的单词C# 如何将docx文件中的所有单词放到听写器中,c#,dictionary,ms-word,docx,C#,Dictionary,Ms Word,Docx,我想从docx(WORD)文件中提取所有单词,并将它们放入字典,字典中有所有值,因为我试图将它们作为键放入,但有时在docx文件中有相同的单词 ` Dictionary <int, string> motRap = new Dictionary<int, string>(); Microsoft.Office.Interop.Word.Application application = new Microsoft.Office.Interop.W
` Dictionary <int, string> motRap = new Dictionary<int, string>();
Microsoft.Office.Interop.Word.Application application = new Microsoft.Office.Interop.Word.Application();
Document document = application.Documents.Open("test.docx");
// Loop through all words in the document.
int count = document.Words.Count;
for (int i = 1; i <= count; i++)
{
string text = document.Words[i].Text;
motRap.Add(i, text);
}
// Close word.
application.Quit();
foreach(var o in motRap)
{
Console.WriteLine(o.Value);
}`
`Dictionary motRap=new Dictionary();
Microsoft.Office.Interop.Word.Application Application=新的Microsoft.Office.Interop.Word.Application();
Document=application.Documents.Open(“test.docx”);
//循环浏览文档中的所有单词。
int count=document.Words.count;
对于(int i=1;我可以捕获异常以查看它是什么吗?因为您似乎是新来的,所以当您遇到异常时,请尽量记住始终在问题中包含错误消息。另外,请花时间查看抛出的异常的数据
属性,因为有时它会提供有用的信息。No No.dll您可以使用的另一个工具是InnerException
属性:如果它不是null
,它可以显示有关出错原因的更多详细信息。消息错误是:“System.Runtime.InteropServices.COMException:消息过滤器指出应用程序正忙。(HRESULT的异常:0x8001010A(RPC_E_SERVERCALL_RETRYLATER))致Microsoft.Office.Interop.Word.DocumentClass.get_Words();以测试Program.cs中的.Program.Main(字符串[]args):第42行谢谢你的提示是的,我是新来的,而且我是法国人,所以我的英语不是很好(对不起)这是String text=document.Words[i].text;
42行吗?