Word Automation:使用 C# 替换图像
我正在尝试使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以循环遍历 InlineShapes 并替换图片
You can loop through the InlineShapes and replace the pictures
我只是从@Mario Favere复制一些路径并使其更容易
< img src="https://i.sstatic.net/sFuhI.png" alt="在此处输入图像描述">
I just copy some path from @Mario Favere and make more easy
您想要替换形状还是内联形状吗? 这是一个很大的区别!
对于内联形状,网上有很多示例。
对于形状,你可以这样做:
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 :