2010-05-18 5 views
6

मैं Interop.Word (11.0) के माध्यम से वर्ड ऑटोमेशन का उपयोग करके वर्ड दस्तावेज़ों के लिए कोड ढूंढने/प्रतिस्थापित करने का प्रयास कर रहा हूं। मेरे दस्तावेज़ों में सभी फ़ील्ड हैं (जो दस्तावेज़.फिल्ड्स में दिखाई नहीं देते हैं) जो ब्रैकेट से घिरे हुए हैं, उदाहरण के लिए, <DATE> को DateTime.Now.Format("MM/dd/yyyy") के साथ प्रतिस्थापित करने की आवश्यकता है। खोज/प्रतिस्थापन ठीक काम करता है। हालांकि, कुछ पाठ प्रतिस्थापित किया जा रहा है, सही है, और प्रतिस्थापन पर, पाठ अगली पंक्ति में लपेटता है। क्या कोई तरीका है कि जब मैं प्रतिस्थापन करता हूं तो मैं औचित्य रख सकता हूं? कोड नीचे है:इंटरऑप.ऑर्ड का उपयोग करना, क्या प्रतिस्थापन करने का कोई तरीका है (Find.Execute का उपयोग करके) और मूल पाठ का औचित्य रखें?

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 
} 

टीआईए।

उत्तर

7

आप कर सकते हैं संरेखण

 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

के बाद आवेदन Marshal.ReleaseComObject (अपने ऑब्जेक्ट) का उपयोग कर स्मृति से रिहाई वस्तु छोड़ने के लिए Microsoft.Office.Interop.Word.WdParagraphAlignment। – KFP