Sign in to follow this  
Followers 0
Dezypher

FileExists

8 posts in this topic

Hello all

I was wondering how to do this...i have played around for a few days trying to get it to work but i cant seem to get it to do just what i want. what i would like is to check if a file exist and if it does open it, and if it does not then create it. this seams simple enough but i, for the life of me, cant seem to get it to work... i have tried if then else statements, select case statements, tried putting in @error to select different statements depending on what error i set..but nothing is working the way i need it to. what i seem to get is if the file does not exist then it will create it...but if the file exist then it opens the file and then runs the rest of the code to create the file and errors. any help / suggestions would be great...here are some of the code that i have tried.

#include <date.au3>
dim $dailysave, $error
$oExcel = ObjCreate("Excel.Application")
$oExcel.visible=1

If not FileExists("c:\users\keith.000\documents\dailyUtilization1a"&@mday&@mon&@year&".xlsm") then SetError(2)

    MsgBox(4096,"test", guictrlread($error))
    MsgBox(4096, "test", GUICtrlRead(@error))
;$error=@error
    MsgBox(4096, "test", GUICtrlRead($error))
    MsgBox(4096, "test", @error -2)
Select
    Case @error = 2
        $oExcel.WorkBooks.open("dailyUtilization1a"&@mday&@mon&@year&".xlsm")
    Case @error > 2
        $oExcel.WorkBooks.open("C:\Users\keith.000\Documents\Work Intergration\dailyUtilization1.xltm")
        $oExcel.ActiveWorkBook.SaveAs ("dailyUtilization1a"&@mday&@mon&@year&".xlsm", "52");, "_FileFormat:=xlOpenXMLWorkbookMacroEnabled", "_CreateBackup:=False");, _FileFormat:=xlNormal, Password:="", WriteResPassword:="", _ReadOnlyRecommended:=False, CreateBackup:=False)
    ;$oExcel.ActiveWorkBook.ActiveSheet.Cells(2,1).Value=@MDAY
        $oExcel.ActiveWorkBook.Save
        $oExcel.ActiveWorkBook.Close
        $oExcel.Quit
EndSelect

#include <date.au3>
dim $dailysave
$oExcel = ObjCreate("Excel.Application")
$oExcel.visible=1
If FileExists("dailyUtilization1a"&@mday&@mon&@year&".xlsm") then 
            $oExcel.WorkBooks.open("dailyUtilization1a"&@mday&@mon&@year&".xlsm")
        Elseif not FileExists("dailyUtilization1a"&@mday&@mon&@year&".xlsm") then
    ;Else
            $oExcel.WorkBooks.open("C:\Users\keith.000\Documents\Work Intergration\dailyUtilization1.xltm")
            $oExcel.ActiveWorkBook.SaveAs ("dailyUtilization1a"&@mday&@mon&@year&".xlsm", "52");, "_FileFormat:=xlOpenXMLWorkbookMacroEnabled", "_CreateBackup:=False");, _FileFormat:=xlNormal, Password:="", WriteResPassword:="", _ReadOnlyRecommended:=False, CreateBackup:=False)
        ;$oExcel.ActiveWorkBook.ActiveSheet.Cells(2,1).Value=@MDAY
            $oExcel.ActiveWorkBook.Save
            $oExcel.ActiveWorkBook.Close
            $oExcel.Quit
    EndIf

#include <date.au3>
$oExcel = ObjCreate("Excel.Application")
$oExcel.visible=1
$open = Test_Excel_Creat()
Func Test_Excel_Creat()
    If not FileExists("dailyUtilization1a"&@mday&@mon&@year&".xlsm")Then SetError(2)
    MsgBox(4096, "test", @error)
    MsgBox(4096,"test", @error <= 2)
    if @error <= 2 Then 
        $oExcel.WorkBooks.open("C:\Users\keith.000\Documents\Work Intergration\dailyUtilization1.xltm")
        $oExcel.ActiveWorkBook.SaveAs ("dailyUtilization1a"&@mday&@mon&@year&".xlsm", "52");, "_FileFormat:=xlOpenXMLWorkbookMacroEnabled", "_CreateBackup:=False");, _FileFormat:=xlNormal, Password:="", WriteResPassword:="", _ReadOnlyRecommended:=False, CreateBackup:=False)
    ;$oExcel.ActiveWorkBook.ActiveSheet.Cells(2,1).Value=@MDAY
        $oExcel.ActiveWorkBook.Save
        $oExcel.ActiveWorkBook.Close
        $oExcel.Quit
    EndIf
    If @error

    
EndFunc

as you can see i have tried several different ways to accomplish this simple task and they have all resulted in what i described above.

Share this post


Link to post
Share on other sites



#include <date.au3>
dim $dailysave
$oExcel = ObjCreate("Excel.Application")
$oExcel.visible=1

$theFile = "dailyUtilization1a"&@mday&@mon&@year&".xlsm"

If FileExists($theFile) then 
    $oExcel.WorkBooks.open($theFile)
Else

    $oExcel.WorkBooks.open("C:\Users\keith.000\Documents\Work Intergration\dailyUtilization1.xltm")
    $oExcel.ActiveWorkBook.SaveAs ($theFile, "52");, "_FileFormat:=xlOpenXMLWorkbookMacroEnabled", "_CreateBackup:=False");, _FileFormat:=xlNormal, Password:="", WriteResPassword:="", _ReadOnlyRecommended:=False, CreateBackup:=False)
    ;$oExcel.ActiveWorkBook.ActiveSheet.Cells(2,1).Value=@MDAY
    $oExcel.ActiveWorkBook.Save
    $oExcel.ActiveWorkBook.Close
    $oExcel.Quit
EndIf

Share this post


Link to post
Share on other sites

#include <date.au3>
dim $dailysave
$oExcel = ObjCreate("Excel.Application")
$oExcel.visible=1

$theFile = "dailyUtilization1a"&@mday&@mon&@year&".xlsm"

If FileExists($theFile) then 
    $oExcel.WorkBooks.open($theFile)
Else

    $oExcel.WorkBooks.open("C:\Users\keith.000\Documents\Work Intergration\dailyUtilization1.xltm")
    $oExcel.ActiveWorkBook.SaveAs ($theFile, "52");, "_FileFormat:=xlOpenXMLWorkbookMacroEnabled", "_CreateBackup:=False");, _FileFormat:=xlNormal, Password:="", WriteResPassword:="", _ReadOnlyRecommended:=False, CreateBackup:=False)
    ;$oExcel.ActiveWorkBook.ActiveSheet.Cells(2,1).Value=@MDAY
    $oExcel.ActiveWorkBook.Save
    $oExcel.ActiveWorkBook.Close
    $oExcel.Quit
EndIf

thanks for the update here but this give me the same thing...i open the file after its created and then it tries to save it again. being that the only save here is teh $oExcel.ActiveWorkBook.save i can only assume that this code is running after the file is created....let me modify this a bit to check for different things and try to figure out where its breaking at...and why this code is running....this is the issue that i have been having all along

thanks for the help though...:)

any other ideas?

Share this post


Link to post
Share on other sites

In all of your examples you care calling SaveAs and Save.

Share this post


Link to post
Share on other sites

In all of your examples you care calling SaveAs and Save.

ah yes ...a bit of older code the saveas...let me change that and see what happens.

Share this post


Link to post
Share on other sites

ah yes ...a bit of older code the saveas...let me change that and see what happens.

#include <date.au3>
dim $dailysave
$oExcel = ObjCreate("Excel.Application")
$oExcel.visible=1

$theFile = "dailyUtilization1a"&@mday&@mon&@year&".xlsm"
MsgBox(4096, "test", $theFile)

If FileExists($theFile) then 
    $oExcel.WorkBooks.open($theFile)
Else

    $oExcel.WorkBooks.open("C:\Users\keith.000\Documents\Work Intergration\dailyUtilization1.xltm")
    $oExcel.ActiveWorkBook.SaveAs ($theFile, "52");, "_FileFormat:=xlOpenXMLWorkbookMacroEnabled", "_CreateBackup:=False");, _FileFormat:=xlNormal, Password:="", WriteResPassword:="", _ReadOnlyRecommended:=False, CreateBackup:=False)
   ;$oExcel.ActiveWorkBook.ActiveSheet.Cells(2,1).Value=@MDAY
   ;$oExcel.ActiveWorkBook.Save
    $oExcel.ActiveWorkBook.Close
    $oExcel.Quit
EndIf

ok correct me here but in this code excution if the file exist then we should only open the file and not close it out...and if the file does not exist then we should open excel using the file specified and then save it. what i am getting is if the file does not exist then we create it...if the file does exist then we open and try to save it...almost like the open is not working and jumping strait to the save as...

Share this post


Link to post
Share on other sites

Aren't you using "$oExcel.WorkBooks.open" to open a template and save a copy?

Share this post


Link to post
Share on other sites

#include <date.au3>
dim $dailysave
$oExcel = ObjCreate("Excel.Application")
$oExcel.visible=1

$theFile = "dailyUtilization1a"&@mday&@mon&@year&".xlsm"
MsgBox(4096, "test", $theFile)

If FileExists($theFile) then 
    $oExcel.WorkBooks.open($theFile)
Else

    $oExcel.WorkBooks.open("C:\Users\keith.000\Documents\Work Intergration\dailyUtilization1.xltm")
    $oExcel.ActiveWorkBook.SaveAs ($theFile, "52");, "_FileFormat:=xlOpenXMLWorkbookMacroEnabled", "_CreateBackup:=False");, _FileFormat:=xlNormal, Password:="", WriteResPassword:="", _ReadOnlyRecommended:=False, CreateBackup:=False)
  ;$oExcel.ActiveWorkBook.ActiveSheet.Cells(2,1).Value=@MDAY
  ;$oExcel.ActiveWorkBook.Save
    $oExcel.ActiveWorkBook.Close
    $oExcel.Quit
EndIf

ok correct me here but in this code excution if the file exist then we should only open the file and not close it out...and if the file does not exist then we should open excel using the file specified and then save it. what i am getting is if the file does not exist then we create it...if the file does exist then we open and try to save it...almost like the open is not working and jumping strait to the save as...

ok if i modify the "$theFile = "dailyUtilization1a"&@mday&@mon&@year&".xlsm"" to point to the file with the full path it seems to work :)

thanks again let me play with it and stuff now ^_^

thanks a lot again :)

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
Sign in to follow this  
Followers 0