Problem using Shell NameSpace method in VBA

Discussion in 'Information Technology' started by thor.carlson@gmail.com, Nov 1, 2005.


  1. 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.
     

  2. Gitche Gumee

    Gitche Gumee Guest

    Your string variable is not equivalent to the string literal. It's missing
    the required quotes:

    strTemp = """ & "c:\temp2" & """



    "thor.carlson@gmail.com" wrote:

    > 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.
    >
    >
     
  3. Dirk Goldgar

    Dirk Goldgar Guest

    <thor.carlson@gmail.com> wrote in message
    news:1130774508.573242.245590@f14g2000cwb.googlegroups.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)
     
  4. Thor

    Thor Guest

    Excellent! Dirk's solution worked for me. Is there a technical name
    for this "forced evaluation" technique so I can study up?
     
  5. Dirk Goldgar

    Dirk Goldgar Guest

    "Thor" <thor.carlson@gmail.com> wrote in message
    news:1130780081.032834.139030@g43g2000cwa.googlegroups.com
    > 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...

Share This Page