Word Automation:使用C#替换图像

时间:2009-07-15 01:02:19

标签: c# image automation ms-word replace

我正在尝试使用c#和word自动化更改word文档中的文本和图像。我已经让它在文本中工作得很好,我做了类似下面的代码片段,但我甚至不知道如何开始替换图像。

非常感谢任何帮助!

奥利弗

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);

3 个答案:

答案 0 :(得分: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 :(得分:0)

您要替换形状还是内嵌形状?这是一个很大的区别!

对于内联形状,网上有很多示例。 对于形状,您可以这样:

    private object missing = System.Reflection.Missing.Value;
    .....other code.....



        foreach (Microsoft.Office.Interop.Word.Shape s in wordApp.ActiveDocument.Shapes)
        {

            if (s.AlternativeText.ToUpper().Contains("FOTO"))
            {
                object A = s.Anchor;
                Shape new = Brief.Shapes.AddPicture(@"mynewpicture.jpg", ref missing, ref missing, ref missing, ref missing, ref missing, ref missing,ref A);
                new.Top = s.Top;
                new.Left = s.Left;
                new.Width = s.Width;
                new.Height = s.Height;
                s.Delete();
            }
        }

答案 2 :(得分:0)

我只是从@Mario Favere复制一些路径,并使其更加简单

  • 首先创建空白图片并将图片插入Word文档中

enter image description here

  • 第二次需要在图片上单击鼠标右键添加Alt Text

enter image description here

private object missing = System.Reflection.Missing.Value;
.....other code.....

// Change Image

foreach (Microsoft.Office.Interop.Word.Shape s in wordApp.ActiveDocument.Shapes)
{

    if (s.AlternativeText.ToUpper().Contains("POTO"))
    {
        s.Fill.UserPicture(@"PATH");                       
    }
}