如何捕獲正在使用c#編寫的word文檔中的文本並將其保存到內存中。 對不起,我的英語和謝謝你的答案從當前文檔中捕獲文本
0
A
回答
0
首先,你必須添加Microsoft.Office.Interop.Word
從這裏的參考。
然後你就可以讀你的Word文檔中的句子串名單是這樣的:
using Word = Microsoft.Office.Interop.Word;
public List<string> ReadWordDoc()
{
object objMissing = System.Reflection.Missing.Value;
//Start Word application.
Word._Application wordApp;
Word._Document wordDoc;
wordApp = new Word.Application();
wordApp.Visible = false;
object fileName = @"c:\temp\TestWord.docx";
wordDoc = wordApp.Documents.Open(ref fileName,
ref objMissing, ref objMissing, ref objMissing, ref objMissing, ref objMissing,
ref objMissing, ref objMissing, ref objMissing, ref objMissing, ref objMissing,
ref objMissing, ref objMissing, ref objMissing, ref objMissing, ref objMissing);
List<string> sentences = new List<string>(); // List to store sentences.
Word.Range rng;
for (int i = 1; i < wordDoc.Sentences.Count+1; i++)
{
object startLocation = wordDoc.Sentences[i].Start;
object endLocation = wordDoc.Sentences[i].End;
// Supply a Start and End value for the Range.
rng = wordDoc.Range(ref startLocation, ref endLocation);
// Select the Range.
rng.Select();
sentences.Add(rng.Text);
}
object doNotSaveChanges = Word.WdSaveOptions.wdDoNotSaveChanges;
wordDoc.Close(ref doNotSaveChanges, ref objMissing, ref objMissing);
wordApp.Quit(ref objMissing, ref objMissing, ref objMissing);
return sentences;
}
0
@Batista:如果你問如何監控用戶正在做Word應用程序 - 「捕捉你正在寫的文字」 - 然後簡短的回答是你不能。在Word API中沒有任何內容支持監視擊鍵和鼠標操作,缺少一些事件,如WindowSelectionChange,WindowBeforeDoubleClick和WindowBeforeRightClick。
較長的答案是可以使用Windows API進行一些監視。如果您進行Google搜索,您應該參與一些代碼示例的討論。我見過的是在MSDN的VSTO論壇(https://social.msdn.microsoft.com/Forums/vstudio/en-US/home?forum=vsto)。但是,您的里程可能會有所不同,因爲Windows API截取Word應用程序中消息的有效性在Word和Windows版本之間有所不同。
相關問題
- 1. Roslyn:獲取當前項目/獲取當前選中的文檔
- 2. Visual Studio DTE2:如何獲取當前文檔的文本內容
- 3. 從文本編輯器捕獲文本
- 4. 將文本粘貼到文本視圖之前捕獲文本
- 5. 獲取當前打開的文檔
- 6. 用Javascript獲取當前文檔
- 7. 獲取當前Word文檔的OneDrive項目ID文檔
- 8. 如何從文檔中的當前字段獲取數組值?
- 9. 從當前文本區域獲取文本
- 10. 從當前文檔到新的window.open(「otherdomainURL」)
- 11. MongoDB的繼續拉從當前文檔
- 12. 從當前文檔生成pdf
- 13. Xpages從當前文檔複製附件到新文檔
- 14. 從圖像中捕獲部分文本並突出顯示捕獲的文本
- 15. 從html文本中捕獲數字
- 16. 從當前捕獲獲取設備ID
- 17. 獲取當前選中的文本
- 18. Microsoft.Office.Interop.Word - 覆蓋當前文檔中的文本,如何阻止它?
- 19. 從外部HTML文檔獲取文本
- 20. 接取當前文檔
- 21. Jquery - 從當前URL中取文本
- 22. 獲取文檔中的下一個div後,當前的文檔與同一類
- 23. Android:onSearchRequested()捕獲文本
- 24. 獲取從IExternalApplication當前應用程序和文檔 - 的Revit
- 25. Android從當前上下文獲取文本
- 26. 捕獲文本選擇在谷歌文檔
- 27. 如何在當前文檔中加載DOM文檔?
- 28. Google文檔:設置當前文檔中定義的標題
- 29. C#WPF - 如何總是從文本框中獲取當前文本?
- 30. 從CSV文件中獲取R文本挖掘文檔
紅色辦公自動化,它可以讓你與微軟Word進行交互 – user469104
請發佈一些你試過的代碼和錯誤。請參閱堆棧溢出的發佈準則。謝謝! – maxshuty