1. ► Join Elite Columnists/Writer's Panel: You are cordially invited to join (register here) SPN's elite panel of Columnists, writers & start sharing your perspectives with a worldwide audience!
    Dismiss Notice

Problem using Shell NameSpace method in VBA

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

  1. thor.carlson@gmail.com

    thor.carlson@gmail.com
    Expand Collapse
    Guest

    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
    Expand Collapse
    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
    Expand Collapse
    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
    Expand Collapse
    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
    Expand Collapse
    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...
Similar Threads Forum Date
Politics BNP using Sikhs to create problems against Islam for no reason! Current News May 8, 2010
Dasam" Granth - A Look at the Core Problems Blogs Oct 21, 2015
Problems In Life Sikh Sikhi Sikhism Apr 13, 2015
How can I make decent and useful friends and from where ?? most people have a problem with me Sikh Youth Oct 23, 2013
The Problem with Taking Too Many Vitamins Health & Nutrition Oct 18, 2013

Share This Page