Trouble specifying path to open Excel woorbook in vb.net app - vb.net

I have a WFP app that works well. I open an Access DB and want to do the same with an Excel workbook. Opening the DB is not an issue as I am able to remove the drive letter from the path. I am having a bit of an issue with the search path for the workbook. My path operates as expected when the drive letter is specified [here is the line of code that works properly -- xlWorkBook_AR = xlApp_AR.Workbooks.Open("S:\11_2017_Spring\MPRecords-2\Accounting\FinancialSystem.xlsm")]). When I remove the drive specification from the path it does not operate as expected. I receive an error stating the file is not found. What do I need to do to make this dynamic?
Thanks in advance.
Ed

Try this (if it works for your scenario):
Place the excel file in the same folder as your app (.exe) (I assume this works for your needs).
Use this code to dynamically get the path of your app, and then add on the name of the file, something like the below:
Application.StartupPath & "\FinancialSystem.xlsm"
Supply this to excel's .Open method and I believe it will work.
You will need to import System.Windows.Forms. Read about Application.StartupPath property here: https://msdn.microsoft.com/en-us/library/system.windows.forms.application.startuppath.aspx

Related

Excel on Mac. Cannot open IQY file in Data/Get External Data/Run Web Query

I am running Excel version 16.45 on Mac.
I have created a .iqy and saved it in the Queries directory alongside certain templates which were already there.
I go to Data/Get External Data/Run Web Queries. While the templates are accessible, my file is visible but greyed out (same thing happens if I save the file in a different directory).
Would anyone be able to help?
According to your information, I would like to confirm whether the issue occurs when you follow the steps as below:
1.Create a Word file, paste the web URL.
2.Save the Word as .iqy with .txt format.
3.Choose MS-DOS as coding.
4.Create an Excel file and click "Data >Get External Data >Run Web Query (Or Run Saved Query) " of the Bar
I was able to address the issue as follows.
When navigating to the relevant directory with Finder, the '.iqy' file appears to be appropriately named (as per Image 1).
In fact, if you reach the file in Terminal, the file is saved as '.iqy.txt'.
So all I had to do is rename the file, simply removing the '.txt' string at the end.
Screen you see when navigating in Finder

change of location of the database dynamically in app running

Try to make my app read the ms-access database from shortcut of my database it failed to read, so try to change the location of database dynamically ( there is an option in the app to move the database to drop-box folder and create a shortcut to that database in app folder )
try to make an shortcut to the moved ms-database
the app to read the database or to change the location of database dynamically
First of all, you should create a folder with a clear name in your VB.Net application path, namely inside the project Debug folder, let's name that folder as "MyProjFiles", so it will be in this path: ProjectFolderName\bin\Debug\MyProjFiles
Put your whole projects files inside our lovely folder MyProjFiles, including all types of your attachments: database, images, sounds, files, etc.
Call your database or whatever of those attachments files in addition to our \MyProjFiles\ using this method: My.Computer.FileSystem.CurrentDirectory & "\MyProjFiles\YourFilesPathHere.EXT".
Now, the whole path will be such as this string: "C:\CurrentUserNam\RootFolder\ProjectFolderName\bin\Debug\MyProjFiles\YourFilesPathHere.EXT"
For great practical example of this, supposuply let's open our MSAccessDB.accdb which is already copied into our project folder \MyProjFiles\ by this code directly:
System.Diagnostics.Process.Start(My.Computer.FileSystem.CurrentDirectory() & "\MyProjFiles\MSAccessDB.accdb")
The result will be simply opening our database which called "MSAccessDB.accdb"
Or open some pdf files such as this line:
System.Diagnostics.Process.Start(My.Computer.FileSystem.CurrentDirector() & "\MyProjFiles\MyPdfFile.pdf")
and so on.
I hope this can help you all brothers.
Best ^_^ Regards.
You should read this link.
It explains how to read the information you need to give the access Datareader something to do.
.NET read binary contents of .lnk file
Maybe this is enough, so you don't need to copy anything.

Unable to find file path using workbooks.open in VBA

I am new to VBA howeverI have come across a bit of a stumbling block.
I need to import a CSV file to automate some data.
using a pc I have been able to import the data fine using the below code:
Workbooks.OpenText Filename:="File path is inserted here", local:=True
The above code works fine.
However, when trying this on my Mac, I get a file path error.
I managed to get round this but then returned the file or folder may be read only (which its not). Checked the file to read+write but when entering the file path again I got the same old error of not being able t find the file.
I am racking my brains here as I have tried using ":" and "\" as separators however the file path is still not found
Any help would be much appreciated.
Oh and I have also recorded a macro and imported the file manually, which bought up a whole list of code.
Thanks in advance.

Open a specific file in vb.net

I want my program to open a specific .txt file. The text file will always stay in the same folder within the solution folder. However, the location of the solution folder itself may change if the solution is moved to a different computer, or a different directory on the current computer.
I know how to hardcode the file path append the file name and then open it. But how can I define the file path so that the file can still be opened if the solution moves to a different computer?
If the file is contained within the solution, you can use a virtual path which is then mapped to a physical path using Server.MapPath
The following should work :
Dim filePath As String = Server.MapPath("~/FileName.txt")
Please note that the location of FileName.txt in my example is in the root of the solution and not in any specified folders, ~/ is essentially the root of the current solution.
For more information on the Server.MapPath method and Virtual Paths see below:
Server.MapPath MSDN Documentation
Virtual Path Utility Class
If you are sure that your application (.exe) and (.txt) files are in the same folder, then just use the file name - do not put the path.
IO.File.OpenText("thefile.txt")
Edited:
Mostly "current working directory" is the same directory where the (.exe) file exists. Yes, sometimes, it is not the same directory. Thus, Application.ExecutablePath would be the right solution.
Dim fn As String
fn = Application.ExecutablePath.Remove(Application.ExecutablePath.LastIndexOf("\")+1) & "thefile.txt"
IO.File.OpenText("thefile.txt")
...
If you meant to say your FileName.txt was always present in the same directory as the assembly, you could do something simple like this:
Function GetAssemblyDirectoryPath() As String
Dim fullAssemblyPath As String = System.Reflection.Assembly.GetExecutingAssembly().Location
Return fullAssemblyPath.Substring(0, fullAssemblyPath.LastIndexOf("\"c))
End Function
This just returns the path to the folder where the assembly resides. The "Solution" might not always be present, but the assembly will, so this will work in both cases (if you make sure the file is always copied to the output directory).
If you're asking about a desktop application, application.ExecutablePath will do what you want. It's not really a good idea though, if your application will reside within Program Files - it's best to avoid writing to anywhere within there, and you will have to run as administrator on post-XP OS's .

How do I publish a folder along with my VB.NET program that contains.html?

I have a VB.NET program that I wish to to publish. In the code it references a HTML page that I created. Instead of having the URL hard coded (example: www.test2.com/folder/index.html)
I would like to have it relative to wherever the encoder is installed to (example: /folder/index.html)
How can I do this?
Thanks!
In VB.Net you have either
Dim lPath As String = Application.ExecutablePath
Which gives you the full path and app.name (which you can remove) or
Dim lPath As String = Application.StartupPath
Which gives you the directory the application started up in ..
Not sure I know the answer to the clickonce question, as we use our own deployment method at work but if you :-
Add your file via Project Explorer
Right click on file, select properties and set Build Action to "content"
Go to Project, yourprojectnameProperties, then Publish tab, then
Application Files button
you should see the file listed there then set to Include(auto).
.... or copy and paste them using internet explorer into the project explorer
If the file is referenced within your solution, the publish operation should automatically include the file to be published.
as for reference, if the html is hosted with in same website, then you can use a relative path from the page you are linking to.
You can also reference the file in your code by using Server.MapPath("relativepathtofile") and the relative path to the file you're referencing.