我正在尝试在 C# 中自动执行一项任务,借此循环遍历 word (docx) 文件中的图像并根据图像名称(在选择 Pane 下重命名)更改图像。 我似乎找不到从哪里访问图像的名称属性?
测试代码:
using System;
using System.Collections.Generic;
using Word = Microsoft.Office.Interop.Word;
namespace wordReplace
{
class Program
{
private static Word.Application app;
private static object yes = true;
private static object no = false;
private static object missing = System.Reflection.Missing.Value;
static void Main(string[] args)
{
try
{
app = new Word.Application();
app.Visible = true;
Word.Document d;
object filename = @"C:\test.docx";
d = app.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<Word.Range>();
foreach (Word.InlineShape s in d.InlineShapes)
{
//need to access the image name property here!!
}
app.Quit(ref no, ref missing, ref missing);
}
catch (Exception x)
{
Console.WriteLine(x.Message);
app.Quit(ref no, ref missing, ref missing);
}
}
}
}
最佳答案
我知道它很旧,但也许有人会用它。
InlineShape 的标题属性在选择 Pane 中设置了图像名称。
示例:
Microsoft.Office.Interop.Word.Application wordApplication = null;
Microsoft.Office.Interop.Word.Documents wordDocuments = null;
Microsoft.Office.Interop.Word.Document wordDocument = null;
try
{
wordApplication = new Microsoft.Office.Interop.Word.Application();
wordDocuments = wordApplication.Documents;
wordDocument = wordDocuments.Open(documentPath);
foreach(Microsoft.Office.Interop.Word.InlineShape inlineShape in wordDocument.InlineShapes)
{
if (inlineShape.Title.Contains(imageTitleCriteria)) inlineShape.Delete();
}
}
catch(Exception)
{
}
finally
{
if (wordDocument != null)
{
wordDocument.Close(false);
Marshal.ReleaseComObject(wordDocument);
}
if (wordDocuments != null)
{
wordDocuments.Close(false);
Marshal.ReleaseComObject(wordDocuments);
}
if (wordApplication != null)
{
wordApplication.Quit(false);
Marshal.ReleaseComObject(wordApplication);
}
}
关于c# - 如何在 C#.NET 中从 MS Word 访问图像名称?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30377492/