【问题标题】:get the path and name of the docx that is currently running c#获取当前正在运行的 docx 的路径和名称 c#
【发布时间】:2017-06-21 03:31:26
【问题描述】:

我正在为 word 编写一个插件,在下面的代码中,我将文件中的所有单词放入字典中。

`
   Dictionary<string, string> motRap = new Dictionary<string, string>();
   Microsoft.Office.Interop.Word.Application application = new Microsoft.Office.Interop.Word.Application();
   Document document = application.Documents.Open("monfichiertxt.docx");

        // Loop through all words in the document.
        int count = document.Words.Count;
        for (int i = 1; i <= count; i++)
        {
            // Write the word.
            string text = document.Words[i].Text;
            //motRapport.Add(text);
            motRap.Add(text, "blabla");
        }
        // Close word.
        application.Quit();

我想要当前正在运行的 docx 的文件名,而不是写“monfichiertxt.docx”。

有人可以帮我吗 谢谢。

【问题讨论】:

标签: c# ms-word vsto add-in docx


【解决方案1】:

您可以像这样使用Name 属性或FullName 属性

string name = document.Name;
string fullName = document.FullName;

Name 将为您提供“MyDocument.docx”,FullName 将为您提供“...\MyFolder\MyDocument.docx”

【讨论】:

  • 谢谢你,这正是我想要的
【解决方案2】:

这个问题之前已经处理过,从未尝试过,1 google,2行代码,我得到了一个简单的答案。虽然作为一个插件,我没想到你需要这样做来获得你已经属于的单词 instance..

    Microsoft.Office.Interop.Word.Application word = System.Runtime.InteropServices.Marshal.GetActiveObject("Word.Application") as Microsoft.Office.Interop.Word.Application;
    label1.Text = word.ActiveDocument.Name;

【讨论】:

  • 您必须知道,我们没有像您一样轻松找到问题/问题答案的所有能力。感谢您的回答,但这不适用于我的情况。
猜你喜欢
  • 2011-07-08
  • 2020-10-20
  • 2021-10-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-01-11
  • 1970-01-01
相关资源
最近更新 更多