I am trying to automate a task in C# whereby I loop through images in a word (docx) file and change images based on the image name (renamed under the Selection Pane). I can't seem to find where to access the name property of the image?
Test code:
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);
}
}
}
}
I know it's old but maybe someone will use it. The InlineShape's Title property has the image name set in the Selection Pane.
Example:
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);
}
}
The figure caption are stored as fields as shown below. You need to iterate through fields looking for the field code "SEQ Figure * ARABIC"
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.