wordapp = new Word.Application();
wordapp.Visible = true;
Object filename = textBox3.Text;//название файла для обработки
Object confirmConversions = true;
Object readOnly = false;
Object addToRecentFiles = true;
Object passwordDocument = Type.Missing;
Object passwordTemplate = Type.Missing;
Object revert = false;
Object writePasswordDocument = Type.Missing;
Object writePasswordTemplate = Type.Missing;
Object format = Type.Missing;
Object encoding = Type.Missing; ;
Object oVisible = Type.Missing;
Object openConflictDocument = Type.Missing;
Object openAndRepair = Type.Missing;
Object documentDirection = Type.Missing;
Object noEncodingDialog = false;
Object xmlTransform = Type.Missing;
#if OFFICEXP
worddocument=wordapp.Documents.Open2000(ref filename,
#else
worddocument = wordapp.Documents.Open(ref filename,
#endif
ref confirmConversions, ref readOnly, ref addToRecentFiles,
ref passwordDocument, ref passwordTemplate, ref revert,
ref writePasswordDocument, ref writePasswordTemplate,
ref format, ref encoding, ref oVisible,
ref openAndRepair, ref documentDirection, ref noEncodingDialog, ref xmlTransform);
Object unit = Word.WdUnits.wdStory;
Object extend = Word.WdMovementType.wdMove;
wordapp.Selection.HomeKey(ref unit, ref extend);
Word.Find fnd = wordapp.Selection.Find;
fnd.ClearFormatting();
Object findText = "188";//Поиск слова, за место которого потом вставлю слово
Object matchCase = Type.Missing;
Object matchWholeWord = Type.Missing;
Object matchWildcards = Type.Missing;
Object matchSoundsLike = Type.Missing;
Object matchAllWordForms = Type.Missing;
Object forward = Type.Missing;
Object wrap = Type.Missing;
Object replaceWith = Type.Missing;
Object replace = Type.Missing;
Object matchKashida = Type.Missing;
Object matchDiacritics = Type.Missing;
Object matchAlefHamza = Type.Missing;
Object matchControl = Type.Missing;
if (fnd.Execute(ref findText, ref matchCase, ref matchWholeWord,
ref matchWildcards, ref matchSoundsLike, ref matchAllWordForms,
ref forward, ref wrap, ref format, ref replaceWith,
ref replace, ref matchKashida, ref matchDiacritics,
ref matchAlefHamza, ref matchControl))
{
MessageBox.Show("Text found.", "Поиск искомого слова");
}
else
{
MessageBox.Show("Text not found.", "Поиск искомого слова");
}
//Выставляем курсор на начало докуммента и производим поиск.
wordapp.Selection.HomeKey(ref unit, ref extend);
findText = "926";//Искомый текс.
if (fnd.Execute(ref findText, ref matchCase, ref matchWholeWord,
ref matchWildcards, ref matchSoundsLike, ref matchAllWordForms,
ref forward, ref wrap, ref format, ref replaceWith,
ref replace, ref matchKashida, ref matchDiacritics,
ref matchAlefHamza, ref matchControl))
{
MessageBox.Show("Text found.", "Поиск искомого слова");
//Задаем режим вставки. Шрифты, размер, выделение и т.д.
wordapp.Options.Overtype = false;
//wordapp.Selection.Font.Italic = 0;
//wordapp.Selection.Font.Bold = 1;
wordapp.Selection.Font.Underline = Word.WdUnderline.wdUnderlineNone;
wordapp.Selection.TypeText("Megafon");//На что заменяем. В данном случае на слово "Megafon"
MessageBox.Show("Слово заменено", "Замена слов");
}
else
{
MessageBox.Show("Text not found.", "Поиск искомого слова");
}
private Word.Application wordapp;
private Word.Documents worddocuments;
private Word.Document worddocument;
и еще подцепи в "Add reference" такие библ.
Microsoft.Office.Interop.Word
Microsoft.Office.Tools.Word
Данное сообщение получено с сайта GotDotNet.RU
|