Environ("username") - compile error Excel 2010

MerkDog

New Member
Joined
Jan 9, 2007
Messages
23
Hi,

The following code used to work fine for me in Excel 2003:

Code:
MsgBox Environ("username")

In 2010, I get a compile error: "cannot find project or library". I get the same error on trying to use the Chr function, which makes me think it must be something obvious...but I can't figure it out.

Thanks
MerkDog

Excel 2010, Windows XP
 
Just for anyone that might, someday come across this thread in the future, for the record, I probably would not actually do
Code:
    #If VBA7 Then
        UserForm1.Show VBA.[__MIDL___MIDL_itf_m_0001_0016_0001].vbModal
    #Else
        UserForm1.Show VBA.FormShowConstants.vbModal
    #End If
Simply because I would be worried about Microsoft later catching this snafu and fixing it in the next release of Excel and the VBAx compiling constants tend to be "x or better" type constants, i.e. in Excel 2010, VBA6 is TRUE. So if they would fix it, you'd then have a third case to worry about. What I've done is to "throw caution to the wind and live life on the edge" and simply remove the full qualification for code I need to upgrade for 2010 (and I quit using the full qualification when writing new code).
 
Upvote 0

Excel Facts

What is the shortcut key for Format Selection?
Ctrl+1 (the number one) will open the Format dialog for whatever is selected.
Just for anyone that might, someday come across this thread in the future, for the record, I probably would not actually do
Code:
    #If VBA7 Then
        UserForm1.Show VBA.[__MIDL___MIDL_itf_m_0001_0016_0001].vbModal
    #Else
        UserForm1.Show VBA.FormShowConstants.vbModal
    #End If
Simply because I would be worried about Microsoft later catching this snafu and fixing it in the next release of Excel and the VBAx compiling constants tend to be "x or better" type constants, i.e. in Excel 2010, VBA6 is TRUE. So if they would fix it, you'd then have a third case to worry about. What I've done is to "throw caution to the wind and live life on the edge" and simply remove the full qualification for code I need to upgrade for 2010.

How about using the handy CallByName Method and handle a runtime error instead - It always work.
 
Upvote 0
How about using the handy CallByName Method and handle a runtime error instead - It always work.

Frankly, although I knew about the existence of the CallByName method, I have never used it. You wanna throw together an example of what that might look like?
 
Upvote 0
Here is an example that shows how to display the form in excel 2007
Code:
CallByName UserForm1, "Show", VbMethod, vbModal

If you were to pass a wrong Member AND/OR wrong Argument as follows it would raise a Runtime error that can be safely handled :
Code:
CallByName UserForm1, "BlaBlaBla", VbMethod, "bla bla bla"

The above line is the equivalent of :
Code:
UserForm1.BlaBlaBla "bla bla bla"
Which would rise a Compile error.


Looking more carefully,I am not sure if in our particular scenario where the argument is a hidden member ,the form can be displayed in excel 2010 using the CallByName Method.

Anyway, see if running this code line works : (I didn't try this as I don't have Excel 2010)
Code:
CallByName UserForm1, "Show", VbMethod, [__MIDL___MIDL_itf_m_0001_0016_0001].vbModal

Another trick that I tend to use in order to avoid version related compile errors is by placing each version-code in a seperate module thus avoiding compiling constants.
 
Upvote 0
Jafaar,

As I thought, you can't get that to compile in the "other" version, i.e.
Code:
Sub foofoo()
 
    CallByName UserForm1, "Show", VbMethod, VBA.FormShowConstants.vbModal
 
End Sub

Won't compile in XL2010. But
Code:
Sub fofofo()
 
    CallByName UserForm1, "Show", VbMethod, VBA.[__MIDL___MIDL_itf_m_0001_0016_0001].vbModal
 
End Sub

compiles, no problem.
 
Upvote 0

Forum statistics

Threads
1,215,575
Messages
6,125,631
Members
449,241
Latest member
NoniJ

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top