Using Interop.Word, is there a way to make a replacement (using Find.Execute) and keep the original text alignment?

I am trying to write search / replace code for Word documents using Word Automation through Interop.Word (11.0). My documents have different fields (which do not appear in Document.Fields) that are surrounded by parentheses, for example. <DATE> must be replaced with DateTime.Now.Format("MM/dd/yyyy") . Search / replace works fine. Nevertheless, part of the replaced text is justified and after the replacement the text is transferred to the next line. Is there a way I can keep my excuse when doing a replacement? Code below:

 using Word = Microsoft.Office.Interop.Word; Word.Application wordApp = null; try { wordApp = new Word.Application {Visible = false}; //.... open the document .... object unitsStory = Word.WdUnits.wdStory; object moveType = Word.WdMovementType.wdMove; wordApp.Selection.HomeKey(ref unitsStory, ref moveType); wordApp.Selection.Find.ClearFormatting(); wordApp.Selection.Find.Replacement.ClearFormatting(); //tried removing this, no luck object replaceTextWith = DateTime.Now.ToString("MM/dd/yyyy"); object textToReplace = "<DATE>"; object replaceAll = Word.WdReplace.wdReplaceAll; object typeMissing = System.Reflection.Missing.Value; wordApp.Selection.Find.Execute(ref textToReplace, ref typeMissing, ref typeMissing, ref typeMissing, ref typeMissing, ref typeMissing, ref typeMissing, ref typeMissing, ref typeMissing, ref typeMissing, ref replaceTextWith, ref replaceAll, ref typeMissing, ref typeMissing, ref typeMissing, ref typeMissing); // ... save quit etc.... } finally { //clean up wordApp } 

TIA.

+6
c # ms-word office-interop word-automation
source share
1 answer

You can Microsoft.Office.Interop.Word.WdParagraphAlignment to align

  Microsoft.Office.Interop.Word.Application word = new Microsoft.Office.Interop.Word.Application(); Microsoft.Office.Interop.Word.Document doc = new Microsoft.Office.Interop.Word.Document(); object missing = System.Type.Missing; try { object fileName = @"C:\TT\change.doc"; doc = word.Documents.Open(ref fileName, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing); doc.Activate(); foreach (Microsoft.Office.Interop.Word.Range tmpRange in doc.StoryRanges) { tmpRange.Find.Text = "<DATE>"; tmpRange.Find.Replacement.Text = DateTime.Now.ToString("MM/dd/yyyy"); tmpRange.Find.Replacement.ParagraphFormat.Alignment = Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphJustify; tmpRange.Find.Wrap = Microsoft.Office.Interop.Word.WdFindWrap.wdFindContinue; object replaceAll = Microsoft.Office.Interop.Word.WdReplace.wdReplaceAll; tmpRange.Find.Execute(ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref replaceAll, ref missing, ref missing, ref missing, ref missing); } doc.Save(); doc.Close(ref missing, ref missing, ref missing); word.Application.Quit(ref missing, ref missing, ref missing); } catch (Exception ex) { doc.Close(ref missing, ref missing, ref missing); word.Application.Quit(ref missing, ref missing, ref missing); } 
+7
source share

All Articles