c# - 如何在 C#.NET 中从 MS Word 访问图像名称?

标签 c# .net ms-word word-automation

我正在尝试在 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/

相关文章:

excel - 从 Word 中提取作者和评论到 Excel

PHP 清理粘贴的 Microsoft 输入

vba - 找到所有标题 1 的文本并将其放入一个数组中

c# - 创建用于上传图像的 ViewModel 的正确方法是什么?

c# - 测试两个 IEnumerable<T> 是否具有相同频率的相同值

c# - 我的简单应用程序中的无效转换异常

c# - C# 中的 XML 序列化

c# - 如何装饰依赖运行时值创建的类

c# - 我想要什么 WinForm 控件?

.net - 如何判断流编写器是否已关闭?