2015-05-21 75 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); 
      } 
     } 
相關問題