2012-05-07 11 views
0

findオブジェクトを使用すると、文書内の単語を置き換える方法がわかりました。今私は、ワード文書全体を通して、単語 "logo"を実際のロゴイメージに置き換えることに苦労しています。テキストをWord文書の画像で検索して置換する

は、私は、ドキュメント内の各範囲とをループがロゴが範囲内に発見された単語場合、私がやって絵を追加することができると言うことができます考え出し:

foreach (Range rngStory in doc.StoryRanges) 
{     
    rngStory.InlineShapes.AddPicture(filename); 
} 

の問題は、この意志範囲の上部に画像を追加します。テキストのロゴがどこにあるのか正確には分かりません。

誰でもこれを行うには良い解決策がありますか?

答えて

0

http://msdn.microsoft.com/en-us/library/microsoft.office.interop.word.inlineshapes.addpicture%28v=office.11%29.aspx

範囲

オプションのオブジェクトオプションの範囲パラメータを参照してください。写真が置かれる場所は です。範囲が折りたたまれていない場合、画像は の範囲に置き換えられます。それ以外の場合は、画像が挿入されます。この引数が でない場合、画像は自動的に配置されます。

だから私はあなたがあなたのロゴタグを持って現在位置を照会言うと、範囲値としてあることを使用します。

+0

おかげで上の画像にテキストを置き換えます。それは私のループの場合のように見える、Rangeは実際には文書のテキストの非常に大きなチャンクです - 範囲に画像を追加するだけですべてのテキストの前に置いている - 私は必要と思います範囲を指定することですどういうわけか? – Stian

+0

AddPictureを返すとShapeが返されます。この[link]を参照してください(http://msdn.microsoft.com/en-us/library/microsoft.office.interop.word.shape%28v=office.11​​%29.aspx )、 'RelativeHorizo​​ntalPosition'と 'RelativeVerticalPosition'プロパティの説明を読んでください –

1

以下の方法でこれを行うことができます。このコードは、テキストを正確な位置に置き換えます。

 Word.Application wordApp = new Word.Application(); 
     Microsoft.Office.Interop.Word.Document doc = wordApp.Documents.Open(ref outputFile, ref oMissing, 
        ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, 
        ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, 
        ref oMissing, ref oMissing, ref oMissing, ref oMissing); 
       doc.Activate(); 
       try 
       { 
        //----------------------Replace-------------------------------- 
        Microsoft.Office.Interop.Word.Find fnd = wordApp.ActiveWindow.Selection.Find; 
        fnd.ClearFormatting(); 
        fnd.Replacement.ClearFormatting(); 
        fnd.Forward = true; 
        fnd.Wrap = Microsoft.Office.Interop.Word.WdFindWrap.wdFindContinue; 

    string imagePath = Server.MapPath("~/data/uploads/violations/resize/Image.jpg"); 
    var keyword = "LOGO"; 
         var sel = wordApp.Selection; 
         sel.Find.Text = string.Format("[{0}]", keyword); 
    wordApp.Selection.Find.Execute(keyword); 

         Word.Range range = wordApp.Selection.Range; 
    if (range.Text.Contains(keyword)) 
          { 
           //gets desired range here it gets last character to make superscript in range 
           Word.Range temprange = doc.Range(range.End - 4, range.End);//keyword is of 4 charecter range.End - 4 
           temprange.Select(); 
           Word.Selection currentSelection = wordApp.Selection; 
           //currentSelection.Font.Superscript = 1; 

           sel.Find.Execute(Replace: WdReplace.wdReplaceOne); 
           sel.Range.Select(); 
           var imagePath1 = Path.GetFullPath(string.Format(imagePath, keyword)); 
           sel.InlineShapes.AddPicture(FileName: imagePath1, LinkToFile: false, SaveWithDocument: true); 
    } 
} 
catch { } 
      finally 
      { 
       if (doc != null) 
       { 
        ((_Document)doc).Close(ref oMissing, ref oMissing, ref oMissing); 
        Marshal.FinalReleaseComObject(doc); 
       } 
       if (wordApp != null) 
       { 
        ((_Application)wordApp).Quit(); 
        Marshal.FinalReleaseComObject(wordApp); 
       } 
      } 
0

それとも、このように:これは、文書の先頭

Word.Application wordApp = new Word.Application(); 
    Microsoft.Office.Interop.Word.Document doc = wordApp.Documents.Open(ref outputFile, ref oMissing, 
       ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, 
       ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, 
       ref oMissing, ref oMissing, ref oMissing, ref oMissing); 
      doc.Activate(); 
      try 
      { 
       //----------------------Replace-------------------------------- 
       Microsoft.Office.Interop.Word.Find fnd = wordApp.ActiveWindow.Selection.Find; 
       fnd.ClearFormatting(); 
       fnd.Replacement.ClearFormatting(); 
       fnd.Forward = true; 
       fnd.Wrap = Microsoft.Office.Interop.Word.WdFindWrap.wdFindContinue; 

string imagePath = Server.MapPath("~/data/uploads/violations/resize/Image.jpg"); 
var keyword = "LOGO"; 
        var sel = wordApp.Selection; 
        sel.Find.Text = string.Format("[{0}]", keyword); 
wordApp.Selection.Find.Execute(keyword); 

        Word.Range range = wordApp.Selection.Range; 


          sel.Find.Execute(Replace: WdReplace.wdReplaceOne); 
          sel.Range.Select(); 
          var imagePath1 = Path.GetFullPath(string.Format(imagePath, keyword)); 
          sel.InlineShapes.AddPicture(FileName: imagePath1, LinkToFile: false, SaveWithDocument: true); 




    } 
    catch (Exception) 
     { 
     } 
     finally 
     { 
      if (doc != null) 
      { 
       ((_Document)doc).Close(ref oMissing, ref oMissing, ref oMissing); 
       Marshal.FinalReleaseComObject(doc); 
      } 
      if (wordApp != null) 
      { 
       ((_Application)wordApp).Quit(); 
       Marshal.FinalReleaseComObject(wordApp); 
      } 
     } 
関連する問題