2010-05-18 7 views
6

Estoy intentando escribir código de búsqueda/reemplazo para documentos de Word utilizando Word Automation a través de Interop.Word (11.0). Todos mis documentos tienen varios campos (que no aparecen en Document.Fields) que están rodeados con corchetes, por ejemplo, <DATE> debe reemplazarse por DateTime.Now.Format("MM/dd/yyyy"). El buscar/reemplazar funciona bien. Sin embargo, parte del texto que se reemplaza está justificado a la derecha, y al reemplazarlo, el texto se ajusta a la siguiente línea. ¿Hay alguna manera de que pueda mantener la justificación cuando realizo el reemplazo? El código está a continuación:Usando Interop.Word, ¿hay alguna manera de hacer un reemplazo (usando Find.Execute) y mantener la justificación del texto original?

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.

Respuesta

7

Puede Microsoft.Office.Interop.Word.WdParagraphAlignment para la alineación

 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); 
     } 
+0

Después de la aplicación renunció objeto de liberación de la memoria utilizando Marshal.ReleaseComObject (el objeto). – KFP

Cuestiones relacionadas