2011-08-24 75 views
1

我可以如何檢查文件是否在某些Excel實例已經打開?如何檢查,如果文件是在Excel中使用OLE打開(葉Excel進程打開)

我使用DXL(門)的語言,但它應該從這種語言是獨立的。

有任何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;  
     } 
    } 

回答

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

2

解決它使用現有DXL方法canOpenFile(字符串路徑,布爾寫):

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 ;))"); 
     } 
    } 
+0

你可以通過點擊答案左邊的勾號來接受你自己的答案這樣你的問題就會以某種方式關閉了 – JMax