简体   繁体   English

如何遍历每个单词(ms单词)?

[英]How to iterate through each word, ms-word?

Ok, my main objective is to go through each word and check if word is underlined. 好的,我的主要目标是仔细检查每个单词并检查单词是否带有下划线。 If it is, I want to change the font size to an int x. 如果是这样,我想将字体大小更改为int x。

I have tried simply going through each character like so Edit: Updated code 我已经尝试过像这样简单地遍历每个字符:编辑:更新代码

   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();
    }

But, I get this error 但是,我得到这个错误

Call was rejected by callee. 通话被通话对象拒绝。 (Exception from HRESULT: 0x80010001 (RPC_E_CALL_REJECTED)) (来自HRESULT的异常:0x80010001(RPC_E_CALL_REJECTED))

I have no idea why it gives that 我不知道为什么会这样

Your code could be improved upon heavily: 您的代码可以在很大程度上进行改进:

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;
      }
}

Here is my solution 这是我的解决方案

             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 };
                }

It works beautifully, but the only problem is that popups prevent the code from continuing 它工作得很漂亮,但是唯一的问题是弹出窗口阻止代码继续运行

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM