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.
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))
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
User contributions licensed under CC BY-SA 3.0