![]() |
How to make error handle for mis-typed variable?
Have the following situation:
An .xla file will import a Sub from a text file and then run it like this: Dim strSub as String 'some code here to get the Sub imported Application.Run strSub Now it is possible that in this imported Sub are variables that are not defined publicly, so there will be a compile error, variable not defined. As this is not a runtime error I am not sure this error can be handled gracefully, but I would be interested in any suggestions how this could be done. RBS |
How to make error handle for mis-typed variable?
Don't you get the same error if you run it from the workbook that owns the code?
Maybe you could change the original procedure to pass it the parameters that it needs??? RB Smissaert wrote: Have the following situation: An .xla file will import a Sub from a text file and then run it like this: Dim strSub as String 'some code here to get the Sub imported Application.Run strSub Now it is possible that in this imported Sub are variables that are not defined publicly, so there will be a compile error, variable not defined. As this is not a runtime error I am not sure this error can be handled gracefully, but I would be interested in any suggestions how this could be done. RBS -- Dave Peterson |
How to make error handle for mis-typed variable?
I do run it from the workbook that owns the code.
The code is imported in the VBE from a text file. RBS "Dave Peterson" wrote in message ... Don't you get the same error if you run it from the workbook that owns the code? Maybe you could change the original procedure to pass it the parameters that it needs??? RB Smissaert wrote: Have the following situation: An .xla file will import a Sub from a text file and then run it like this: Dim strSub as String 'some code here to get the Sub imported Application.Run strSub Now it is possible that in this imported Sub are variables that are not defined publicly, so there will be a compile error, variable not defined. As this is not a runtime error I am not sure this error can be handled gracefully, but I would be interested in any suggestions how this could be done. RBS -- Dave Peterson |
How to make error handle for mis-typed variable?
Do I conclude then that there is just no solution for this?
RBS "RB Smissaert" wrote in message ... Have the following situation: An .xla file will import a Sub from a text file and then run it like this: Dim strSub as String 'some code here to get the Sub imported Application.Run strSub Now it is possible that in this imported Sub are variables that are not defined publicly, so there will be a compile error, variable not defined. As this is not a runtime error I am not sure this error can be handled gracefully, but I would be interested in any suggestions how this could be done. RBS |
How to make error handle for mis-typed variable?
If they're public variables, maybe you could put them in the same module so that
their declaratations get imported, too. But I think I would try to rewrite the sub so that any variables that it needs are passed as parameters. Then I could just pass the parms I need in the line that calls the sub. RB Smissaert wrote: Do I conclude then that there is just no solution for this? RBS "RB Smissaert" wrote in message ... Have the following situation: An .xla file will import a Sub from a text file and then run it like this: Dim strSub as String 'some code here to get the Sub imported Application.Run strSub Now it is possible that in this imported Sub are variables that are not defined publicly, so there will be a compile error, variable not defined. As this is not a runtime error I am not sure this error can be handled gracefully, but I would be interested in any suggestions how this could be done. RBS -- Dave Peterson |
How to make error handle for mis-typed variable?
I am not sure how that would work.
In it's most simple form this is how the situation is: In Add-in: Public VarABC as String Sub RunImportedSub() Application.Run strImportedSub End Sub In text file: Sub strImportedSub() VarABZ = "test" End Sub Then VarABZ is nowhere declared in the add-in and the compile error occurs. I can't alter the text files as they are with different people in different places. All I can do is alter the add-in to deal with this. RBS "Dave Peterson" wrote in message ... If they're public variables, maybe you could put them in the same module so that their declaratations get imported, too. But I think I would try to rewrite the sub so that any variables that it needs are passed as parameters. Then I could just pass the parms I need in the line that calls the sub. RB Smissaert wrote: Do I conclude then that there is just no solution for this? RBS "RB Smissaert" wrote in message ... Have the following situation: An .xla file will import a Sub from a text file and then run it like this: Dim strSub as String 'some code here to get the Sub imported Application.Run strSub Now it is possible that in this imported Sub are variables that are not defined publicly, so there will be a compile error, variable not defined. As this is not a runtime error I am not sure this error can be handled gracefully, but I would be interested in any suggestions how this could be done. RBS -- Dave Peterson |
How to make error handle for mis-typed variable?
Hi RBS:
Can you give an example of the imported sub where the variable is not declared publicly, giving rise to a compile error? I'm not sure I completely understand the problem. Is strSub a different sub at different times, with different sets of variables? Regards, Vasant "RB Smissaert" wrote in message ... Do I conclude then that there is just no solution for this? RBS "RB Smissaert" wrote in message ... Have the following situation: An .xla file will import a Sub from a text file and then run it like this: Dim strSub as String 'some code here to get the Sub imported Application.Run strSub Now it is possible that in this imported Sub are variables that are not defined publicly, so there will be a compile error, variable not defined. As this is not a runtime error I am not sure this error can be handled gracefully, but I would be interested in any suggestions how this could be done. RBS |
How to make error handle for mis-typed variable?
RBS:
OK, got it now. That's a challenge ... I can't quite figure it out at the moment. Perhaps, as you said, there is no good solution. I guess you're trying to avoid the "Compile error in hidden module" message. I'm not sure how you would handle the error much more gracefully than that anyway, since your add-in is not going to know the exact nature of the error. Regards, Vasant "Vasant Nanavati" <vasantn *AT* aol *DOT* com wrote in message ... Hi RBS: Can you give an example of the imported sub where the variable is not declared publicly, giving rise to a compile error? I'm not sure I completely understand the problem. Is strSub a different sub at different times, with different sets of variables? Regards, Vasant "RB Smissaert" wrote in message ... Do I conclude then that there is just no solution for this? RBS "RB Smissaert" wrote in message ... Have the following situation: An .xla file will import a Sub from a text file and then run it like this: Dim strSub as String 'some code here to get the Sub imported Application.Run strSub Now it is possible that in this imported Sub are variables that are not defined publicly, so there will be a compile error, variable not defined. As this is not a runtime error I am not sure this error can be handled gracefully, but I would be interested in any suggestions how this could be done. RBS |
How to make error handle for mis-typed variable?
I don't understand this sentence:
"Then VarABZ is nowhere declared in the add-in and the compile error occurs." Because you said you had this in the addin: In Add-in: Public VarABC as String == In my simple test, I had this in a text file: Option Explicit Sub test01A() VarABC = "hi" MsgBox VarABC End Sub In my other workbook, I had this in module1: Option Explicit Public VarABC As String Sub ImportTheTextFile() Dim myFileName As String myFileName = "C:\my documents\excel\test1.bas" ThisWorkbook.VBProject.VBComponents.Import myFileName Application.Run ThisWorkbook.Name & "!test01a" End Sub And it worked fine (xl2003). RB Smissaert wrote: I am not sure how that would work. In it's most simple form this is how the situation is: In Add-in: Public VarABC as String Sub RunImportedSub() Application.Run strImportedSub End Sub In text file: Sub strImportedSub() VarABZ = "test" End Sub Then VarABZ is nowhere declared in the add-in and the compile error occurs. I can't alter the text files as they are with different people in different places. All I can do is alter the add-in to deal with this. RBS "Dave Peterson" wrote in message ... If they're public variables, maybe you could put them in the same module so that their declaratations get imported, too. But I think I would try to rewrite the sub so that any variables that it needs are passed as parameters. Then I could just pass the parms I need in the line that calls the sub. RB Smissaert wrote: Do I conclude then that there is just no solution for this? RBS "RB Smissaert" wrote in message ... Have the following situation: An .xla file will import a Sub from a text file and then run it like this: Dim strSub as String 'some code here to get the Sub imported Application.Run strSub Now it is possible that in this imported Sub are variables that are not defined publicly, so there will be a compile error, variable not defined. As this is not a runtime error I am not sure this error can be handled gracefully, but I would be interested in any suggestions how this could be done. RBS -- Dave Peterson -- Dave Peterson |
How to make error handle for mis-typed variable?
OK, I have an add-in with a public variable:
Dim VarABC as string Now a Sub gets imported into the VBE from a text file. This textfile is just completely unrelated to the add-in, so the variables can be anything. Now in this Sub in the textfile is a variable VarABZ. This really should be VarABC, but it isn't. Now when this Sub is imported the variable VarABZ isn't declared anywhere (as the variable should be VarABC) and there will be a compile error. Can't make it any clearer. The more I think about it, the more I think there is no solution for this other than somehow scanning the whole textfile before importing it in the VBE. RBS "Dave Peterson" wrote in message ... I don't understand this sentence: "Then VarABZ is nowhere declared in the add-in and the compile error occurs." Because you said you had this in the addin: In Add-in: Public VarABC as String == In my simple test, I had this in a text file: Option Explicit Sub test01A() VarABC = "hi" MsgBox VarABC End Sub In my other workbook, I had this in module1: Option Explicit Public VarABC As String Sub ImportTheTextFile() Dim myFileName As String myFileName = "C:\my documents\excel\test1.bas" ThisWorkbook.VBProject.VBComponents.Import myFileName Application.Run ThisWorkbook.Name & "!test01a" End Sub And it worked fine (xl2003). RB Smissaert wrote: I am not sure how that would work. In it's most simple form this is how the situation is: In Add-in: Public VarABC as String Sub RunImportedSub() Application.Run strImportedSub End Sub In text file: Sub strImportedSub() VarABZ = "test" End Sub Then VarABZ is nowhere declared in the add-in and the compile error occurs. I can't alter the text files as they are with different people in different places. All I can do is alter the add-in to deal with this. RBS "Dave Peterson" wrote in message ... If they're public variables, maybe you could put them in the same module so that their declaratations get imported, too. But I think I would try to rewrite the sub so that any variables that it needs are passed as parameters. Then I could just pass the parms I need in the line that calls the sub. RB Smissaert wrote: Do I conclude then that there is just no solution for this? RBS "RB Smissaert" wrote in message ... Have the following situation: An .xla file will import a Sub from a text file and then run it like this: Dim strSub as String 'some code here to get the Sub imported Application.Run strSub Now it is possible that in this imported Sub are variables that are not defined publicly, so there will be a compile error, variable not defined. As this is not a runtime error I am not sure this error can be handled gracefully, but I would be interested in any suggestions how this could be done. RBS -- Dave Peterson -- Dave Peterson |
How to make error handle for mis-typed variable?
Hi RBS:
I guess the only way would be to search for the string VarABC in the text file, and not run the sub if it is not found but throw up your own graceful error message :). But I'm assuming that this is a simplified case and the real-life scenario is more complex. Regards, Vasant "RB Smissaert" wrote in message ... OK, I have an add-in with a public variable: Dim VarABC as string Now a Sub gets imported into the VBE from a text file. This textfile is just completely unrelated to the add-in, so the variables can be anything. Now in this Sub in the textfile is a variable VarABZ. This really should be VarABC, but it isn't. Now when this Sub is imported the variable VarABZ isn't declared anywhere (as the variable should be VarABC) and there will be a compile error. Can't make it any clearer. The more I think about it, the more I think there is no solution for this other than somehow scanning the whole textfile before importing it in the VBE. RBS "Dave Peterson" wrote in message ... I don't understand this sentence: "Then VarABZ is nowhere declared in the add-in and the compile error occurs." Because you said you had this in the addin: In Add-in: Public VarABC as String == In my simple test, I had this in a text file: Option Explicit Sub test01A() VarABC = "hi" MsgBox VarABC End Sub In my other workbook, I had this in module1: Option Explicit Public VarABC As String Sub ImportTheTextFile() Dim myFileName As String myFileName = "C:\my documents\excel\test1.bas" ThisWorkbook.VBProject.VBComponents.Import myFileName Application.Run ThisWorkbook.Name & "!test01a" End Sub And it worked fine (xl2003). RB Smissaert wrote: I am not sure how that would work. In it's most simple form this is how the situation is: In Add-in: Public VarABC as String Sub RunImportedSub() Application.Run strImportedSub End Sub In text file: Sub strImportedSub() VarABZ = "test" End Sub Then VarABZ is nowhere declared in the add-in and the compile error occurs. I can't alter the text files as they are with different people in different places. All I can do is alter the add-in to deal with this. RBS "Dave Peterson" wrote in message ... If they're public variables, maybe you could put them in the same module so that their declaratations get imported, too. But I think I would try to rewrite the sub so that any variables that it needs are passed as parameters. Then I could just pass the parms I need in the line that calls the sub. RB Smissaert wrote: Do I conclude then that there is just no solution for this? RBS "RB Smissaert" wrote in message ... Have the following situation: An .xla file will import a Sub from a text file and then run it like this: Dim strSub as String 'some code here to get the Sub imported Application.Run strSub Now it is possible that in this imported Sub are variables that are not defined publicly, so there will be a compile error, variable not defined. As this is not a runtime error I am not sure this error can be handled gracefully, but I would be interested in any suggestions how this could be done. RBS -- Dave Peterson -- Dave Peterson |
How to make error handle for mis-typed variable?
Maybe the only solution is somehow to check the text before importing it in
the VBE, but I am not sure that is worth the trouble. RBS "Vasant Nanavati" <vasantn *AT* aol *DOT* com wrote in message ... RBS: OK, got it now. That's a challenge ... I can't quite figure it out at the moment. Perhaps, as you said, there is no good solution. I guess you're trying to avoid the "Compile error in hidden module" message. I'm not sure how you would handle the error much more gracefully than that anyway, since your add-in is not going to know the exact nature of the error. Regards, Vasant "Vasant Nanavati" <vasantn *AT* aol *DOT* com wrote in message ... Hi RBS: Can you give an example of the imported sub where the variable is not declared publicly, giving rise to a compile error? I'm not sure I completely understand the problem. Is strSub a different sub at different times, with different sets of variables? Regards, Vasant "RB Smissaert" wrote in message ... Do I conclude then that there is just no solution for this? RBS "RB Smissaert" wrote in message ... Have the following situation: An .xla file will import a Sub from a text file and then run it like this: Dim strSub as String 'some code here to get the Sub imported Application.Run strSub Now it is possible that in this imported Sub are variables that are not defined publicly, so there will be a compile error, variable not defined. As this is not a runtime error I am not sure this error can be handled gracefully, but I would be interested in any suggestions how this could be done. RBS |
How to make error handle for mis-typed variable?
Do you have "option explicit" in your text file?
If yes, try removing it. Again, that variable won't be initialized. RB Smissaert wrote: OK, I have an add-in with a public variable: Dim VarABC as string Now a Sub gets imported into the VBE from a text file. This textfile is just completely unrelated to the add-in, so the variables can be anything. Now in this Sub in the textfile is a variable VarABZ. This really should be VarABC, but it isn't. Now when this Sub is imported the variable VarABZ isn't declared anywhere (as the variable should be VarABC) and there will be a compile error. Can't make it any clearer. The more I think about it, the more I think there is no solution for this other than somehow scanning the whole textfile before importing it in the VBE. RBS "Dave Peterson" wrote in message ... I don't understand this sentence: "Then VarABZ is nowhere declared in the add-in and the compile error occurs." Because you said you had this in the addin: In Add-in: Public VarABC as String == In my simple test, I had this in a text file: Option Explicit Sub test01A() VarABC = "hi" MsgBox VarABC End Sub In my other workbook, I had this in module1: Option Explicit Public VarABC As String Sub ImportTheTextFile() Dim myFileName As String myFileName = "C:\my documents\excel\test1.bas" ThisWorkbook.VBProject.VBComponents.Import myFileName Application.Run ThisWorkbook.Name & "!test01a" End Sub And it worked fine (xl2003). RB Smissaert wrote: I am not sure how that would work. In it's most simple form this is how the situation is: In Add-in: Public VarABC as String Sub RunImportedSub() Application.Run strImportedSub End Sub In text file: Sub strImportedSub() VarABZ = "test" End Sub Then VarABZ is nowhere declared in the add-in and the compile error occurs. I can't alter the text files as they are with different people in different places. All I can do is alter the add-in to deal with this. RBS "Dave Peterson" wrote in message ... If they're public variables, maybe you could put them in the same module so that their declaratations get imported, too. But I think I would try to rewrite the sub so that any variables that it needs are passed as parameters. Then I could just pass the parms I need in the line that calls the sub. RB Smissaert wrote: Do I conclude then that there is just no solution for this? RBS "RB Smissaert" wrote in message ... Have the following situation: An .xla file will import a Sub from a text file and then run it like this: Dim strSub as String 'some code here to get the Sub imported Application.Run strSub Now it is possible that in this imported Sub are variables that are not defined publicly, so there will be a compile error, variable not defined. As this is not a runtime error I am not sure this error can be handled gracefully, but I would be interested in any suggestions how this could be done. RBS -- Dave Peterson -- Dave Peterson -- Dave Peterson |
How to make error handle for mis-typed variable?
Ps. Inside the VBE, I have tools|options|editor tab|"require variable
declaration" checked. But when I imported the text file manually into its own module, I didn't get the "Option Explicit" at the top--unless it was part of the text file. I tried importing it via code and that matched, too. === The only way I've seen that undeclared variable message is when I have "option explicit" at the top of the module. Turn it off and no problem. (Well, except for what the code is really doing!) RB Smissaert wrote: OK, I have an add-in with a public variable: Dim VarABC as string Now a Sub gets imported into the VBE from a text file. This textfile is just completely unrelated to the add-in, so the variables can be anything. Now in this Sub in the textfile is a variable VarABZ. This really should be VarABC, but it isn't. Now when this Sub is imported the variable VarABZ isn't declared anywhere (as the variable should be VarABC) and there will be a compile error. Can't make it any clearer. The more I think about it, the more I think there is no solution for this other than somehow scanning the whole textfile before importing it in the VBE. RBS "Dave Peterson" wrote in message ... I don't understand this sentence: "Then VarABZ is nowhere declared in the add-in and the compile error occurs." Because you said you had this in the addin: In Add-in: Public VarABC as String == In my simple test, I had this in a text file: Option Explicit Sub test01A() VarABC = "hi" MsgBox VarABC End Sub In my other workbook, I had this in module1: Option Explicit Public VarABC As String Sub ImportTheTextFile() Dim myFileName As String myFileName = "C:\my documents\excel\test1.bas" ThisWorkbook.VBProject.VBComponents.Import myFileName Application.Run ThisWorkbook.Name & "!test01a" End Sub And it worked fine (xl2003). RB Smissaert wrote: I am not sure how that would work. In it's most simple form this is how the situation is: In Add-in: Public VarABC as String Sub RunImportedSub() Application.Run strImportedSub End Sub In text file: Sub strImportedSub() VarABZ = "test" End Sub Then VarABZ is nowhere declared in the add-in and the compile error occurs. I can't alter the text files as they are with different people in different places. All I can do is alter the add-in to deal with this. RBS "Dave Peterson" wrote in message ... If they're public variables, maybe you could put them in the same module so that their declaratations get imported, too. But I think I would try to rewrite the sub so that any variables that it needs are passed as parameters. Then I could just pass the parms I need in the line that calls the sub. RB Smissaert wrote: Do I conclude then that there is just no solution for this? RBS "RB Smissaert" wrote in message ... Have the following situation: An .xla file will import a Sub from a text file and then run it like this: Dim strSub as String 'some code here to get the Sub imported Application.Run strSub Now it is possible that in this imported Sub are variables that are not defined publicly, so there will be a compile error, variable not defined. As this is not a runtime error I am not sure this error can be handled gracefully, but I would be interested in any suggestions how this could be done. RBS -- Dave Peterson -- Dave Peterson -- Dave Peterson |
How to make error handle for mis-typed variable?
His Vasant,
Yes, it is a very much simplified example. On the other hand this is doable as there is only a limited number of variables that could be missing. All the textfiles are written by myself (up to now) so I know what to expect. I was just hoping for some elegant, simple solution, but it seems there isn't. I suppose the solution to avoid this problem in the future is to give every text file Sub a unique identifier, like a version number or a GUID, so you can check easily that you are dealing with the right text. RBS "Vasant Nanavati" <vasantn *AT* aol *DOT* com wrote in message ... Hi RBS: I guess the only way would be to search for the string VarABC in the text file, and not run the sub if it is not found but throw up your own graceful error message :). But I'm assuming that this is a simplified case and the real-life scenario is more complex. Regards, Vasant "RB Smissaert" wrote in message ... OK, I have an add-in with a public variable: Dim VarABC as string Now a Sub gets imported into the VBE from a text file. This textfile is just completely unrelated to the add-in, so the variables can be anything. Now in this Sub in the textfile is a variable VarABZ. This really should be VarABC, but it isn't. Now when this Sub is imported the variable VarABZ isn't declared anywhere (as the variable should be VarABC) and there will be a compile error. Can't make it any clearer. The more I think about it, the more I think there is no solution for this other than somehow scanning the whole textfile before importing it in the VBE. RBS "Dave Peterson" wrote in message ... I don't understand this sentence: "Then VarABZ is nowhere declared in the add-in and the compile error occurs." Because you said you had this in the addin: In Add-in: Public VarABC as String == In my simple test, I had this in a text file: Option Explicit Sub test01A() VarABC = "hi" MsgBox VarABC End Sub In my other workbook, I had this in module1: Option Explicit Public VarABC As String Sub ImportTheTextFile() Dim myFileName As String myFileName = "C:\my documents\excel\test1.bas" ThisWorkbook.VBProject.VBComponents.Import myFileName Application.Run ThisWorkbook.Name & "!test01a" End Sub And it worked fine (xl2003). RB Smissaert wrote: I am not sure how that would work. In it's most simple form this is how the situation is: In Add-in: Public VarABC as String Sub RunImportedSub() Application.Run strImportedSub End Sub In text file: Sub strImportedSub() VarABZ = "test" End Sub Then VarABZ is nowhere declared in the add-in and the compile error occurs. I can't alter the text files as they are with different people in different places. All I can do is alter the add-in to deal with this. RBS "Dave Peterson" wrote in message ... If they're public variables, maybe you could put them in the same module so that their declaratations get imported, too. But I think I would try to rewrite the sub so that any variables that it needs are passed as parameters. Then I could just pass the parms I need in the line that calls the sub. RB Smissaert wrote: Do I conclude then that there is just no solution for this? RBS "RB Smissaert" wrote in message ... Have the following situation: An .xla file will import a Sub from a text file and then run it like this: Dim strSub as String 'some code here to get the Sub imported Application.Run strSub Now it is possible that in this imported Sub are variables that are not defined publicly, so there will be a compile error, variable not defined. As this is not a runtime error I am not sure this error can be handled gracefully, but I would be interested in any suggestions how this could be done. RBS -- Dave Peterson -- Dave Peterson |
How to make error handle for mis-typed variable?
Does the code fail due to Option Explicit, either in an existing module if
the macro is imported line by line, or top of the text if imported as a new module (per Dave's example). If so the solution seems obvious. Or, is the compile problem due to some attempted usage of the variable, and if so in the imported proc or when passed elsewhere in the project. Regards, Peter T "RB Smissaert" wrote in message ... OK, I have an add-in with a public variable: Dim VarABC as string Now a Sub gets imported into the VBE from a text file. This textfile is just completely unrelated to the add-in, so the variables can be anything. Now in this Sub in the textfile is a variable VarABZ. This really should be VarABC, but it isn't. Now when this Sub is imported the variable VarABZ isn't declared anywhere (as the variable should be VarABC) and there will be a compile error. Can't make it any clearer. The more I think about it, the more I think there is no solution for this other than somehow scanning the whole textfile before importing it in the VBE. RBS "Dave Peterson" wrote in message ... I don't understand this sentence: "Then VarABZ is nowhere declared in the add-in and the compile error occurs." Because you said you had this in the addin: In Add-in: Public VarABC as String == In my simple test, I had this in a text file: Option Explicit Sub test01A() VarABC = "hi" MsgBox VarABC End Sub In my other workbook, I had this in module1: Option Explicit Public VarABC As String Sub ImportTheTextFile() Dim myFileName As String myFileName = "C:\my documents\excel\test1.bas" ThisWorkbook.VBProject.VBComponents.Import myFileName Application.Run ThisWorkbook.Name & "!test01a" End Sub And it worked fine (xl2003). RB Smissaert wrote: I am not sure how that would work. In it's most simple form this is how the situation is: In Add-in: Public VarABC as String Sub RunImportedSub() Application.Run strImportedSub End Sub In text file: Sub strImportedSub() VarABZ = "test" End Sub Then VarABZ is nowhere declared in the add-in and the compile error occurs. I can't alter the text files as they are with different people in different places. All I can do is alter the add-in to deal with this. RBS "Dave Peterson" wrote in message ... If they're public variables, maybe you could put them in the same module so that their declaratations get imported, too. But I think I would try to rewrite the sub so that any variables that it needs are passed as parameters. Then I could just pass the parms I need in the line that calls the sub. RB Smissaert wrote: Do I conclude then that there is just no solution for this? RBS "RB Smissaert" wrote in message ... Have the following situation: An .xla file will import a Sub from a text file and then run it like this: Dim strSub as String 'some code here to get the Sub imported Application.Run strSub Now it is possible that in this imported Sub are variables that are not defined publicly, so there will be a compile error, variable not defined. As this is not a runtime error I am not sure this error can be handled gracefully, but I would be interested in any suggestions how this could be done. RBS -- Dave Peterson -- Dave Peterson |
How to make error handle for mis-typed variable?
Peter, Dave,
That is it! I had Option Explicit at the top of the module that got the code imported. If I leave this off the code will run happily even with un-declared variables. I will now have to figure out what happens when there are these un-declared variables. It causes some strange behaviour or maybe not: If VarNonse = False Then Columns(2).Delete End If This will delete the column, even when the variable is declared nowhere. I suppose this is expected in that un-declared equates to false. In fact this probably is more trouble than getting the compile error as I now don't know if the variable was un-declared or declared but False. Is it possible to check somehow if a variable was declared or not? Thanks again for all the contributions; I learned something there! RBS "RB Smissaert" wrote in message ... Have the following situation: An .xla file will import a Sub from a text file and then run it like this: Dim strSub as String 'some code here to get the Sub imported Application.Run strSub Now it is possible that in this imported Sub are variables that are not defined publicly, so there will be a compile error, variable not defined. As this is not a runtime error I am not sure this error can be handled gracefully, but I would be interested in any suggestions how this could be done. RBS |
How to make error handle for mis-typed variable?
Hi RBS:
IsEmpty(MyVar) will return a False if the variable has been initialized. For Example: Sub Test1() Dim s As String MsgBox IsEmpty(s) End Sub Sub Test2() MsgBox IsEmpty(s) End Sub Hope this helps! Regards, Vasant "RB Smissaert" wrote in message ... Peter, Dave, That is it! I had Option Explicit at the top of the module that got the code imported. If I leave this off the code will run happily even with un-declared variables. I will now have to figure out what happens when there are these un-declared variables. It causes some strange behaviour or maybe not: If VarNonse = False Then Columns(2).Delete End If This will delete the column, even when the variable is declared nowhere. I suppose this is expected in that un-declared equates to false. In fact this probably is more trouble than getting the compile error as I now don't know if the variable was un-declared or declared but False. Is it possible to check somehow if a variable was declared or not? Thanks again for all the contributions; I learned something there! RBS "RB Smissaert" wrote in message ... Have the following situation: An .xla file will import a Sub from a text file and then run it like this: Dim strSub as String 'some code here to get the Sub imported Application.Run strSub Now it is possible that in this imported Sub are variables that are not defined publicly, so there will be a compile error, variable not defined. As this is not a runtime error I am not sure this error can be handled gracefully, but I would be interested in any suggestions how this could be done. RBS |
How to make error handle for mis-typed variable?
First, I should mention I didn't see Dave's slightly earlier and similar
comment re Option Explicit when I posted, or I wouldn't have duplicated. As to your new problem - sounds tricky. If you're not confident about mistyped var's in the imported macro what else could be wrong! Theoretically you could parse the entire text file and look for undeclared var's, a lot of work which I'll leave to you. Or, look for existence of expected Globals, and take action if not there. How about importing into an empty module that's deliberately headed Option Explicit. Warn the user you are about to compile with instructions what to do if it fails, then force a compile: Application.VBE.CommandBars.FindControl(Id:=578).E xecute FWIW, for my own curiosity I wrote a little routine to comment out the Option Explicit in Dave's example. This is the opposite of what I'm now suggesting but it might give ideas how to re-write the macro. Sub test() ' comment "Option Explicit" in a text file named text1.bas ' and save to text2.bas Dim iRead As Integer Dim iWrite As Integer Dim myFileName As String Dim newName As String Dim sInput As String myFileName = "C:\my documents\test1.bas" newName = "C:\my documents\test2.bas" iRead = FreeFile iWrite = iRead + 1 Close #iRead 'just in case open Close #iWrite Open myFileName For Input Access Read As #iRead Open newName For Output Access Write As #iWrite Do While Not EOF(iRead) Line Input #iRead, sInput If Len(sInput) Then If Left$(Trim(sInput), 15) = "Option Explicit" Then sInput = "'" & sInput End If End If Print #iWrite, sInput Loop End Sub Regards, Peter T "RB Smissaert" wrote in message ... Peter, Dave, That is it! I had Option Explicit at the top of the module that got the code imported. If I leave this off the code will run happily even with un-declared variables. I will now have to figure out what happens when there are these un-declared variables. It causes some strange behaviour or maybe not: If VarNonse = False Then Columns(2).Delete End If This will delete the column, even when the variable is declared nowhere. I suppose this is expected in that un-declared equates to false. In fact this probably is more trouble than getting the compile error as I now don't know if the variable was un-declared or declared but False. Is it possible to check somehow if a variable was declared or not? Thanks again for all the contributions; I learned something there! RBS "RB Smissaert" wrote in message ... Have the following situation: An .xla file will import a Sub from a text file and then run it like this: Dim strSub as String 'some code here to get the Sub imported Application.Run strSub Now it is possible that in this imported Sub are variables that are not defined publicly, so there will be a compile error, variable not defined. As this is not a runtime error I am not sure this error can be handled gracefully, but I would be interested in any suggestions how this could be done. RBS |
How to make error handle for mis-typed variable?
Vasant,
Thanks, I didn't know that one. Still it doesn't help me that much. Rember I do: Sub RunningImportedSub() Application.Run strImportedSub End Sub I have no control over what is in strImportedSub. So, how could I have an error handler in Sub RunningImportedSub() that catches the variable in strImportedSub where IsEmpty is True, without causing a compile error? In fact checking all the variables doesn't even catch everything as there could be wrongly named functions and/or subs. RBS "Vasant Nanavati" <vasantn *AT* aol *DOT* com wrote in message ... Hi RBS: IsEmpty(MyVar) will return a False if the variable has been initialized. For Example: Sub Test1() Dim s As String MsgBox IsEmpty(s) End Sub Sub Test2() MsgBox IsEmpty(s) End Sub Hope this helps! Regards, Vasant "RB Smissaert" wrote in message ... Peter, Dave, That is it! I had Option Explicit at the top of the module that got the code imported. If I leave this off the code will run happily even with un-declared variables. I will now have to figure out what happens when there are these un-declared variables. It causes some strange behaviour or maybe not: If VarNonse = False Then Columns(2).Delete End If This will delete the column, even when the variable is declared nowhere. I suppose this is expected in that un-declared equates to false. In fact this probably is more trouble than getting the compile error as I now don't know if the variable was un-declared or declared but False. Is it possible to check somehow if a variable was declared or not? Thanks again for all the contributions; I learned something there! RBS "RB Smissaert" wrote in message ... Have the following situation: An .xla file will import a Sub from a text file and then run it like this: Dim strSub as String 'some code here to get the Sub imported Application.Run strSub Now it is possible that in this imported Sub are variables that are not defined publicly, so there will be a compile error, variable not defined. As this is not a runtime error I am not sure this error can be handled gracefully, but I would be interested in any suggestions how this could be done. RBS |
How to make error handle for mis-typed variable?
Peter,
That might be the best solution. Have a warning in the sub that is going to run the imported sub, saying something if you get an errror that opens the VBE it means you don't have the latest version of text file so and so, please download the newest file from ... etc. How about importing into an empty module that's deliberately headed Option Explicit That is how it always happened. RBS "Peter T" <peter_t@discussions wrote in message ... First, I should mention I didn't see Dave's slightly earlier and similar comment re Option Explicit when I posted, or I wouldn't have duplicated. As to your new problem - sounds tricky. If you're not confident about mistyped var's in the imported macro what else could be wrong! Theoretically you could parse the entire text file and look for undeclared var's, a lot of work which I'll leave to you. Or, look for existence of expected Globals, and take action if not there. How about importing into an empty module that's deliberately headed Option Explicit. Warn the user you are about to compile with instructions what to do if it fails, then force a compile: Application.VBE.CommandBars.FindControl(Id:=578).E xecute FWIW, for my own curiosity I wrote a little routine to comment out the Option Explicit in Dave's example. This is the opposite of what I'm now suggesting but it might give ideas how to re-write the macro. Sub test() ' comment "Option Explicit" in a text file named text1.bas ' and save to text2.bas Dim iRead As Integer Dim iWrite As Integer Dim myFileName As String Dim newName As String Dim sInput As String myFileName = "C:\my documents\test1.bas" newName = "C:\my documents\test2.bas" iRead = FreeFile iWrite = iRead + 1 Close #iRead 'just in case open Close #iWrite Open myFileName For Input Access Read As #iRead Open newName For Output Access Write As #iWrite Do While Not EOF(iRead) Line Input #iRead, sInput If Len(sInput) Then If Left$(Trim(sInput), 15) = "Option Explicit" Then sInput = "'" & sInput End If End If Print #iWrite, sInput Loop End Sub Regards, Peter T "RB Smissaert" wrote in message ... Peter, Dave, That is it! I had Option Explicit at the top of the module that got the code imported. If I leave this off the code will run happily even with un-declared variables. I will now have to figure out what happens when there are these un-declared variables. It causes some strange behaviour or maybe not: If VarNonse = False Then Columns(2).Delete End If This will delete the column, even when the variable is declared nowhere. I suppose this is expected in that un-declared equates to false. In fact this probably is more trouble than getting the compile error as I now don't know if the variable was un-declared or declared but False. Is it possible to check somehow if a variable was declared or not? Thanks again for all the contributions; I learned something there! RBS "RB Smissaert" wrote in message ... Have the following situation: An .xla file will import a Sub from a text file and then run it like this: Dim strSub as String 'some code here to get the Sub imported Application.Run strSub Now it is possible that in this imported Sub are variables that are not defined publicly, so there will be a compile error, variable not defined. As this is not a runtime error I am not sure this error can be handled gracefully, but I would be interested in any suggestions how this could be done. RBS |
How to make error handle for mis-typed variable?
"RB Smissaert" wrote in message
... In fact checking all the variables doesn't even catch everything as there could be wrongly named functions and/or subs. This is the core of the problem and I don't think it has a satisfactory solution. Unfortunately you are working in the dark. I wonder if there might be a way of importing not just the sub but also all globally declared variables. That might alleviate the problem. -- Vasant |
How to make error handle for mis-typed variable?
The imported text files don't normally have public or even private
variables, although they could have. I think I will go with may latest reply to Peter T's post. RBS "Vasant Nanavati" <vasantn *AT* aol *DOT* com wrote in message ... "RB Smissaert" wrote in message ... In fact checking all the variables doesn't even catch everything as there could be wrongly named functions and/or subs. This is the core of the problem and I don't think it has a satisfactory solution. Unfortunately you are working in the dark. I wonder if there might be a way of importing not just the sub but also all globally declared variables. That might alleviate the problem. -- Vasant |
All times are GMT +1. The time now is 05:30 PM. |
Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com