Jump to content
Simpel

[work around] $oActiveWorkbook.UserStatus not working

Recommended Posts

Simpel
Posted (edited)

Hi.

I try to figure out who is using a excel workbook which I can only open "read only". I use this code:

#include <Array.au3>
#include <Excel.au3>
Local $sFile = ; excel file with path on a network drive

Local $oExcel = _Excel_Open(True, True)
Local $oTabelle = _Excel_BookOpen($oExcel, $sFile)
Local $aUsers
If IsObj($oTabelle) Then
    $aUsers = $oTabelle.UserStatus
    _ArrayDisplay($aUsers)
EndIf

If I am the one allowed to write to the excel file (I'm the first one who opened it) then I will get an array with myself:

UserStatus_Array.PNG.610210eb1edd63ae9956f14166a3fbcb.PNG

If my collegue opened the excel file first and I run the code I get the following error message:

"H:\_Conrad lokal\Downloads\AutoIt3\_COX\Tests\test.au3" (9) : ==> The requested action with this object has failed.:
$aUsers = $oTabelle.UserStatus
$aUsers = $oTabelle^ ERROR

The excel file is on a network drive. Is that's the problem?

Regards, Conrad

Edited by Simpel
[work around]

SciTE4AutoIt = 3.7.3.0   AutoIt = 3.3.14.2   AutoItX64 = 0   OS = Win7Pro SP1   OSArch = X64   Language = 0407/german
H:\...\AutoIt3\SciTE     H:\...\AutoIt3      H:\...\AutoIt3\Include     (H:\ = Network Drive)

   88x31.png  Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind.

Share this post


Link to post
Share on other sites
water

You need to add some kind of error checking to your script:

#include <Array.au3>
#include <Excel.au3>
Local $aUsers, $sFile = ; excel file with path on a network drive
Local $oExcel = _Excel_Open(True, True)
If @error Then Exit MsgBox(0, "Error", "Error " & @error & " opening Excel!")
Local $oTabelle = _Excel_BookOpen($oExcel, $sFile)
If @error Then Exit MsgBox(0, "Error", "Error " & @error & " opening Workbook!")
If @extended = 1 Then ; Workbook could not be opened as read/write
    $aUsers = $oTabelle.UserStatus
    _ArrayDisplay($aUsers)
EndIf

 


My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2018-06-01 - Version 1.4.9.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (2018-01-27 - Version 1.3.3.1) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

Tutorials:
ADO - Wiki

 

Share this post


Link to post
Share on other sites
Simpel

Hi @water,

currently I can't try it out (in the evening I can) but I think I will run into this error too. As I wrote I run into this error if the excel file is opened read only by me. Than @extended will be 1 and then $oTabelle.UserStatus will fail with object $oTabelle. Or am I wrong?

Conrad


SciTE4AutoIt = 3.7.3.0   AutoIt = 3.3.14.2   AutoItX64 = 0   OS = Win7Pro SP1   OSArch = X64   Language = 0407/german
H:\...\AutoIt3\SciTE     H:\...\AutoIt3      H:\...\AutoIt3\Include     (H:\ = Network Drive)

   88x31.png  Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind.

Share this post


Link to post
Share on other sites
water

Seems property UserStatus does not work the way we expect it to work.
Details and a solution can be found here: https://stackoverflow.com/questions/15555522/how-to-tell-if-an-excel-2007-spreadsheet-is-open-and-who-has-it-open-using-vbscr


My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2018-06-01 - Version 1.4.9.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (2018-01-27 - Version 1.3.3.1) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

Tutorials:
ADO - Wiki

 

Share this post


Link to post
Share on other sites
Simpel

Thanks @water for this hint. I couldn't find anything about it. This is my whole solution now:

#include <File.au3>
Local $sFile = ; fullpath to an excel file
Local $sOpened = _ExcelFileOpened($sFile)
If @error Then ; nobody has opened this excel file
    If @error = 1 Then
        ConsoleWrite("You can open the excel file and write to it." & @CRLF)
    ElseIf @error = 2 Then
        ConsoleWrite("File does not exist." & @CRLF)
    ElseIf @error = 3 Then
        ConsoleWrite("File ist not an excel file." & @CRLF)
    EndIf
ElseIf @extended = 1 Then
    ConsoleWrite("This excel file is already opened by you with write privilege" & @CRLF)
Else
    ConsoleWrite("This excel file is opened by " & $sOpened & @CRLF & "You can't write to the file therefor it will not be opened." & @CRLF)
EndIf
Exit


; #FUNCTION# ====================================================================================================================
; Name ..........: _ExcelFileOpened
; Description ...: Returns who has the excel file opened with write privilege
; Syntax ........: _ExcelFileOpened($sFile)
; Parameters ....: $sFile - must be an excel file
; Return values .: Success - name of who has opened the excel file with write privilege, sets @extended to:
;                  |1 - if I am the one
;                  Failure - 0, sets @error to:
;                  |1 - excel file is not opened
;                  |2 - file does not exist
;                  |3 - file is not an excel file
; Author ........: Simpel
; Modified ......:
; Remarks .......:
; Related .......:
; Link ..........:
; Example .......: No
; ===============================================================================================================================
Func _ExcelFileOpened($sFile)
    ; check if file exists
    If FileExists($sFile) = 0 Then Return SetError(2, 0, 0)
    ; check if file is an excel file
    Local $iDelimiter = StringInStr($sFile, ".", 0, -1) ; splits suffix
    Local $sExtension = StringTrimLeft($sFile, $iDelimiter) ; only file extension
    If StringLeft($sExtension, 3) <> "xls" Then Return SetError(3, 0, 0)
    ; if excel has created a temporary excel file with a ~$ prefix then it is already opened
    $iDelimiter = StringInStr($sFile, "\", 0, -1) ; splits file from path
    Local $sTempFile = StringLeft($sFile, $iDelimiter) & "~$" & StringTrimLeft($sFile, $iDelimiter) ; adds prefix ~$ at excel filename
    Local $iTempFileExist = FileExists($sTempFile) ; if this file exists then the excel file is opened
    If $iTempFileExist = 0 Then Return SetError(1, 0, 0) ; is not opened
    Local $sOwnerTempFile = _Owner($sTempFile) ; owner of the excel temp file is the one with write privilege
    ; find out who I am
    Local $sTestFile = _TempFile() ; needs file.au3
    FileWrite($sTestFile, "") ; create a test file (I am definitely the owner)
    If @error Then
        Return $sOwnerTempFile ; returns only the one with write privilege to the excel file
    EndIf
    Local $sMe = _Owner($sTestFile) ; this is me
    FileDelete($sTestFile)
    ; look if I am the one created the temp excel file
    If $sOwnerTempFile = $sMe Then
        Return SetError(0, 1, $sMe) ; returns me opened excel file with write privilege and sets @extended to 1
    Else
        Return $sOwnerTempFile ; returns the one with write privilege to the excel file
    EndIf
EndFunc

Func _Owner($sFile) ; the one who saved it last - code by siao?
    Local $secUtil = ObjCreate("ADsSecurityUtility")
    Local $secDesc = $secUtil.GetSecurityDescriptor($sFile, 1, 1)
    Local $sOwner = $secDesc.Owner
    $secUtil = Null
    Return $sOwner
EndFunc

Regards, Conrad


SciTE4AutoIt = 3.7.3.0   AutoIt = 3.3.14.2   AutoItX64 = 0   OS = Win7Pro SP1   OSArch = X64   Language = 0407/german
H:\...\AutoIt3\SciTE     H:\...\AutoIt3      H:\...\AutoIt3\Include     (H:\ = Network Drive)

   88x31.png  Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind.

Share this post


Link to post
Share on other sites
water

Looks great!
Will test as soon as I find some spare time :)


My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2018-06-01 - Version 1.4.9.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (2018-01-27 - Version 1.3.3.1) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

Tutorials:
ADO - Wiki

 

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Similar Content

    • nooneclose
      By nooneclose
      My program has to first search for names in Column D that do not match up with column C. I got that search to work using arrays. It was slow and I could not figure out how to delete them so I just manually put coded the names that do not belong. I found their cell location but I do not know how to store that location and delete it.
      This is what I have so far.
      Local $NameToDelete1[6]  = _Excel_RangeFind($OpenWorkbook, "Smith, Bill") _ArrayDisplay($NameToDelete1, "Excel UDF: _Excel_RangeFind Example 1", "", 0, "|", "Sheet|Name|Cell|Value|Formula|Comment") _Excel_RangeDelete($OpenWorkbook.ActiveSheet, $NameToDelete1[2], $xlShiftUp)  
      Please help, I wanted to have this program done yesterday but I did not see this problem until yesterday. 
    • nooneclose
      By nooneclose
      I need to perform a subtotal in excel and I would like to automate this process using Autoit if possible like always any and all help will be greatly appreciated. 
      I can not find a good example but the two from Microsoft. Here is one of the two from msdn.microsoft.com/en-us/vba/excel-vba/articles/range-subtotal-method-excel
      I do not really understand how to translate this into AutoIt, but I gave it a try and here is what I have.
      $OpenRange      = "A1:E200" $xlSum          = -4157 $Added_Array[2] = [2, 3] $OpenRange.Subtotal("B1", $xlSum, $Added_Array, True, False, True) I just need to perform a subtotal on a range based on a header called department, and then perform a sum on the results.
    • nooneclose
      By nooneclose
      How to use _Excel_RangeSort to sort my excel file by three different headers Column A1, B1, and C1 have headers on which I want to sort by. The headers on which I want to sort are department, employee type, and name.
      I still really new to AutoIt so I do not actually know how to properly start this line or lines of code, to be honest. The example code is the best I can do.
      _Excel_RangeSort($OpenWorkbook, Default, "A1:C1", "1:1", $xlDescending, Default, $xlYes, Default, $xlSortRows) I just need to sort by those three headers in that order of department, employee type, and name, plus in descending order.
       
      any and all help would be greatly appreciated.  Thank you!
    • Daniza
      By Daniza
      It works fine on my Excel 2007, but after I Emailed My Area Manager he told me after he Enable Macro Security nothing happen's, can someone test this to run on Excel 2016? My AM can't provide me more information 

      Thank You in Advance,
      Please see attachment.
      <snip>
    • tuffgong
      By tuffgong
      Good morning. I have a system I am trying to automate that works like this: user fills a column in an Excel spreadsheet with values they would like printed and saves it to a folder on their desktop, they start the script and it formats their data into a text file (adding a prefix) and sends the text file as a .bch file where it needs to go. This is working:
      #include <Array.au3> #include <Excel.au3> #include <File.au3> #include <MsgBoxConstants.au3> Global $sSTCArray Global $sFilename = @DesktopDir & "\Labels\print.txt" Global $sWorkbook = @DesktopDir & "\Labels\Labels.xlsx" Global $oExcel = _Excel_Open(False,False,False,False,True) barcodePrint() Func barcodePrint() $Read = _Excel_BookOpen($oExcel, $sWorkbook, True, False, Default, Default, Default) FileOpen($sFilename, $FO_OVERWRITE) ;Global $oWorkbook = _Excel_BookAttach($oExcel) Global $sSTCArray = _Excel_RangeRead($Read) For $i = 0 to UBound($sSTCArray, 1) - 1 FileWriteLine($sFilename, "!StaticShelving1x3_ZPL," & $sSTCArray[$i]) Next _Excel_Close($oExcel) FileMove($sFilename, "***file path***\print.bch") EndFunc However, it only works if the user first formats the spreadsheet to text. I want to automate that. From what I have read it appears AutoIt does not like formatting cells that already have values. True? Can I pull the values from an un-formatted (default GENERAL format) spreadsheet and go straight to my text file? I have also considered opening a second spreadsheet, formatting it, and copying the values over. Like this:
        
      Global $sPrefix Global $oPath Global $sSTCArray Global $sFilename = @DesktopDir & "\Labels\print.txt" Global $sWorkbook = @DesktopDir & "\Labels\Labels.xlsx" Global $oExcel = _Excel_Open(False,False,False,False,True) Global $aArray Global $bExcel _Excel_BookNew($bExcel) While 1 $nMsg = GUIGetMsg() Switch $nMsg Case $GUI_EVENT_CLOSE Exit Case $Button1 $aArray = IniReadSection("***File path***\barcode.ini", "stc/rvt/mgm") $sPrefix = $aArray[1][1] $oPath = $aArray[2][1] barcodePrint() EndSwitch WEnd Func barcodePrint() $oWorkbook = _Excel_BookOpen($bExcel, @DesktopDir & "\Labels\print.xlsx") $oWorkbook.ActiveSheet.Columns("A").NumberFormat = "@" Local $Read = _Excel_BookOpen($oExcel, $sWorkbook, True, False, Default, Default, Default) $oCopy = _Excel_RangeRead($sWorkbook) _Excel_RangeWrite($oWorkbook, Default, $oCopy) FileOpen($sFilename, $FO_OVERWRITE) $sSTCArray = _Excel_RangeRead($Read,"Default","Default",3) ;_ArrayDisplay($sSTCArray) For $i = 0 to UBound($sSTCArray, 1) - 1 FileWriteLine($sFilename, $sPrefix & $sSTCArray[$i]) Next _Excel_Close($oExcel) ;FileMove($sFilename, $oPath) Exit EndFunc This does not like the formatting of $oWorkbook: "Variable must be of type 'Object'".  Do I need this second sheet? If so, how can I format it? Is there a better way to get the Excel values into a .txt file? Any ideas would be appreciated. Thanks!
×