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
news:eG8i9CpTFHA.3620@TK2MSFTNGP09.phx.gbl...
> 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).Execute
>
> 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" <bartsmissaert@blueyonder.co.uk> wrote in message
> news:#5irmsoTFHA.3620@TK2MSFTNGP09.phx.gbl...
>> 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" <bartsmissaert@blueyonder.co.uk> wrote in message
>> news:OlThA0$SFHA.2872@TK2MSFTNGP14.phx.gbl...
>> > 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
>> >
>>
>
>
Bookmarks