AC2Designs Posted February 9, 2010 Share Posted February 9, 2010 If a file gets selected and is in the Filename textbox, it does not matter if you choose "OK" or "Cancel" the name still gets based to my code. it does not matter if I click "OK" or "Cancel" what matters is whether there is anything in the filename box or not. Is there a way around this? Or do I need to read the click event somehow to test whether "OK" or "Cancel" was clicked? How would I do this? Thanks for your help, Kevin Quote Link to comment Share on other sites More sharing options...
MSasu Posted February 10, 2010 Share Posted February 10, 2010 It will be useful to post an excerpt from your code for debugging. However the code below works correctly on my workstation: CommonDialog1.FileName = "C:\MyTestFile.txt" CommonDialog1.Filter = "Text Files (*.TXT) |*.txt" & "|All Files (*.*)|*.*" CommonDialog1.FilterIndex = 1 CommonDialog1.DialogTitle = "Select a file " CommonDialog1.ShowOpen MsgBox CommonDialog1.FileName Regards, Quote Link to comment Share on other sites More sharing options...
AC2Designs Posted February 10, 2010 Author Share Posted February 10, 2010 AC2 Designs, I LOve you!!! Quote Link to comment Share on other sites More sharing options...
goran Posted February 10, 2010 Share Posted February 10, 2010 Try with this: Sub OpenFile() On Error GoTo errHan ' Your code ' ' ' Exit errHan: If Err.Number = 32755 Then 'Cancel Unload Me ElseIf Err.Number = 75 Then MsgBox "File " & fName & " is open" Unload Me End If End Sub Quote Link to comment Share on other sites More sharing options...
AC2Designs Posted February 10, 2010 Author Share Posted February 10, 2010 I do not think I was clear on my original post. My code works fine, and I do not get any errors. The problem is when prompted for a Filename, there is no way to cancel out. ex. When I click "Cancel" in my commomdialog, if there is a Filename in the FileName textbox, the Filename still gets passed to commondialog1.filename I guess what I need, is a way to determine which button I click, "OK" or "Cancel" Is that possible? Kevin Quote Link to comment Share on other sites More sharing options...
MSasu Posted February 11, 2010 Share Posted February 11, 2010 From your post and signature I presume that you are using VBA (VSTA isn’t supported under AutoCAD 2008). For this reason my proposed example is based on VBA and probably wasn't clear enough in my first post. So, what component are you using for Open box? Should be "Microsoft Common Dialog Control 6.0". On a new form add two buttons and a CommonDialog control and paste the code below. Private Sub CommandButton1_Click() Dim SelectedFile As String CommonDialog1.FileName = "C:\MyTestFile.txt" CommonDialog1.ShowOpen [color=red]SelectedFile = CommonDialog1.FileName [/color] MsgBox SelectedFile End Sub Private Sub CommandButton2_Click() Dim SelectedFile As String CommonDialog1.FileName = "C:\MyTestFile.txt" [color=red]SelectedFile = CommonDialog1.FileName[/color] CommonDialog1.ShowOpen MsgBox SelectedFile End Sub As you can see it depends where in code you are getting the file name; the first approach is the right one (I don’t mean that it is the only one) and should solve your issue. Regards, Quote Link to comment Share on other sites More sharing options...
MSasu Posted February 11, 2010 Share Posted February 11, 2010 My code works fine, and I do not get any errors. Regarding @goran’s suggestion, usage of error handler can be handy even when you don’t have a code error (mistake) – sometime may deliberate cause an error just to test it and act accordingly; it is a quite common programming practice (i.e. try / except coding). Only one comment, on CommonDialog’s boxes the error isn’t raised by default for Cancel usage, so need to explicitly force it by using: CommonDialog1.CancelError = True Regards, Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.