2016-04-28 53 views
2

此代碼將圖像替換爲文本,但將圖像的多個副本放置在文檔的開頭。我希望將圖像放置在文字出現的相同位置。我的查找文本在表格單元格中可用。這是因爲那個嗎?在C#中用相同位置的圖像替換文本

using System; 
    using System.Collections.Generic; 
    using System.Linq; 
    using System.Text; 
    using System.Diagnostics; 
    using System.IO; 
    using word = Microsoft.Office.Interop.Word; 
    using System.Runtime.InteropServices; 
    //using System.Drawing; 


    namespace WritingIntoDocx 
    { 
     [ComVisible(true)] 
     public interface IMyClass 
     { 

      void DocumentDigitalSign(string filep,string findt,string replacet); 
     } 


     [ComVisible(true)] 
     [ClassInterface(ClassInterfaceType.None)] 

     public class Program : IMyClass 
     { 
      public void DocumentDigitalSign(string filep, string findt, string imagepath) 
      { 
       string filepath = filep; 
       string Findtext = findt; 
       word.Application app = new word.Application(); 
       word.Document doc = app.Documents.Open(filepath); 

       word.Range myStoryRange = doc.Range(); 

       //First search the main document using the Selection 
       word.Find myFind = myStoryRange.Find; 
       myFind.Text = Findtext;    myFind.Replacement.Application.Selection.InlineShapes.AddPicture(imagepath); 
       myFind.Forward = true; 
       myFind.Wrap = word.WdFindWrap.wdFindContinue; 
       myFind.Format = false; 
       myFind.MatchCase = false; 
       myFind.MatchWholeWord = false; 
       myFind.MatchWildcards = false; 
       myFind.MatchSoundsLike = false; 
       myFind.MatchAllWordForms = false; 
       myFind.Execute(Replace: word.WdReplace.wdReplaceAll); 

       //'Now search all other stories using Ranges 
       foreach (word.Range otherStoryRange in doc.StoryRanges) 
       { 
        if (otherStoryRange.StoryType != word.WdStoryType.wdMainTextStory) 
        { 
         word.Find myOtherFind = otherStoryRange.Find; 
         myOtherFind.Text = Findtext;   myOtherFind.Replacement.Application.Selection.InlineShapes.AddPicture(imagepath); 
         myOtherFind.Wrap = word.WdFindWrap.wdFindContinue; 
         myOtherFind.Execute(Replace: word.WdReplace.wdReplaceAll); 
        } 

        // 'Now search all next stories of other stories (doc.storyRanges dont seem to cascades in sub story) 
        word.Range nextStoryRange = otherStoryRange.NextStoryRange; 
        while (nextStoryRange != null) 
        { 
         word.Find myNextStoryFind = nextStoryRange.Find; 
         myNextStoryFind.Text = Findtext; 
         myNextStoryFind.Replacement.Application.Selection.InlineShapes.AddPicture(imagepath); 
         myNextStoryFind.Wrap = word.WdFindWrap.wdFindContinue; 
         myNextStoryFind.Execute(Replace: word.WdReplace.wdReplaceAll); 

         nextStoryRange = nextStoryRange.NextStoryRange; 
        } 

       } 
       app.Documents.Save(); 
       app.Documents.Close(); 
      } 

     } 


} 

回答

1

Replacement.Application是對應用程序對象的引用。當您調用AddPicture()時,即使執行查找操作,圖片也立即插入當前位置。

我看到兩種可能性:

  1. 負載的圖片,將其放置到Windows剪貼板中,然後執行查找操作指定「^ C」作爲替換文本。 Word將用剪貼板的當前內容替換「^ c」。這是documentation這樣說:

ReplaceWith
類型:System.Object
可選的對象。
替換文本。要刪除Find參數指定的文本,請使用空字符串(「」)。您可以像爲Find參數一樣指定特殊字符和高級搜索條件。要指定圖形對象或其他非文本項目作爲替換項目,請將項目移至剪貼板併爲ReplaceWith指定「^ c」。

  • 不要使用wdReplaceAll,但wdReplaceNone,使查找操作本身並不做任何更換。但是你有機會在你找到的地方插入你的內容。直到沒有發現更多事件爲止。