Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/257.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何从.NET中的MS Word访问图像名称?_C#_.net_Ms Word_Word Automation - Fatal编程技术网

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

C# 如何从.NET中的MS Word访问图像名称?,c#,.net,ms-word,word-automation,C#,.net,Ms Word,Word Automation,我试图在C#中自动执行一项任务,在word(docx)文件中循环浏览图像,并根据图像名称(在选择窗格下重命名)更改图像。 我似乎找不到在哪里访问图像的name属性 测试代码: using System; using System.Collections.Generic; using Word = Microsoft.Office.Interop.Word; namespace wordReplace { class Program { private stati

我试图在C#中自动执行一项任务,在word(docx)文件中循环浏览图像,并根据图像名称(在选择窗格下重命名)更改图像。 我似乎找不到在哪里访问图像的name属性

测试代码:

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);
            }
        }
    }
}
使用系统;
使用System.Collections.Generic;
使用Word=Microsoft.Office.Interop.Word;
名称空间字替换
{
班级计划
{
私有静态字应用程序;
私有静态对象yes=true;
私有静态对象no=false;
缺少私有静态对象=System.Reflection.missing.Value;
静态void Main(字符串[]参数)
{
尝试
{
app=新单词.Application();
app.Visible=true;
Word.d号文件;
对象文件名=@“C:\test.docx”;
d=应用程序文档。打开(参考文件名、参考缺失、参考编号、参考缺失、,
参考缺失,参考缺失,参考缺失,参考缺失,参考缺失,参考缺失,
参考缺失、参考缺失、参考是、参考缺失、参考缺失、参考缺失、参考缺失);
列表范围=新列表();
foreach(d.InlineShapes中的Word.InlineShapes)
{
//需要访问此处的图像名称属性!!
}
应用程序退出(参考号、参考号缺失、参考号缺失);
}
捕获(异常x)
{
控制台写入线(x.Message);
应用程序退出(参考号、参考号缺失、参考号缺失);
}
}
}
}

图形标题存储为字段,如下所示。您需要遍历字段,查找字段代码“SEQ Figure*ARABIC”


我知道它很旧,但也许有人会用它。 InlineShape的标题属性在选择窗格中设置了图像名称。
例如:

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

谢谢你的回答,但我不知道这是怎么回答这个问题的?我正在寻找图像名称(显示在选择窗格中:主功能区->编辑部分->选择->选择窗格…)