Sign in to follow this  
Followers 0
KenNichols

If Not FileExists / ElseIf FileExists

3 posts in this topic

Is there a way to loop this, so everytime you enter a file name that exists it brings up another InputBox?

#include <Word.au3>

Global $CustName = "ABC Company Inc"

Global $sFilePath = @ScriptDir & "\New Microsoft Word document.doc"

Global $oWordApp = _WordCreate($sFilePath,"",0)

Global $oDoc = _WordDocGetCollection($oWordApp, 0)

$oDoc.Range.Text = "This is some text."

$FileName = StringLeft($CustName,30)

If Not FileExists(@ScriptDir & "\" & $FileName & ".doc") Then

_WordDocSaveAs ($oDoc, @ScriptDir & "\" & $FileName & ".doc")

_WordQuit ($oWordApp)

ElseIf FileExists(@ScriptDir & "\" & $FileName & ".doc") Then

$NewFileName = InputBox("Error: File Exists","Please input a new file name!",StringLeft($CustName,30),"",250,125)

$error = @error

If $error = 1 Then ;<==Cancel was pressed

_WordQuit ($oWordApp,0)

EndIf

$OldFileName = $NewFileName

If FileExists(@ScriptDir & "\" & $NewFileName & ".doc") Then ; <=== Is there a way to loop this?

$NewFileName = InputBox("Error: File Exists","THE FILE WILL BE OVERWRITTEN!",$OldFileName,"",250,125)

$error = @error

If $error = 1 Then ;<==Cancel was pressed

_WordQuit ($oWordApp,0)

EndIf

EndIf

$FileName = $NewFileName & ".doc"

_WordDocSaveAs ($oDoc, @ScriptDir & "\" & $FileName)

_WordQuit ($oWordApp)

EndIf


[topic="21048"]New to AutoIt? Check out AutoIt 1-2-3![/topic] Need to make a GUI? You NEED KODA FormDesigner!

Share this post


Link to post
Share on other sites



#include <Word.au3>
Global $CustName = "ABC Company Inc"
Global $sFilePath = @ScriptDir & "\New Microsoft Word document.doc"

Global $oWordApp = _WordCreate($sFilePath,"",0)
Global $oDoc = _WordDocGetCollection($oWordApp, 0)

$oDoc.Range.Text = "This is some text."

$FileName = StringLeft($CustName,30)

If Not FileExists(@ScriptDir & "\" & $FileName & ".doc") Then
_WordDocSaveAs ($oDoc, @ScriptDir & "\" & $FileName & ".doc")
_WordQuit ($oWordApp)

ElseIf FileExists(@ScriptDir & "\" & $FileName & ".doc") Then
$NewFileName = InputBox("Error: File Exists","Please input a new file name!",StringLeft($CustName,30),"",250,125)
$error = @error
If $error = 1 Then ;<==Cancel was pressed
_WordQuit ($oWordApp,0)
EndIf
$OldFileName = $NewFileName

Do
    $NewFileName = InputBox("Error: File Exists","THE FILE WILL BE OVERWRITTEN!",$OldFileName,"",250,125)
    If @error Then
        _WordQuit($oWordApp,0)
        Exit
    EndIf
Until Not FileExists(@ScriptDir & "\" & $NewFileName & ".doc")

$FileName = $NewFileName & ".doc"
_WordDocSaveAs ($oDoc, @ScriptDir & "\" & $FileName)
_WordQuit ($oWordApp)
EndIf

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