2015-05-21 42 views
2

我想在C#中自动执行一个任务,借此循环访问单词(docx)文件中的图像,并根据图像名称更改图像(在选择窗格下重命名)。 我似乎无法找到在哪里访问图像的名称属性?如何从C#.NET中的MS Word访问图像名称?

测试代码:

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

回答

0

该图字幕被存储为字段,如下所示。你需要通过寻找域代码“SEQ图* ARABIC”

enter image description here

+0

感谢您的答复,但我没有看到如何回答这个问题?我正在查找图像名称(因为它出现在选择窗格中:主页功能区 - >编辑部分 - >选择 - >选择窗格...) – jk777

0

我知道这是旧的,但也许有人会用它的字段进行迭代。 InlineShape的Title属性在选择窗格中设置了图像名称。
例子:

 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); 
      } 
     } 
相关问题