0

好的,我的主要目标是检查每个单词并检查单词是否带有下划线。如果是,我想将字体大小更改为 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))

我不知道为什么它会给出

4

2 回答 2

1

您的代码可以大大改进:

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;
      }
}
于 2013-08-25T16:47:29.630 回答
0

这是我的解决方案

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

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

于 2013-08-24T17:32:59.460 回答