+ Reply to Thread
Results 1 to 11 of 11

Can you use a piece of VB code to get the path of the current excel file?

  1. #1
    Registered User
    Join Date
    10-04-2009
    Location
    UK
    MS-Off Ver
    Excel 2010
    Posts
    77

    Can you use a piece of VB code to get the path of the current excel file?

    Hi all

    First off, I hope that this makes sense...

    I work for a company which has about 40 branches nationwide.
    I created a system which stores customer booking info and emails the customer with confirmation to save users time having to manually type everything - plus its then standard.

    As pricing and stuff changes I need to be able to send out an "upgrade" workbook as required which the branches will run and it will automatically run a macro to update the pricing values of the main workbook. The main program workbook is always called HBPOS3.xls. The only thing that will change is the path to this file. It is always stored in HBPOS/HBPOS3.xls. All upgrades are saved into the HBPOS/UpgradeFiles/ folder.

    However the path to the HBPOS folder will change dependant on the computer i.e. our's is stored at "C:/Documents And Settings/hbbas/desktop/HBPOS" so it depends on the user i.e. our's is hbbas. I won't know what each path is.

    So is there a way that when they open the upgrade file, which will be saved in HBPOS/UpgradeFiles/, that i can use some code to automatically get the PATH to the HBPOS/UpgradeFiles/ directory so that i can then just trim off "/UpgradeFiles/" and replace with "HBPOS3.xls" to transfer the data over?

    Is there also a way to check that the file exists? i.e. say it detects the path is C:/HBPOS3/, is there a way to check C:/HBPOS3/HBPOS3.xls exists? and if it doesn't, that it says that it can't find HBPOS3 and so upgrade aborted rather than coming up with a VB error?

    Thanks for your time and help. I've not coded anything yet as I'm seeing if it's possible first.

  2. #2
    Forum Guru
    Join Date
    03-02-2006
    Location
    Los Angeles, Ca
    MS-Off Ver
    WinXP/MSO2007;Win10/MSO2016
    Posts
    12,627

    Re: Can you use a piece of VB code to get the path of the current excel file?

    Look up DIR function in VB Help
    Ben Van Johnson

  3. #3
    Registered User
    Join Date
    10-04-2009
    Location
    UK
    MS-Off Ver
    Excel 2010
    Posts
    77

    Re: Can you use a piece of VB code to get the path of the current excel file?

    You must specify pathname the first time you call the Dir function, or an error occurs. If you also specify file attributes, pathname must be included.
    I looked up DIR and it says this.
    I already said that when they run the installer, I wouldn't know the path that they had installed HBPOS too - it won't be the same on every computer - so I'd need some way for VB to find HBPOS3.xls's own path and then work from there...

    Thanks

  4. #4
    Forum Guru DonkeyOte's Avatar
    Join Date
    10-22-2008
    Location
    Northumberland, UK
    MS-Off Ver
    O365
    Posts
    21,531

    Re: Can you use a piece of VB code to get the path of the current excel file?

    If the file is open - see .FullName and/or .Path (applied to ActiveWorkbook / ThisWorkbook as appropriate)

  5. #5
    Forum Guru Andy Pope's Avatar
    Join Date
    05-10-2004
    Location
    Essex, UK
    MS-Off Ver
    O365
    Posts
    20,441

    Re: Can you use a piece of VB code to get the path of the current excel file?

    You can use the .Path property

    Please Login or Register  to view this content.
    This previous post will should you how to get the users document folder
    http://www.excelforum.com/excel-prog...ple-users.html
    Cheers
    Andy
    www.andypope.info

  6. #6
    Forum Expert snb's Avatar
    Join Date
    05-09-2010
    Location
    VBA
    MS-Off Ver
    Redhat
    Posts
    5,649

    Re: Can you use a piece of VB code to get the path of the current excel file?

    You can easily check one of these standardpaths:

    Please Login or Register  to view this content.
    checking whether the file exists:
    Please Login or Register  to view this content.
    Last edited by snb; 01-03-2011 at 07:28 AM.



  7. #7
    Registered User
    Join Date
    10-04-2009
    Location
    UK
    MS-Off Ver
    Excel 2010
    Posts
    77

    Re: Can you use a piece of VB code to get the path of the current excel file?

    Quote Originally Posted by Andy Pope View Post
    You can use the .Path property

    Please Login or Register  to view this content.
    This previous post will should you how to get the users document folder
    http://www.excelforum.com/excel-prog...ple-users.html
    This works absolutely fine :-)

    The path that the install files is held in is always "/UpgradeFiles" which is 13 characters long including the /. I've used left and right before to get the characters from the end of the string or the beginning, is there a way that I can get all the characters in the string except for those last 13 characters, bearing in mind the path will not always be the same dependant on the user?

    Thanks a lot for your help

  8. #8
    Forum Guru DonkeyOte's Avatar
    Join Date
    10-22-2008
    Location
    Northumberland, UK
    MS-Off Ver
    O365
    Posts
    21,531

    Re: Can you use a piece of VB code to get the path of the current excel file?

    Assuming the obvious perhaps:

    Please Login or Register  to view this content.

  9. #9
    Registered User
    Join Date
    10-04-2009
    Location
    UK
    MS-Off Ver
    Excel 2010
    Posts
    77

    Re: Can you use a piece of VB code to get the path of the current excel file?

    Quote Originally Posted by snb
    checking whether the file exists:
    Please Login or Register  to view this content.
    That also works great - thanks :-)
    Last edited by DonkeyOte; 01-03-2011 at 07:45 AM. Reason: corrected tags

  10. #10
    Registered User
    Join Date
    10-04-2009
    Location
    UK
    MS-Off Ver
    Excel 2010
    Posts
    77

    Re: Can you use a piece of VB code to get the path of the current excel file?

    Quote Originally Posted by DonkeyOte View Post
    Assuming the obvious perhaps:

    Please Login or Register  to view this content.
    Magic! thanks :-)

    And one last thing. I've searched for this but it doesn't seem to be bringing up any relevant results.

    HBPOS3 contains a userform called Dashboard which is the main screen that opens when you first open HBPOS3.

    What happens is the administrator clicks on "Run Upgrades" on the dashboard and another userform appears called AdminUpgrade. This is where you select an upgrade file and it opens that upgrade file.

    So for example, the one I've just trialed is called "HBPOS_3Jan11.xls". I've managed to make the macro in HBPOS_3Jan11.xls update data in HBPOS3.xls fine. It then says a message that the install is complete and closes HBPOS_3Jan11.xls.

    However in order to open HBPOS_3Jan11.xls I had to make the macro in HBPOS3.xls, unload Dashboard and AdminUpgrade. So when HBPOS_3Jan11.xls closes, it leaves HBPOS3.xls open with just its main sheets. Is there anyway that when HBPOS_3Jan11.xls closes, I can make it load up the Dashboard userform from HBPOS3.xls again?

    Thank you

  11. #11
    Registered User
    Join Date
    10-04-2009
    Location
    UK
    MS-Off Ver
    Excel 2010
    Posts
    77

    Re: Can you use a piece of VB code to get the path of the current excel file?

    Please Login or Register  to view this content.
    I've tried this. Which works. However it doesn't close the HBPOS_3Jan11.xls file as Dashboard userform is shown.

    However if I switch the coding, like so

    Please Login or Register  to view this content.
    The workbook HBPOS_3Jan11.xls closes and then it doesn't run the macro below.

    Is there a way I can close HBPOS_3Jan11.xls and run the LoadDashboard module from HBPOS3.xls?

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1