2013-01-10 3 views
4

텍스트 상자가있는 단어 문서가 있습니다. 자동 검색을 실행하여 주 문서에서 일치 항목을 찾아서 바꾸지 만 텍스트 상자의 내용과 일치하지 않습니다. 텍스트 상자를 포함하는 찾기 및 바꾸기 기능을 어떻게 말합니까?Word 자동화 찾기 및 바꾸기 텍스트 상자 포함하지 않음

Word.Range range = objDoc.Content; 

object findtext = Field; 
object findreplacement = Value; 
object findwrap = WdFindWrap.wdFindContinue; 
object findreplace = WdReplace.wdReplaceAll; 

range.Find.Execute(findtext, missing, missing, missing, missing, missing, missing, findwrap, missing, findreplacement, findreplace); 

나는 range = objDoc.content 행을 변경해야한다고 생각합니다.

+1

확인이 장소 밖으로 http://social.msdn.microsoft.com/Forums/en-US/csharpgeneral/thread/36cebc60-7c7e-494c-ad2d-0dcebce5a706/ 검색하는 과정에 주어진 일부 답변 텍스트 상자는 '기본 이야기'의 일부가 아닌지 확인합니다. – RhysW

답변

5

약간 지저분하지만이 나를 위해 일한 :

using System.Runtime.InteropServices; 
using Microsoft.Office.Interop.Word; 

namespace ConsoleApplication7 
{ 
    class Program 
    { 
     static void Main() 
     { 
      const string documentLocation = @"C:\temp\Foo.docx"; 
      const string findText = "Foobar"; 
      const string replaceText = "Woo"; 

      FindReplace(documentLocation, findText, replaceText); 
     } 

     private static void FindReplace(string documentLocation, string findText, string replaceText) 
     { 
      var app = new Application(); 
      var doc = app.Documents.Open(documentLocation); 

      var range = doc.Range(); 

      range.Find.Execute(FindText: findText, Replace: WdReplace.wdReplaceAll, ReplaceWith: replaceText); 

      var shapes = doc.Shapes; 

      foreach (Shape shape in shapes) 
      { 
       var initialText = shape.TextFrame.TextRange.Text; 
       var resultingText = initialText.Replace(findText, replaceText); 
       shape.TextFrame.TextRange.Text = resultingText; 
      } 

      doc.Save(); 
      doc.Close(); 

      Marshal.ReleaseComObject(app); 
     } 
    } 
} 

전 :

Before

후 :

After

+0

텍스트 상자가 어디에 있었는지 알아 내려고 노력한 지 오래되었습니다. 'doc.Shapes'는 승자입니다. – xan

+1

그런데도 TextFrame의 범위에서 찾기를 사용할 수 있습니다. 그것은 단지 별도의 호출이어야합니다. 단지 약간 성가신. – Chris

+0

@Chris gotcha, 잘 알고 있습니다! – JMK

-1

텍스트 WI를 대체하지 않습니다이 솔루션 Header의 TextBox로 얇게 만듭니다.

관련 문제