Results 1 to 12 of 12

Thread: Problem locating application directory

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Mar 2006
    Posts
    21

    Problem locating application directory

    Hi all,

    I recently upgraded an application I had in VB 6 to VB.net (2005). In my old application I had some macros that presented data stored in text files in Excel (Results.xls). In my upgrade I had to copy these macros to a module in a seperate Excel spread sheet (Module.xls) where I execute them. I am still learning how to program in bot .net and vb6 so your help is greatly appreciated.

    The application runs well as long as I clearly specify the location of the folder containing the text files and the spread sheet used to present the data. This is the same folder that contains my executable. An example of the code I used with the location clearly specified is:

    Code:
    ChDir "C:\Work\Pipe02\"
    Workbooks.OpenText Filename:=("COMPMASS.TXT"), Origin:= _
            xlWindows, StartRow:=1,.........
    The problem I am having is that I cannot specify the location of the executable or the text files as the user may choose to install the application in whatever directory he/she may use. I tried using App.Path but that gives the directory as "". It goes without saying that my application crashes as the text files cannot be located.

    The code I tried to use is:
    Code:
    ChDir (App.Path & "\Pipe02\")
    Workbooks.OpenText Filename:=(App.Path &"\COMPMASS.TXT"), Origin:= _
            xlWindows, StartRow:=1,.........
    Your help will be much appreciated in resolving this problem.

  2. #2
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: Problem locating application directory

    probably this should be in the dot net forum

    a simple search i found this
    vb Code:
    1. Public Function App_Path() As String
    2.         Return System.AppDomain.CurrentDomain.BaseDirectory()
    3.     End Function
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Mar 2006
    Posts
    21

    Re: Problem locating application directory

    Thanks for your reply. I used that function in my .net application. However, my problem is occuring with my Excel macro. Is possible to use that function also in my macro?

  4. #4
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: Problem locating application directory

    app.path is not valid in excel, it has no idea where your vb6 (or net) program is running from, you would need to pass the path into the excel worksheet or code module or something, from you vb code
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  5. #5

    Thread Starter
    Junior Member
    Join Date
    Mar 2006
    Posts
    21

    Re: Problem locating application directory

    Thanks again. Is there an example of a code I could use to do this? I was thinking of storing the application location as a variable in memory then read it in Excel. The problem is I am not sure of how to implememnt it.

  6. #6
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: Problem locating application directory

    Quote Originally Posted by gangstergrene
    However, my problem is occuring with my Excel macro.
    Then this is a VBA question.

    Moved to Office Development

  7. #7

    Thread Starter
    Junior Member
    Join Date
    Mar 2006
    Posts
    21

    Re: Problem locating application directory

    Any assistance with this problem is greatly appreciated

  8. #8
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: Problem locating application directory

    If you are doing this from within your vb.net app then use ...

    Code:
    Application.StartupPath()
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  9. #9
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: Problem locating application directory

    Application.StartupPath()
    yeah, but he needs to pass this to his excel macro
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  10. #10

    Thread Starter
    Junior Member
    Join Date
    Mar 2006
    Posts
    21

    Re: Problem locating application directory

    Thanks again for you feedback guys. Is it possible to store the location then access it in my Excel macro?

  11. #11

    Thread Starter
    Junior Member
    Join Date
    Mar 2006
    Posts
    21

    Re: Problem locating application directory

    I just found the answer I was searching for
    I can use:
    Code:
    strFilePath =  ThisWorkbook.Path
    This returns the directory the workbook is in, then I use
    Code:
    Workbooks.OpenText Filename:=(strFilePath & "\COMPMASS.TXT"), Origin:= _
            xlWindows,..........
    to execute the macro.

    Thanks again for your help an support guys .

  12. #12
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: Problem locating application directory

    Um, but as from your first post you said you were doing this in vb.net?
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

Posting Permissions

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



Click Here to Expand Forum to Full Width