是否可以使用办公自动化从MS Word中的给定行号获取文本(行或句子)?我的意思是,如果我可以获得给定行号中的文本或作为该行的一部分的句子本身,这是可以的。
我不提供任何代码,因为我完全不知道如何使用办公自动化读取MS Word。我可以像这样打开这个文件:
var wordApp = new ApplicationClass();
wordApp.Visible = false;
object file = path;
object misValue= Type.Missing;
Word.Document doc = wordApp.Documents.Open(ref file, ref misValue, ref misValue,
ref misValue, ref misValue, ref misValue,
ref misValue, ref misValue, ref misValue,
ref misValue, ref misValue, ref misValue);
//and rest of the code given I have a line number = 3 ?
编辑:为了澄清@Richard Marskell -Drackir的疑问,虽然MS Word中的文本是一个很长的字符串链,但办公自动化仍然会让我们知道行号。实际上,我从另一段代码中获得行号本身,如下所示:
Word.Revision rev = //SomeRevision
object lineNo = rev.Range.get_Information(Word.WdInformation.wdFirstCharacterLineNumber);
例如,假设Word文件看起来像这样:
fix grammatical or spelling errors
clarify meaning without changing it correct minor mistakes add related resources or links
always respect the original author
这里有4行代码。
发布于 2012-02-08 04:29:20
幸运的是,经过一些史诗般的搜索,我得到了一个解决方案。
object file = Path.GetDirectoryName(Application.ExecutablePath) + @"\Answer.doc";
Word.Application wordObject = new Word.ApplicationClass();
wordObject.Visible = false;
object nullobject = Missing.Value;
Word.Document docs = wordObject.Documents.Open
(ref file, ref nullobject, ref nullobject, ref nullobject,
ref nullobject, ref nullobject, ref nullobject, ref nullobject,
ref nullobject, ref nullobject, ref nullobject, ref nullobject,
ref nullobject, ref nullobject, ref nullobject, ref nullobject);
String strLine;
bool bolEOF = false;
docs.Characters[1].Select();
int index = 0;
do
{
object unit = Word.WdUnits.wdLine;
object count = 1;
wordObject.Selection.MoveEnd(ref unit, ref count);
strLine = wordObject.Selection.Text;
richTextBox1.Text += ++index + " - " + strLine + "\r\n"; //for our understanding
object direction = Word.WdCollapseDirection.wdCollapseEnd;
wordObject.Selection.Collapse(ref direction);
if (wordObject.Selection.Bookmarks.Exists(@"\EndOfDoc"))
bolEOF = true;
} while (!bolEOF);
docs.Close(ref nullobject, ref nullobject, ref nullobject);
wordObject.Quit(ref nullobject, ref nullobject, ref nullobject);
docs = null;
wordObject = null;
Here是代码背后的天才。点击链接了解更多关于它是如何工作的解释。
发布于 2012-02-08 02:05:08
如果您要读取标准文本.txt文件,请使用此选项,您可以使用它通过一次调用读取文件
List<string> strmsWord =
new List<string>(File.ReadAllLines(yourFilePath+ YourwordDocName));
如果您想遍历并查看返回的项是什么,请使用如下所示的内容
foreach (string strLines in strmsWord )
{
Console.WriteLine(strLines);
}
或
我完全忘记了一些Word文档可能是二进制格式的东西,所以看看这个,把内容读到一个RichTextBox中,从那里你可以得到你想要的行号,或者在单词之后把它加载到一个列表中。如果你想阅读word文档的XML格式,这个链接将向你展示Reading from a Word Doc:这里有一个很好的关于结帐的链接,也是ReadXML Format of a Word Document
此onne是一个更简单的示例,它将内容读取到ClipBoard Load Word into ClipBoard中
发布于 2013-02-07 18:03:12
var word = new Word.Application();
object miss = Missing.Value;
object path = @"D:\viewstate.docx";
object readOnly = true;
var docs = word.Documents.Open(ref path, ref miss, ref readOnly, ref miss,
ref miss, ref miss, ref miss, ref miss, ref miss,
ref miss, ref miss, ref miss, ref miss, ref miss,
ref miss, ref miss);
string totaltext = "";
object unit = Word.WdUnits.wdLine;
object count = 1;
word.Selection.MoveEnd(ref unit, ref count);
totaltext = word.Selection.Text;
TextBox1.Text = totaltext;
docs.Close(ref miss, ref miss, ref miss);
word.Quit(ref miss, ref miss, ref miss);
docs = null;
word = null;
https://stackoverflow.com/questions/9181085
复制相似问题