المساعد الشخصي الرقمي

مشاهدة النسخة كاملة : replace a Text in winword within a range



C# Programming
05-17-2013, 12:12 PM
...
[START]
blaaa blaa blaa
T h i s
(this is an empty line)
{END]
...
another line
--------------------------------------
I have been trying to remove empty lines appearing between
[START] and {END] within a word document.
Code seems to work if I did not have a textbox at top of my document. It seems that the textbox is also cleared once such operation takes place.
wordapp = new Word.Application(); wordapp.Visible = false; doc = wordapp.Documents.Open(wordPath); paragraphs = doc.Paragraphs; bool flag = false; foreach (Word.Paragraph paragraph in paragraphs) { string s = paragraph.Range.Text; string y = paragraph.Range.Text.Trim(); if (paragraph.Range.Text == "\r") { flag = true; paragraph.Range.Select(); wordapp.Selection.Delete(); continue; } else if (paragraph.Range.Text == "\r") { flag = false; paragraph.Range.Select(); wordapp.Selection.Delete(); continue; } else if (paragraph.Range.Text.Trim() == string.Empty ) { if (flag) { paragraph.Range.Select(); wordapp.Selection.Delete(); } continue; } } // Save the document and close document doc.Save(); // Quit the word application wordapp.Quit();
is there any other way to achieve what I do using Range in Word or dealing with a textbox in Word?

Student of life