好的,我的主要目标是检查每个单词并检查单词是否加下划线。 如果是,我想将字体大小更改为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))
我不知道为什么会给出
答案 0 :(得分: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;
}
}
答案 1 :(得分: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 };
}
它运行得很漂亮,但唯一的问题是弹出窗口会阻止代码继续