Using a declared function in a dir' address.

Hi Guys,

Ill be upfront, i am extremely new to VB and am still learning the ropes, so apologies if i do not make much sense lol!

Ok, what i am trying to do is place the current user logged on to the computer into a directory. Eg,

-------------------------

ChDir "C:\Documents and Settings\USERNAMELOGGED IN\Desktop"
Workbooks.OpenText Filename:= _
"C:\Documents and Settings\USERNAMELOGGED IN\Desktop\IMEI.xls", Origin:= _
xlMacintosh, StartRow:=1, DataType:=xlDelimited, TextQualifier:= _
xlDoubleQuote, ConsecutiveDelimiter:=False, Tab:=True, Semicolon:=False, _
Comma:=False, Space:=False, Other:=False, FieldInfo:=Array(1, 1)

-----------------------------

I am wanting to replace "Usernamelogged In" with the user currently logged in to the computer. Eg ChDir "C:\Documents and Settings\Joe Blow\Desktop"

Each time the program goes to a new computer, the code has to be changed to reflect the logged in name of that computer, i would now like to automate this process.

My understanding is that i need to use a declared function to obtain the current active username on the PC and then set that into the above address, but i am unsure of how to incorporate this into the above code.

Pretty please help lol!

Steve.



Answer this question

Using a declared function in a dir' address.

  • Tom De Cort

    I think you need to concatenate the return of the environment function and the path you're looking for:

    ChDir (Environment.GetFolderPath(Environment.SpecialFolder.Desktop) & "\ IMEI.xls")  <---- this seems to work ok, but unable to test it as the 2 lines below dont work
         Workbooks.OpenText Filename:= _
            Environment.GetFolderPath(Environment.SpecialFolder.Desktop & "\ IMEI.XLS", Origin:= _  <--- i cant get this to work
            xlMacintosh, StartRow:=1, DataType:=xlDelimited, TextQualifier:= _
            xlDoubleQuote, ConsecutiveDelimiter:=False, Tab:=True, Semicolon:=False, _
            Comma:=False, Space:=False, Other:=False, FieldInfo:=Array(1, 1)

     

    Or even better try using the system.io.path.combine function to create a string representing the path you're interested.



  • learnerplates

    to me you don't really need the ChDir (Change directory) since you are including the path correctly in the connection string/filename of what appears to be the Excel Workbook. If you still have problems even after commenting out the ChDir line, please post the exact error, and the code where it produces the error in

  • ABS_BEG

    Thankyou :0

    i have commented the ChDir line. I still get the exactly the same error - "Runtine error 424 - object required". but on the now first line of code.

    Workbooks.OpenText Filename:= _
    (Environment.GetFolderPath(Environment.SpecialFolder.Desktop) & "\ IMEI.xls"), Origin:= _
    xlMacintosh, StartRow:=1, DataType:=xlDelimited, TextQualifier:= _
    xlDoubleQuote, ConsecutiveDelimiter:=False, Tab:=True, Semicolon:=False, _
    Comma:=False, Space:=False, Other:=False, FieldInfo:=Array(1, 1)

    If it helps, if i replace line (Environment.GetFolderPath(Environment.SpecialFolder.Desktop) & "\ IMEI.xls") with "C:\Documents and Settings\Steve W\Desktop\IMEI.xls" it works %100 fine.

    So i am guessing it is something to do with (Environment.GetFolderPath(Environment.SpecialFolder.Desktop) & "\ IMEI.xls") (or line 2 in the above script)


  • dakerson

    Out of curiosity, what version of VB are you using

    Only weird thing in your code is that you seem to have a space between the \ and the IMEI - not sure if that could be it or not.



  • Chuff

    question I do have is - why are you using ChDir to change the directory you dont really need this

    At the end of the day - what are you trying to achieve I think that should be ok - but if there are any errors please do post them, and the error message as well as the link it is pointing to. For now, I am glad we are able to help you! :-)



  • Brett Selleck

    I would also suggest you step through the debugger and get the full string value of the path you are specifying, and post back the results. however to me, it seems as if its Excel throwing the error really and not the VB.NET application....could may well be the arguments being passed to Excel or something

  • Cory.Isakson

    I am glad there are people that know what the hell is going on with this stuff haha. Way to confusing for me! i really appreciate the help!

    Basically, i am revamping a script that was once designed by a colleague that no longer works here. He was silly enough to script it so it only worked on his computer, now we need it to work on other computers also. i traced the problem back to the Chdir issue. I figured if i replaced his name to change to whatever computer is using the program at the time then the problem should be solved. I am merely just editting what was already there, i have no idea what ChDir does and why it was used other than another option.

    The only error message i am getting is "Runtime error 424 - object required" followed by that line being highlighted yellow.


  • Nolubez

    ahmedilyas wrote:
    I would also suggest you step through the debugger and get the full string value of the path you are specifying, and post back the results. however to me, it seems as if its Excel throwing the error really and not the VB.NET application....could may well be the arguments being passed to Excel or something

    Thats what i dont get. if it was the arguments then how come it works when i use the original code which is the raw dir' with the username typed in manually It only ever goes silly when i place the code in there to determin the dir and username logged in lol

    I tell ya what, i am thankful i am not a programmer this stuff is just way to hard with all the little dots and brackets and hashes hehe.


  • Jim Perry

    Alex, Ver 6.3

    I noticed the space also which i removed, it reproduced the exact same error.


  • Doraemon_1

    hey thankyou for the quick reply!!!

    yes it makes sense to a degree, but i am a little confused as to how i impliment it into the scripting above - this is where my problem sits hehe.

    ChDir (Environment.GetFolderPath(Environment.SpecialFolder.Desktop) \ IMEI.xls) <---- this seems to work ok, but unable to test it as the 2 lines below dont work
    Workbooks.OpenText Filename:= _
    "Environment.GetFolderPath(Environment.SpecialFolder.Desktop \ IMEI.XLS", Origin:= _ <--- i cant get this to work
    xlMacintosh, StartRow:=1, DataType:=xlDelimited, TextQualifier:= _
    xlDoubleQuote, ConsecutiveDelimiter:=False, Tab:=True, Semicolon:=False, _
    Comma:=False, Space:=False, Other:=False, FieldInfo:=Array(1, 1)

    *cries*

    hehe


  • Zero WangXin

    ahmedilyas wrote:
    send over the project/excel file with it via my email address on profile and i will be sure to post back the solution here

    I would love to take up the oppotunity but unfortunately the file contains sensative and personal data (i work for a mobile telco) and naturally i could not release any of it to the public without jeopardising my job.

    I think it is something that i will have to let be and live it.


    Thankyou a stack for all your help!!!!


  • Vlad Bezden

    send over the project/excel file with it via my email address on profile and i will be sure to post back the solution here

  • danadanny

    Ah yes forgot the &'s

    I now have the following

    ChDir (Environment.GetFolderPath(Environment.SpecialFolder.Desktop) & "\ IMEI.xls")
    Workbooks.OpenText Filename:= _
    (Environment.GetFolderPath(Environment.SpecialFolder.Desktop) & "\ IMEI.xls"), Origin:= _
    xlMacintosh, StartRow:=1, DataType:=xlDelimited, TextQualifier:= _
    xlDoubleQuote, ConsecutiveDelimiter:=False, Tab:=True, Semicolon:=False, _
    Comma:=False, Space:=False, Other:=False, FieldInfo:=Array(1, 1)

    When the script has been run it comes to a grinding halt on the first line, highlighted in yellow. I can confirm the file IMEI.xls is sitting on the desktop.

    I think a few more beers are in order!! haha

    apart from the missing close bracket on the third line which i have chucked in, is there anything else wrnog with the above code


  • SteveMo

    to obtain the current logged on user, try this:

    Environment.UserName

    that will get the username of the user who started the process (your application).

    If you are accessing a common folder (such as the start menu, desktop, my documents), best practice to obtain the full path (including the user who has logged in) would be to do this:

    Environment.GetFolderPath(Environment.SpecialFolder.Desktop)

    which will return back the path (string) of that location. Go through the enum of the SpecialFolder (intellisense) and you will see a whole range of properties/enum's that will get you the path your require for the user logged in

    does this help



  • Using a declared function in a dir' address.