首页 文章

复制word文档部分的内容

提问于
浏览
0

在我的应用程序中,我使用Microsoft.Office.Interop.Word,用于将内容写入word文档 .

var wordApplication = new Word.Application { Visible = true };
        var document = wordApplication.Documents.Add();
        document.Activate();

在每1分钟,我将在新的部分写几行文档 . 即,每隔5分钟开始在word文档中添加一个新部分,并将光标移动到该部分,然后写入内容 .

document.Sections.Add();
        wordApplication.ActiveWindow.Selection.GoTo(Word.WdGoToItem.wdGoToPage, Word.WdGoToDirection.wdGoToLast);

每隔10分钟,我需要在后台运行一个线程,并将每个部分中可用的内容复制到远程位置的不同文本文件中 .

我的问题是,我无法访问各个部分 . 建议一种方法,将每个部分中的文本复制到单独的变量或数组中 .

1 回答

  • 1
    System.Collections.ArrayList al = new System.Collections.ArrayList();
    
    int mycount = 0;
    
    foreach (Microsoft.Office.Interop.Word.Section section in document.Sections)
    
    {
    
        al.Insert(mycount, section.Range.Text.ToString());
    
        mycount++;
    
    }     
    
     mycount = 0;
    
     while (mycount < al.Count)
    
     {
    
        MessageBox.Show(" Section Text " + al[mycount].ToString());
    
       mycount++;
    
     }
    

    Check it Out It Worked For ME !!!!

相关问题