मैं 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
}
टीआईए।
के बाद आवेदन Marshal.ReleaseComObject (अपने ऑब्जेक्ट) का उपयोग कर स्मृति से रिहाई वस्तु छोड़ने के लिए Microsoft.Office.Interop.Word.WdParagraphAlignment। – KFP