Word Automation:使用 C# 替换图像

发布于 2024-07-27 04:41:22 字数 1084 浏览 4 评论 0原文

我正在尝试使用 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);

I'm trying to change text and images in a word document using c# and word automation. I've got it working fine for text where I do something like the snippet below, but I don't even know how to start for replacing the image.

Any help is greatly appreciated!

Oliver

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

ぃ弥猫深巷。 2024-08-03 04:41:22

您可以循环遍历 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
 }
}

You can loop through the InlineShapes and replace the pictures

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
 }
}
爱情眠于流年 2024-08-03 04:41:22

我只是从@Mario Favere复制一些路径并使其更容易

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

< img src="https://i.sstatic.net/sFuhI.png" alt="在此处输入图像描述">

  • 第二个需要添加 Alt 文本,右键单击图片

在此处输入图像描述

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

I just copy some path from @Mario Favere and make more easy

  • First create Blank Picture and Insert Picture into word Document

enter image description here

  • Second need to add Alt Text right click on Picture

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");                       
    }
}
∝单色的世界 2024-08-03 04:41:22

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

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

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

Do you want to replace a shape or inline shape ? This is a big difference!

For an inline shape there are plenty of examples on the net.
For a shape you can do like this :

    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();
            }
        }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文