我正在编写一个软件,它需要用图像或文本替换图像。我找到了一些代码来用它工作正常的图像替换图像。我想调整这段代码,以便我也可以用文本替换图像。我知道有更好的方法可以做到这一点,但我特别需要使用 Interlope 来完成。任何帮助,将不胜感激。
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
}
}