如何迭代每个单词,ms单词

本文关键字:单词 ms 何迭代 迭代 | 更新日期: 2023-09-27 18:28:58

好的,我的主要目标是浏览每个单词,并检查单词是否有下划线。如果是,我想将字体大小更改为int x。

我试着简单地浏览每个角色编辑:更新代码

   private void button1_Click(object sender, EventArgs e)
    {
        word.Application page = new word.Application();
        page.Visible = true;
        word.Document doc = null;
        foreach (string fi in listBox1.Items)
        {
            doc = page.Documents.Open(Application.StartupPath + "''old''" + fi);
            if (doc != null)
            {
                int start = 0;
                foreach (string text in doc.Range().Text.Split(" 'r'n't.".ToCharArray()))
                {
                    int x = doc.Range().Text.IndexOf(text, start);
                    if (doc.Range(x, text.Length - 1).Underline == word.WdUnderline.wdUnderlineSingle)
                        doc.Range(x, text.Length - 1).Font = new word.Font() { Name = "Times New Roman", Bold = 4, Size = 12 };
                    else
                        doc.Range(x, text.Length - 1).Font = new word.Font() { Name = "Times New Roman", Size = 8 };
                    start = x+text.Length;
                }
            }
        }
        //doc.Save();
        // doc.Close();
        // page.Quit();
    }

但是,我得到这个错误

被呼叫方拒绝了呼叫。(HRESULT中的异常:0x80010001(RPC_E_CALL_REJECTED))

我不知道为什么它会给出

如何迭代每个单词,ms单词

您的代码可以在以下方面得到很大改进:

doc = page.Documents.Open(System.IO.Path.Combine(Application.StartupPath, "old", fi));
if (doc != null)
{
     word.Font RegularFont = new word.Font() { Name = "Times New Roman", Size = 12 };
     word.Font BigFont = new word.Font() { Name = "Times New Roman", Size = 8 };
     for (int x = 1; x <= doc.Words.Count; x++)
     {
          if (doc.Words[x].Underline != word.WdUnderline.wdUnderlineNone && doc.Words[x].Underline != word.WdUnderline.wdUnderlineDouble)
               doc.Words[x].Font = RegularFont;
           else
                doc.Words[x].Font = BigFont;
      }
}

这是我的解决方案

             doc = page.Documents.Open(Application.StartupPath + "''old''" + fi);
            if (doc != null)
            {
                for (int x = 1; x <= doc.Words.Count - 1; x++)
                {
                        if (doc.Words[x].Underline != word.WdUnderline.wdUnderlineNone && doc.Words[x].Underline != word.WdUnderline.wdUnderlineDouble)
                            doc.Words[x].Font = new word.Font() { Name = "Times New Roman", Bold = 4, Size = 12 };
                        else
                            doc.Words[x].Font = new word.Font() { Name = "Times New Roman", Size = 8 };
                }

它工作得很好,但唯一的问题是弹出窗口阻止代码继续