Selection.MoveEnd在使用Word互操作和C#操作Word文档时不工作
我正在使用运行.NET 4.0的VSTO 2010开发一个.NET程序,以在一组word文档中查找特定子目,并使用word.Interop复制该子目下的所有内容。通过匹配单词的for循环,我成功地搜索了这个单词,然后搜索下一节的起始单词(比如“功能”) 现在文档也有一个目录页,所以我发现简单的单词匹配不起作用,因为它会返回第一次出现的内容,这肯定是在内容部分。所以我试着发现第二次出现是成功的,但后来我意识到这个词甚至可能在副标题之前重复很多次。因此,我求助于寻找判决。在这里,我成功地找到了这两个词(我不得不将搜索字符串修改为“Requirements\r”,因为它是这样被读取的) 无论如何我现在面临的问题是,在我得到开始和结束句子后,我选择了整个文档,并使用MoveStart和MoveEnd,在复制它并粘贴到另一个word文档中之前,我减少了选择(因为我不知道如何使用范围或书签) 然而,虽然我成功地移动了开头,虽然结尾位置是正确的,MoveEnd总是移动到一些文本,这些文本至少比实际的多10句。我已经在这两个星期了,在这件事上的任何帮助都将不胜感激。我的意思不是对世界上所有的程序员有任何不敬。 我已经展示了我正在使用的代码 使用的变量是不言自明的。 //SourceApp和SourceDoc—读取发行说明来源的Word应用程序 //DestinationApp和DestinationDoc=写入新文档的Word应用程序Selection.MoveEnd在使用Word互操作和C#操作Word文档时不工作,c#,.net,ms-word,ms-office,office-interop,C#,.net,Ms Word,Ms Office,Office Interop,我正在使用运行.NET 4.0的VSTO 2010开发一个.NET程序,以在一组word文档中查找特定子目,并使用word.Interop复制该子目下的所有内容。通过匹配单词的for循环,我成功地搜索了这个单词,然后搜索下一节的起始单词(比如“功能”) 现在文档也有一个目录页,所以我发现简单的单词匹配不起作用,因为它会返回第一次出现的内容,这肯定是在内容部分。所以我试着发现第二次出现是成功的,但后来我意识到这个词甚至可能在副标题之前重复很多次。因此,我求助于寻找判决。在这里,我成功地找到了这两个
private void btnGenerate_Click(object sender, EventArgs e)
{
int startpos = findpos(SourceDoc, 1, starttext, sentencecount);
int endpos = findpos(SourceDoc, startpos, endtext, sentencecount);
object realstart = startpos - 1; // To retain the subheading
object realend = -(sentencecount - (endpos - 1)); // to subtract the next subheading
SourceDoc.Activate();
SourceDoc.ActiveWindow.Selection.WholeStory();
SourceDoc.ActiveWindow.Selection.MoveStart(WdUnits.wdSentence, realstart);
SourceDoc.ActiveWindow.Selection.MoveEnd(WdUnits.wdSentence, realend); // the problematic bit
SourceDoc.ActiveWindow.Selection.Copy();
IDataObject data = Clipboard.GetDataObject();
string allText = data.GetData(DataFormats.Text).ToString();
DestinationDoc.Activate();
DestinationDoc.ActiveWindow.Selection.WholeStory();
DestinationDoc.ActiveWindow.Selection.Delete();
DestinationDoc.ActiveWindow.Selection.Paste();
DestinationDoc.Save();
((_Application)SourceApp).Quit();
((_Application)DestinationApp).Quit();
textBox1.AppendText(allText);
}
int findpos(Document docx, int startpos, string txt, int sentencecount)
{
int pos = 0;
string text;
for (int i = startpos; i <= sentencecount; i++)
{
text = docx.Sentences[i].Text;
if (string.Equals(text, txt))
{
pos = i;
break;
}
}
return pos;
}
private void btnGenerate\u单击(对象发送方,事件参数e)
{
int startpos=findpos(SourceDoc,1,starttext,sentencecount);
int endpos=findpos(SourceDoc、startpos、endtext、sentencecount);
object realstart=startpos-1;//保留副标题
object realend=-(sentencecount-(endpos-1));//减去下一个子标题
SourceDoc.Activate();
SourceDoc.ActiveWindow.Selection.批发();
SourceDoc.ActiveWindow.Selection.MoveStart(WdUnits.wdSession,realstart);
SourceDoc.ActiveWindow.Selection.MoveEnd(WdUnits.wdSession,realend);//有问题的位
SourceDoc.ActiveWindow.Selection.Copy();
IDataObject data=Clipboard.GetDataObject();
string allText=data.GetData(DataFormats.Text).ToString();
DestinationDoc.Activate();
DestinationDoc.ActiveWindow.Selection.批发();
DestinationDoc.ActiveWindow.Selection.Delete();
DestinationDoc.ActiveWindow.Selection.Paste();
DestinationDoc.Save();
((_应用程序)SourceApp.Quit();
((_应用程序)DestinationApp.Quit();
textBox1.AppendText(所有文本);
}
int-findpos(文档docx、int-startpos、字符串txt、int-sentencecount)
{
int pos=0;
字符串文本;
对于(int i=startpos;i