+ Reply to Thread
Results 1 to 23 of 23

How to make error handle for mis-typed variable?

  1. #1
    RB Smissaert
    Guest

    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


  2. #2
    Dave Peterson
    Guest

    Re: 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

  3. #3
    RB Smissaert
    Guest

    Re: 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" <ec35720@netscapeXSPAM.com> wrote in message
    news:42717DED.3CB2D147@netscapeXSPAM.com...
    > 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



  4. #4
    RB Smissaert
    Guest

    Re: How to make error handle for mis-typed variable?

    Do I conclude then that there is just no solution for this?

    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
    >



  5. #5
    Dave Peterson
    Guest

    Re: 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" <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
    > >


    --

    Dave Peterson

  6. #6
    RB Smissaert
    Guest

    Re: 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" <ec35720@netscapeXSPAM.com> wrote in message
    news:4274C20B.439BA9EB@netscapeXSPAM.com...
    > 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" <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
    >> >

    >
    > --
    >
    > Dave Peterson



  7. #7
    Vasant Nanavati
    Guest

    Re: 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" <bartsmissaert@blueyonder.co.uk> wrote in message
    news:O7$GQJkTFHA.2768@tk2msftngp13.phx.gbl...
    > Do I conclude then that there is just no solution for this?
    >
    > 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
    > >

    >




  8. #8
    Vasant Nanavati
    Guest

    Re: 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
    news:%2353DaLmTFHA.3392@TK2MSFTNGP12.phx.gbl...
    > 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" <bartsmissaert@blueyonder.co.uk> wrote in message
    > news:O7$GQJkTFHA.2768@tk2msftngp13.phx.gbl...
    > > Do I conclude then that there is just no solution for this?
    > >
    > > 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
    > > >

    > >

    >
    >




  9. #9
    RB Smissaert
    Guest

    Re: 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
    news:ObiSUQmTFHA.2392@TK2MSFTNGP10.phx.gbl...
    > 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
    > news:%2353DaLmTFHA.3392@TK2MSFTNGP12.phx.gbl...
    >> 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" <bartsmissaert@blueyonder.co.uk> wrote in message
    >> news:O7$GQJkTFHA.2768@tk2msftngp13.phx.gbl...
    >> > Do I conclude then that there is just no solution for this?
    >> >
    >> > 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
    >> > >
    >> >

    >>
    >>

    >
    >



  10. #10
    Vasant Nanavati
    Guest

    Re: 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" <bartsmissaert@blueyonder.co.uk> wrote in message
    news:eyWCbunTFHA.3696@TK2MSFTNGP15.phx.gbl...
    > 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" <ec35720@netscapeXSPAM.com> wrote in message
    > news:42751BD7.E206F66C@netscapeXSPAM.com...
    > >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" <ec35720@netscapeXSPAM.com> wrote in message
    > >> news:4274C20B.439BA9EB@netscapeXSPAM.com...
    > >> > 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" <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
    > >> >> >
    > >> >
    > >> > --
    > >> >
    > >> > Dave Peterson

    > >
    > > --
    > >
    > > Dave Peterson

    >




  11. #11
    RB Smissaert
    Guest

    Re: 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" <ec35720@netscapeXSPAM.com> wrote in message
    news:42751BD7.E206F66C@netscapeXSPAM.com...
    >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" <ec35720@netscapeXSPAM.com> wrote in message
    >> news:4274C20B.439BA9EB@netscapeXSPAM.com...
    >> > 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" <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
    >> >> >
    >> >
    >> > --
    >> >
    >> > Dave Peterson

    >
    > --
    >
    > Dave Peterson



  12. #12
    Dave Peterson
    Guest

    Re: 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" <ec35720@netscapeXSPAM.com> wrote in message
    > news:4274C20B.439BA9EB@netscapeXSPAM.com...
    > > 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" <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
    > >> >

    > >
    > > --
    > >
    > > Dave Peterson


    --

    Dave Peterson

  13. #13
    Dave Peterson
    Guest

    Re: 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" <ec35720@netscapeXSPAM.com> wrote in message
    > news:42751BD7.E206F66C@netscapeXSPAM.com...
    > >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" <ec35720@netscapeXSPAM.com> wrote in message
    > >> news:4274C20B.439BA9EB@netscapeXSPAM.com...
    > >> > 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" <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
    > >> >> >
    > >> >
    > >> > --
    > >> >
    > >> > Dave Peterson

    > >
    > > --
    > >
    > > Dave Peterson


    --

    Dave Peterson

  14. #14
    Dave Peterson
    Guest

    Re: 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" <ec35720@netscapeXSPAM.com> wrote in message
    > news:42751BD7.E206F66C@netscapeXSPAM.com...
    > >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" <ec35720@netscapeXSPAM.com> wrote in message
    > >> news:4274C20B.439BA9EB@netscapeXSPAM.com...
    > >> > 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" <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
    > >> >> >
    > >> >
    > >> > --
    > >> >
    > >> > Dave Peterson

    > >
    > > --
    > >
    > > Dave Peterson


    --

    Dave Peterson

  15. #15
    RB Smissaert
    Guest

    Re: 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
    news:Ozusw5nTFHA.2420@TK2MSFTNGP12.phx.gbl...
    > 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" <bartsmissaert@blueyonder.co.uk> wrote in message
    > news:eyWCbunTFHA.3696@TK2MSFTNGP15.phx.gbl...
    >> 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" <ec35720@netscapeXSPAM.com> wrote in message
    >> news:42751BD7.E206F66C@netscapeXSPAM.com...
    >> >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" <ec35720@netscapeXSPAM.com> wrote in message
    >> >> news:4274C20B.439BA9EB@netscapeXSPAM.com...
    >> >> > 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" <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
    >> >> >> >
    >> >> >
    >> >> > --
    >> >> >
    >> >> > Dave Peterson
    >> >
    >> > --
    >> >
    >> > Dave Peterson

    >>

    >
    >



  16. #16
    Peter T
    Guest

    Re: 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" <bartsmissaert@blueyonder.co.uk> wrote in message
    news:eyWCbunTFHA.3696@TK2MSFTNGP15.phx.gbl...
    > 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" <ec35720@netscapeXSPAM.com> wrote in message
    > news:42751BD7.E206F66C@netscapeXSPAM.com...
    > >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" <ec35720@netscapeXSPAM.com> wrote in message
    > >> news:4274C20B.439BA9EB@netscapeXSPAM.com...
    > >> > 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" <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
    > >> >> >
    > >> >
    > >> > --
    > >> >
    > >> > Dave Peterson

    > >
    > > --
    > >
    > > Dave Peterson

    >




  17. #17
    RB Smissaert
    Guest

    Re: 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" <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
    >



  18. #18
    Vasant Nanavati
    Guest

    Re: 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" <bartsmissaert@blueyonder.co.uk> wrote in message
    news:%235irmsoTFHA.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
    > >

    >




  19. #19
    Peter T
    Guest

    Re: 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).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
    > >

    >




  20. #20
    RB Smissaert
    Guest

    Re: 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
    news:uFL7%233oTFHA.3308@TK2MSFTNGP14.phx.gbl...
    > 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" <bartsmissaert@blueyonder.co.uk> wrote in message
    > news:%235irmsoTFHA.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
    >> >

    >>

    >
    >



  21. #21
    RB Smissaert
    Guest

    Re: 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
    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
    >> >

    >>

    >
    >



  22. #22
    Vasant Nanavati
    Guest

    Re: How to make error handle for mis-typed variable?

    "RB Smissaert" <bartsmissaert@blueyonder.co.uk> wrote in message
    news:uX7qnKpTFHA.1568@TK2MSFTNGP10.phx.gbl...
    > 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



  23. #23
    RB Smissaert
    Guest

    Re: 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
    news:eThkuPpTFHA.2336@TK2MSFTNGP12.phx.gbl...
    > "RB Smissaert" <bartsmissaert@blueyonder.co.uk> wrote in message
    > news:uX7qnKpTFHA.1568@TK2MSFTNGP10.phx.gbl...
    >> 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
    >
    >



+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1