如何使用OLE检查文件是否在Excel中打开(保持excel进程打开)

时间:2011-08-24 13:38:59

标签: excel ole ibm-doors

如何检查某个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;    
        }
    }

2 个答案:

答案 0 :(得分:2)

使用现有的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 ;) )");
        }
    }

答案 1 :(得分:0)

我不知道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