Discussion:
Problem using Shell NameSpace method in VBA
(too old to reply)
t***@gmail.com
2005-10-31 16:01:48 UTC
Permalink
Doubtless this is a ridiculous newbie question in which case apologies
in advance.

I'm working from VBA in Microsoft Access and want to operate on a file
folder on the local computer. The NameSpace method creates and returns
a Folder object for the specified folder.

Looking at the Test procedure below, can someone explain to me why I
get "Nothing" back when I call NameSpace with a string variable, but I
get a Folder object back when I call NamesSpace with a string literal?

Private Sub Test()

Dim myApp As Object
Dim myFolder
Dim strTemp As String

strTemp = "c:\temp2"

Set myApp = CreateObject("Shell.Application")
Set myFolder = myApp.NameSpace(strTemp) ' returns Nothing
Set myFolder = myApp.NameSpace("c:\temp2") ' returns a Folder
object

End Sub



Thanks in advance.
Gitche Gumee
2005-10-31 17:10:35 UTC
Permalink
Your string variable is not equivalent to the string literal. It's missing
the required quotes:

strTemp = """ & "c:\temp2" & """
Post by t***@gmail.com
Doubtless this is a ridiculous newbie question in which case apologies
in advance.
I'm working from VBA in Microsoft Access and want to operate on a file
folder on the local computer. The NameSpace method creates and returns
a Folder object for the specified folder.
Looking at the Test procedure below, can someone explain to me why I
get "Nothing" back when I call NameSpace with a string variable, but I
get a Folder object back when I call NamesSpace with a string literal?
Private Sub Test()
Dim myApp As Object
Dim myFolder
Dim strTemp As String
strTemp = "c:\temp2"
Set myApp = CreateObject("Shell.Application")
Set myFolder = myApp.NameSpace(strTemp) ' returns Nothing
Set myFolder = myApp.NameSpace("c:\temp2") ' returns a Folder
object
End Sub
Thanks in advance.
Dirk Goldgar
2005-10-31 17:15:58 UTC
Permalink
Post by t***@gmail.com
Doubtless this is a ridiculous newbie question in which case apologies
in advance.
I'm working from VBA in Microsoft Access and want to operate on a file
folder on the local computer. The NameSpace method creates and
returns a Folder object for the specified folder.
Looking at the Test procedure below, can someone explain to me why I
get "Nothing" back when I call NameSpace with a string variable, but I
get a Folder object back when I call NamesSpace with a string literal?
Private Sub Test()
Dim myApp As Object
Dim myFolder
Dim strTemp As String
strTemp = "c:\temp2"
Set myApp = CreateObject("Shell.Application")
Set myFolder = myApp.NameSpace(strTemp) ' returns Nothing
Set myFolder = myApp.NameSpace("c:\temp2") ' returns a Folder
object
End Sub
I'm not an expert in this area, but I suspect that the string *variable*
is passed differently than the string literal -- and there can be no
implicit conversion because you're using late binding. You can force
evaluation of the string variable and pass the result of that evaluation
to the NameSpace method by wrapping the variable name in an extra set of
parentheses, like this:

Set myFolder = myApp.NameSpace((strTemp))

That worked in my test.
--
Dirk Goldgar, MS Access MVP
www.datagnostics.com

(please reply to the newsgroup)
Thor
2005-10-31 17:34:41 UTC
Permalink
Excellent! Dirk's solution worked for me. Is there a technical name
for this "forced evaluation" technique so I can study up?
Dirk Goldgar
2005-11-01 15:44:49 UTC
Permalink
Post by Thor
Excellent! Dirk's solution worked for me. Is there a technical name
for this "forced evaluation" technique so I can study up?
I was just looking through the VB help, and I couldn't find a topic that
even mentions it. I learned it somewhere along the way, but I don't
know where -- probably from a newsgroup post. In general, putting
parentheses around a variable name -- aside from parentheses that are
otherwise required by the syntax of an array-element reference or a
subroutine/function call -- forces the evaluation of that variable.
Most often, this only makes a difference with object variables, where it
distinguishes between the object itself and the *value* of the object,
but you found a situation where it mattered even with a normal variable.
--
Dirk Goldgar, MS Access MVP
www.datagnostics.com

(please reply to the newsgroup)
Loading...