dchaosw360 Posted October 15, 2019 Share Posted October 15, 2019 (edited) I'm struggling to figure out what I'm doing wrong with this GUI. The premise is that the user needs to save a file that they have open in Excel, and a file with the same name already exists in the save location. They have 3 choices: Overwrite the existing file with the same name (Code will delete the existing file in the save location and then save the open file) Create a new name for the file so the one with the existing name is preserved. Do not save the file The way the GUI is intended to work is that if they hit the button "Overwrite" the application will continue, delete the current file with the same name and save the open file using the same name of the deleted file. If they just hit the enter key in the GUICtrlCreateInput ($idFileName) box it will do exactly the same thing as hitting the "Overwrite" button. Content of the $idFileName control is ignored. If they want to save with a new file name that doesn't exist, I want the GUI to force them to select a file name in the GUICtrlCreateInput ($idFileName) that doesn't currently exists before they can proceed. And here is where I think I'm making the mistake. Do I need to use ContinueSelect here? The result as the code is written is that it saves the file even if a file with the same name exists in the save location, and ends up overwriting it anyway. There is quite a bit of missing code from below (unrelated functions, excel handling etc.) but I think I included everything needed to get the idea of what I'm trying to do. I can post the entire thing if needed. expandcollapse popup#Region Includes #include <MsgBoxConstants.au3> #include <Array.au3> #include <GUIConstantsEx.au3> #include <ColorConstants.au3> #include <String.au3> #include <StringConstants.au3> #include <EditConstants.au3> #include <Excel.au3> #include <ProgressConstants.au3> #include <ListBoxConstants.au3> #include <GUIConstants.au3> #include <WinAPI.au3> #include <Date.au3> #EndRegion Includes ;Save File Local $GUI4HNDL Local $idFileName Local $idGO Local $idNew Local $idCancelSave Local $WatsonID Local $HPEReqID Local $XPOS Local $YPOS Local $oWorkbook HotKeySet("{ESC}", "Terminate") $WatsonID = "IN2-45966-00" $HPEReqID = "HPE-00022206" $XPOS = 500 $YPOS = 500 ;Note for testing purposes - The file "G:\Sales\Allshare\HP Storage\IN2-45966-00 - HPE-00022206.xlsx" does exist ;Save File $GUI4HNDL = GUICreate("Watson Automator", 270, 90, -1, -1, -1, $WS_EX_TOPMOST) GUICtrlCreateLabel("Please enter the name of the quote", 0, 10, 270, 20,$SS_CENTER) GUICtrlCreateLabel("Name: ", 10, 30, 35, 20, $SS_RIGHT) $idFileName = GUICtrlCreateInput("", 50, 30, 210, 20) $idGO = GUICtrlCreateButton("Overwrite", 10, 60, 70) GUICtrlSetColor($idGO, $COLOR_RED) $idNew = GUICtrlCreateButton("New Name", 100, 60, 70) $idCancelSave = GUICtrlCreateButton("Cancel", 190, 60, 70) Local $aAccelKeys[1][2] = [["{ENTER}", $idFileName]] GUISetAccelerators($aAccelKeys, $GUI4HNDL) WinMove ($GUI4HNDL, "", $XPOS, $YPOS) GUICtrlSetData ($idFileName, $WatsonID & " - " & $HPEReqID) GUISetState(@SW_SHOW,$GUI4HNDL) $CancelSave = "" $idMsg = 0 BlockInput($BI_ENABLE) While 1 $idMsg = GUIGetMsg () Select Case $idMsg = $idGO ;Overwrite File ExitLoop Case $idMsg = $idFileName ;Overwrite File (Enter Key Hit - Default is overwrite - $idFileName GUICtrlRead will not be used) ExitLoop Case $idMsg = $idCancelSave ;Don't save file $CancelSave = "Yes" ExitLoop Case $idMsg = $idNew ;New File, but check to make sure doesn't exist first - If it does exist continue checking user input $FileName = "G:\Sales\AllShare\HP Storage\" & GUICtrlRead($idFileName) & ".xlsx" If FileExists ($FileName) Then MsgBox (262144,"File Exists", "File by that name already exists.") Else ExitLoop EndIf EndSelect WEnd BlockInput($BI_DISABLE) ;If GUICTLRRead of $idFileName = starting values then it is an existing file and we want to overwrite it. Delete file to prep to resave same name If GUICtrlRead($idFileName) = $WatsonID & " - " & $HPEReqID Then While FileExists ("G:\Sales\AllShare\HP Storage\" & $WatsonID & " - " & $HPEReqID & ".xlsx") FileDelete ("G:\Sales\AllShare\HP Storage\" & $WatsonID & " - " & $HPEReqID & ".xlsx") Sleep (50) WEnd EndIf If $CancelSave <> "Yes" Then $FileName = GUICtrlRead($idFileName) ;Note for purposes of this test a workbook is already open and ready to be saved $sWorkbook = "G:\Sales\AllShare\HP Storage\" & $FileName _Excel_BookSaveAs($oWorkbook, $sWorkbook, $xlWorkbookDefault, True) If @error Then BlockInput($BI_ENABLE) MsgBox($MB_SYSTEMMODAL, "Excel UDF: _Excel_BookSaveAs Example 1", "Error saving workbook to '" & $sWorkbook & "'." & @CRLF & "@error = " & @error & ", @extended = " & @extended) BlockInput($BI_DISABLE) EndIf EndIf Func Terminate() Exit EndFunc ;==>Terminate ;Continue Code Edited October 15, 2019 by dchaosw360 Link to comment Share on other sites More sharing options...
spudw2k Posted October 15, 2019 Share Posted October 15, 2019 (edited) Your fourth parameter in the _Excel_BookSaveAs function is True (overwrite). Perhaps this is why the overwrite is happening? Also, it appears your {Enter} key Accelerator is mapped to the $idFileName control. Wouldn't it make more sense to map it to the $idGO control and remove the Case statement for $idFileName? Edited October 15, 2019 by spudw2k Spoiler Things I've Made: Always On Top Tool ◊ AU History ◊ Deck of Cards ◊ HideIt ◊ ICU ◊ Icon Freezer ◊ Ipod Ejector ◊ Junos Configuration Explorer ◊ Link Downloader ◊ MD5 Folder Enumerator ◊ PassGen ◊ Ping Tool ◊ Quick NIC ◊ Read OCR ◊ RemoteIT ◊ SchTasksGui ◊ SpyCam ◊ System Scan Report Tool ◊ System UpTime ◊ Transparency Machine ◊ VMWare ESX Builder Misc Code Snippets: ADODB Example ◊ CheckHover ◊ Detect SafeMode ◊ DynEnumArray ◊ GetNetStatData ◊ HashArray ◊ IsBetweenDates ◊ Local Admins ◊ Make Choice ◊ Recursive File List ◊ Remove Sizebox Style ◊ Retrieve PNPDeviceID ◊ Retrieve SysListView32 Contents ◊ Set IE Homepage ◊ Tickle Expired Password ◊ Transpose Array Projects: Drive Space Usage GUI ◊ LEDkIT ◊ Plasma_kIt ◊ Scan Engine Builder ◊ SpeeDBurner ◊ SubnetCalc Cool Stuff: AutoItObject UDF ◊ Extract Icon From Proc ◊ GuiCtrlFontRotate ◊ Hex Edit Funcs ◊ Run binary ◊ Service_UDF Link to comment Share on other sites More sharing options...
pixelsearch Posted October 15, 2019 Share Posted October 15, 2019 Hi dchaosw360, It should be simpler using a FileSaveDialog() function with these parameters : => init dir : G:\Sales\AllShare\HP Storage\ => filter : *.xlsx => options : BitOr($FD_PROMPTOVERWRITE, $FD_PATHMUSTEXIST) => default name : $WatsonID & " - " & $HPEReqID Why not trying the help file example, maybe you'll like it FrancescoDiMuro 1 Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now