|
-
Feb 5th, 2008, 06:06 AM
#1
Thread Starter
Junior Member
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.
-
Feb 5th, 2008, 06:21 AM
#2
Re: Problem locating application directory
probably this should be in the dot net forum
a simple search i found this
vb Code:
Public Function App_Path() As String Return System.AppDomain.CurrentDomain.BaseDirectory() 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
-
Feb 5th, 2008, 06:28 AM
#3
Thread Starter
Junior Member
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?
-
Feb 5th, 2008, 06:35 AM
#4
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
-
Feb 5th, 2008, 06:57 AM
#5
Thread Starter
Junior Member
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.
-
Feb 5th, 2008, 07:20 AM
#6
Re: Problem locating application directory
 Originally Posted by gangstergrene
However, my problem is occuring with my Excel macro.
Then this is a VBA question.
Moved to Office Development
-
Feb 5th, 2008, 08:37 AM
#7
Thread Starter
Junior Member
Re: Problem locating application directory
Any assistance with this problem is greatly appreciated
-
Feb 5th, 2008, 01:36 PM
#8
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 Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API 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 
-
Feb 5th, 2008, 03:16 PM
#9
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
-
Feb 6th, 2008, 04:33 AM
#10
Thread Starter
Junior Member
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?
-
Feb 6th, 2008, 04:50 AM
#11
Thread Starter
Junior Member
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 .
-
Feb 6th, 2008, 03:49 PM
#12
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 Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API 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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|