Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Attaching Excel File to finished Visual Basic project

Tags:

excel

vb.net

vba

I've created a Visual Basic project with many forms. In the project I write information to an excel file that I reference the location of on my local (C:) drive where the excel file resides. By reference I mean, I open an excel instance ten provide the source=local path on my hard drive. My question is how do I attach the excel file to the project so I don't have to reference the local location and can run the program on a different computer. In other words how do I bundle the excel file with the program? And how would I change the call to the excel file once I have it "bundled" to the program?

like image 751
CaffeinatedMike Avatar asked Dec 12 '13 16:12

CaffeinatedMike


1 Answers

Add the excel file to the project. Go to solution explorer and right click on the excel file and select properties. Change the Copy to Output Directory to Copy Always. Now the file will be in the same directory as your exe. You can use Reflection.Assembly.GetExecutingAssembly to get the directory.

To add the file to the project:

Right click on project > Add > Existing Item > Your Excel File.xls

To include the file in build:

Right click on the file > Properties > Copy to Output Directory. Set this value to either Copy always or Copy if newer.

Here is the code to get the path to the excel file:

Dim exeDir As New IO.FileInfo(Reflection.Assembly.GetExecutingAssembly.FullName)
Dim xlPath = IO.Path.Combine(exeDir.DirectoryName, "Your Excel File.xls")

xlPath should now be the full path to the excel file.

like image 76
Jeff Avatar answered Sep 28 '22 05:09

Jeff