【问题标题】:get list of opened Excel documents in C#?获取 C# 中打开的 Excel 文档的列表?
【发布时间】:2015-02-05 19:23:20
【问题描述】:

这里我成功获取Word文档打开文件列表..

        try
        {
            Microsoft.Office.Interop.Word.Application WordObj;
            WordObj = (Microsoft.Office.Interop.Word.Application)System.Runtime.InteropServices.Marshal.GetActiveObject("Word.Application");

                x = "";
                for (int i = 0; i < WordObj.Windows.Count; i++)
                {
                    object idx = i + 1;
                    Microsoft.Office.Interop.Word.Window WinObj = WordObj.Windows.get_Item(ref idx);

                    // doc_list.Add(WinObj.Document.FullName);


                    x = x + "," + WinObj.Document.FullName;

                    //x = WinObj.Document.FullName;
                }                                                         

        }
        catch (Exception ex)
        {

            // No documents opened
        }

就像我想要获取 Excel 文件列表一样...

        try
        {
             Microsoft.Office.Interop.Excel.Application ExcelObj;
             ExcelObj = (Microsoft.Office.Interop.Excel.Application)System.Runtime.InteropServices.Marshal.GetActiveObject("Excel.Application");
             //excel = (Excel.Application)Marshal.GetActiveObject("Excel.Application");
                x = "";
                for (int i = 0; i < ExcelObj.Windows.Count; i++)
                {
                    object idx = i + 1;
                    Microsoft.Office.Interop.Excel.Window WinObj = ExcelObj.Windows.get_Item(idx);

                    doc_list.Add(WinObj.Document.FullName);

                  //Here is the problem ,How can i get FullName of opened excel file

                  // x = x + "," + WinObj.Activate.

                  //  x = WinObj.Document.FullName;
                }                  
        }   
        catch (Exception ex)
        {
        }

Excel 文件没有得到..这条线.. 在每个word文档中都很好..with WinObj.Document.Fullname 但是 在 Excel x = x + "," + WinObj.Document.FullName; 没有 Excel 文件名文档的属性...我怎样才能获得与 word 相同的文件全名..

【问题讨论】:

  • 每次空抓异常,小猫就会死去。
  • 请注意,Excel 2010 到 2013(ish)之间的模型发生了变化——您的目标是什么?
  • 我正在使用 Excel 2010..我想显示打开的 excel 打开的文档文件名..有没有可能找到这种方式..?

标签: c# winforms


【解决方案1】:

我在stackoverflow上找到了这个

你可以试试。

//Excel Application Object
Microsoft.Office.Interop.Excel.Application oExcelApp;

this.Activate ( );

//Get reference to Excel.Application from the ROT.
oExcelApp = ( Microsoft.Office.Interop.Excel.Application ) System.Runtime.InteropServices.Marshal.GetActiveObject ( "Excel.Application" );

//Display the name of the object.
MessageBox.Show ( oExcelApp.ActiveWorkbook.FullName );

//Release the reference.
oExcelApp = null;

我在这里找到了这个 How to get the current open documents in Excel using C#?

这里

http://support2.microsoft.com/kb/316126

【讨论】:

  • 它工作正常...谢谢...Kevinkrs ,你能给我建议的Power point Applicin 是否与相同的要求相同...?是否可以获得PPT文件的实例...?
猜你喜欢
  • 2011-01-27
  • 1970-01-01
  • 1970-01-01
  • 2013-10-08
  • 1970-01-01
  • 1970-01-01
  • 2021-10-23
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多