【问题标题】:how to check if file is opened in excel using OLE (leaves excel process open)如何使用 OLE 检查文件是否在 excel 中打开(保持 excel 进程打开)
【发布时间】:2011-08-24 13:38:58
【问题描述】:

如何检查某个文件是否已在某些 Excel 实例中打开?

我使用 DXL (DOORS) 语言,但它应该独立于该语言。

我可以调用任何 OLE 方法来检查打开的文件并将其与路径/文件名进行比较吗?

如果可能的话,我可以只关闭那个 Excel 应用程序中的那个工作表/文件吗?

编辑: 这是我到目前为止得到的,这有效,但只有一次。 DXL 使 Excel.exe 进程保持打开状态,并在接下来检查使用的实例没有打开的工作簿,甚至根本没有窗口。

        if (confirm "File \"" fPath "\" already exists. Do you want to overwrite it?") {

        // check if file is opened in any Excel instance
        OleAutoObj oleWorkbooks     = null;
        OleAutoObj oleExcel         = null;
        OleAutoObj oleWorkbook      = null;
        OleAutoArgs autoArgs = create;

        oleExcel = oleGetAutoObject("Excel.Application");
        bool opened = false;
        // if excel is opened
        if(oleExcel != null){
            d("Excel is opened");
            // Get workbooks and open file
            oleGet(oleExcel,"Workbooks", oleWorkbooks);

            // compare each open workbook
            int count = 0;
            oleGet(oleWorkbooks,"Count", count);
            string workbookname = "";
            string sPath = replace(fPath, "\\", "/");
            sPath = stripPath(sPath, true);

            while (count > 0) {
                d("checking opened document");
                clear(autoArgs);
                put(autoArgs, count);
                oleGet(oleWorkbooks,"Item", autoArgs, oleWorkbook);
                oleGet(oleWorkbook, "Name", workbookname);
                opened = sPath == workbookname;
                if(opened) {
                    if(confirm "The file is currently opened in Excel. It must be closed. Do you want to close the document?") {
                        clear(autoArgs);
                        oleMethod(oleWorkbook,"Close",autoArgs);
                    }
                    break;  
                }
                count--;
            }
        }
        oleCloseAutoObject(oleExcel);
        oleCloseAutoObject(oleWorkbooks);
        oleCloseAutoObject(oleWorkbook);
        // todo leaves excel process open

        if(!opened) {
            streamOutputData = write fPath;
            streamOutputData << sOutput;
            close streamOutputData;
            return true;    
        }
    }

【问题讨论】:

    标签: excel ole ibm-doors


    【解决方案1】:

    使用现有的 DXL 方法解决了它 canOpenFile(string path, bool write):

    if (confirm "File \"" fPath "\" already exists. Do you want to overwrite it?") {
            if(canOpenFile(fPath, true)) {
                streamOutputData = write fPath;
                streamOutputData << sOutput;
                close streamOutputData;
                return true;    
            }
            else {
                e("File \"" fPath "\" is opened in another program. Close it! (It's probably Excel ;) )");
            }
        }
    

    【讨论】:

    • 您可以通过单击答案左侧的勾号来接受您自己的答案,这样您的问题就会以某种方式结束
    【解决方案2】:

    我不知道 DXL,但这是你可以在 VBA 中做的,我让你适应 DXL:

    Sub IsXLBookOpen(strName As String) 
    
         'Procedure designed to test if a specific Excel
         'workbook is open or not.
    
        Dim i As Long, XLAppFx As Excel.Application 
    
         'Find/create an Excel instance
        On Error Resume Next 
        Set XLAppFx = GetObject(, "Excel.Application") 
        If Err.Number = 429 Then 
            Set XLAppFx = CreateObject("Excel.Application") 
            Err.Clear 
        End If 
        On Error GoTo 0
    
         'Loop through all open workbooks in such instance
        For i = 1 To XLAppFx.Workbooks.Count
            If XLAppFx.Workbooks(i).Name = strName Then
               MsgBox("The workbook is open")
               'Close the workbook and ask if user wants to save
               XLAppFx.Workbooks(i).Close
               'Force close and save changes
               XLAppFx.Workbooks(i).Close True
        Next i 
    End Sub
    

    改编自here

    【讨论】:

      猜你喜欢
      • 2015-11-28
      • 2022-11-22
      • 2015-08-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多