2009-07-15 15 views
6

C#と単語の自動化を使用して、単語文書のテキストと画像を変更しようとしています。私は以下のスニペットのようなものをテキストの上でうまく動作させていますが、イメージの置き換えを開始する方法はわかりません。Wordオートメーション:C#を使用して画像を置換する

ご協力いただきありがとうございます。

オリバー

using Microsoft.Office.Interop.Word; 
... 

private static Application WordApp; 
private static object missing = System.Reflection.Missing.Value; 
private static object yes = true; 
private static object no = false; 

... 
object search; 
object replace; 

object replaceAll = 
    Microsoft.Office.Interop.Word.WdReplace.wdReplaceAll; 

object filename = SourceFile; 
object destination = DestinationFile; 

Document d = WordApp.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); 

d.Activate(); 
search = "OLDSTRING"; 
replace = "NEWSTRING"; 
WordApp.Selection.Find.Execute(
    ref search, ref yes, ref yes, ref no, ref no, ref no, ref yes, 
    ref missing, ref missing, ref replace, ref replaceAll, 
    ref missing, ref yes, ref missing, ref missing); 
+0

おさるの写真を交換してください。これは、Wordアドイン内で公開している機能か、外部アプリケーションから何らかの種類のバッチ自動化機能ですか?外部の場合、私はむしろ2007年のOpenXML SDKを介してWordMLをオーバーライドするか、代わりに2003の場合はxmlを使いこなすことになります。はるかに迅速かつ簡単に行うことができます。 –

+3

ちょうど提案。 VBを使用します。 COM Automationの土地での生活が楽になります。画像を置き換える場合は、「範囲」を調べて、画像オブジェクトの種類を探しているテキストやオブジェクトを繰り返し処理することができます。 –

+0

@roygbiv:+1。 COMオートメーションではVBがはるかに簡単です – shahkalpesh

答えて

8

あなたはInlineShapesをループができるとあなたが作るしようとしているソリューションの種類を

using System.Collections.Generic; 
using Word = Microsoft.Office.Interop.Word; 

namespace WordExample 
{ 
    class WordExample 
    { 
     #region Constructor 
     public WordExample() 
     { 
      WordApp = new Microsoft.Office.Interop.Word.Application(); 
     } 
     #endregion 

     #region Fields 
     private Word.Application WordApp; 
     private object missing = System.Reflection.Missing.Value; 
     private object yes = true; 
     private object no = false; 
     private Word.Document d; 
     private object filename = @"C:\FullPathToFile\example.doc"; 
     #endregion 

     #region Methods 
     public void UpdateDoc() 
     { 
      d = WordApp.Documents.Open(ref filename, ref missing, ref no, ref missing, 
       ref missing, ref missing, ref missing, ref missing, ref missing, 
       ref missing, ref missing, ref yes, ref missing, ref missing, ref missing, ref missing); 
      List<Word.Range> ranges = new List<Microsoft.Office.Interop.Word.Range>(); 
      foreach (Word.InlineShape s in d.InlineShapes) 
      { 
       if (s.Type == Microsoft.Office.Interop.Word.WdInlineShapeType.wdInlineShapePicture) 
       { 
        ranges.Add(s.Range); 
        s.Delete(); 
       } 
      } 
      foreach (Word.Range r in ranges) 
      { 
       r.InlineShapes.AddPicture(@"c:\PathToNewImage\Image.jpg", ref missing, ref missing, ref missing); 
      } 
      WordApp.Quit(ref yes, ref missing, ref missing); 
     } 
     #endregion 
} 
} 
+1

Excelent code! しかし、ループにピクチャを識別できるかどうか? これは、私がオートメーションプロジェクトに1つのイメージを置き換える必要があるためです。 –

+0

これは本当に古くなっていますが、本当に形を区別できる唯一の方法は、カスタム情報を追加することです。ハイパーリンクを追加してアドレスを使うか、 'Title'や' AlternativeText'のプロパティを使うことができます( 'Alt Text'の' Format Picture'ダイアログで見ることができます)。 – Chris

関連する問題