How to identify between ok and cancel button in inputbox

Viewed 8780

Hi Hi I have to write a code where if the user clicks enters something in the input box it should proceed further.If it doesnot enter any value it should throw back the same question again again.This i have already achieved,but my problem is when user click on CANCEl it agains asks the same question whereas it ishould exit .I am very new to VB Script .Plz help me how to handle these buttons?Below is my existing code

Do while x=0
strAnswer = InputBox("Please enter the file extension  *  For all files:", _
    "File Extension")
If strAnswer = "" Then
        MsgBox"You must enter an extension."

Else

        a=strAnswer
        Exit Do
    End If
Loop


intRow = 2
'strFileName = "T:\public\Madhumita\New.xls"
Set objExcel = CreateObject("Excel.Application")
objExcel.Visible = True
Set objWorkbook = objExcel.Workbooks.Add()
'objWorkbook.SaveAs(strFileName)
objExcel.Cells(1, 1).Value = "Folder"
objExcel.Cells(1, 2).Value = "File Name"
objStartFolder = "T:\public\Madhumita\Madhu"
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFolder = objFSO.GetFolder(objStartFolder)
Set colFiles = objFolder.Files
If a="*" Then
For Each objFile in colFiles
objExcel.Cells(intRow, 1).Value = objfolder.Name
objExcel.Cells(intRow, 2).Value = objFile.Name
intRow = intRow + 1
Next

else

For Each objFile in colFiles
m=objFSO.GetExtensionName( objFile.Path )

If m=a Then

objExcel.Cells(intRow, 1).Value = objfolder.Name
objExcel.Cells(intRow, 2).Value = objFile.Name
intRow = intRow + 1


End If
Next
End If 
objExcel.Range("A1:B1").Select
objExcel.Selection.Font.Bold = True
objExcel.Cells.EntireColumn.AutoFit
Sub SaveAs() 
    Application.Dialogs(xlDialogSaveAs).Show 
End Sub 


objExcel.Quit
MsgBox "Done"
3 Answers

For InputBox(), you can use the default value to determine if the user clicked Cancel or if they clicked OK or hit Enter to continue without entering a value:

Sub Get_TIN()
   TIN = Trim(InputBox("Enter the provider TIN:", "Provider TIN", "ex. 123456789"))

   If TIN = "" Then    'When CANCEL is clicked because "TIN" will be empty.
      MsgBox "You pressed Cancel. Program will now end.", vbExclamation + vbOKOnly, "Macro End"
      Exit Sub
   End If

   If IsEmpty(TIN) = False Then     'When OK is clicked or Enter pressed because default text will be stored. Next, set TIN to "".
      TIN = ""
   End If
End Sub
Related