Attaching Excel File to finished Visual Basic project - vb.net

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?

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.

MODIFIED REPLY AFTER USER COMMENTS
1) First create a Setting (named say MyExcelFile) to save the Name and Path of your excel file.
2) Now you can use My.Settings.MyExcelFile to refer to your excel file path.
3) If excel file is not found at the desired location, you can reset it by opening a File Open Dialog and asking the user to specify the file location.
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
If Not IO.File.Exists(My.Settings.MyExcelFile) Then
Dim ofd As New OpenFileDialog
ofd.Filter = "Excel Files (*.xls, *.xlsx)|*.xls; *.xlsx"
ofd.Title = "Specify Excel File Location"
If ofd.ShowDialog = Windows.Forms.DialogResult.OK Then
My.Settings.MyExcelFile = ofd.FileName
My.Settings.Save()
End If
End If
End Sub

i think iam not really getting what you mean but if you need to open the excel file with one click in the app and you dont want to write the path of the excel file (may be because you want you app portable) so the answer is to
put the excel file anywhere in the project folder
add existing item (*.* filter) and select the excel file
change the properties of the excel file build action: resource Copy to output directory:copy always
open the properties of the solution and click resources the drag the file and drop it
use the (using system.reflection & using system.io & using system.resources)
then write the code like this
string sPath = Path.GetTempFileName();
File.WriteAllBytes(sPath, Properties.Resources.excel File name here);
then take that (spath) and use it as your path so then you can change the project location with the excel file inside it and it will still be working without writing a constant path for it

Related

How to Use OpenFileDialog to copy a file with datetime stamped filename from souce Dir to target Dir IN VB.NET

C:\Restore\databasefile.accdb
D:\Backup\xyz.accdb
I wish to copy xyz.accdb from "D:\Backup" to "C:\Restore" (the xyz.accdb should be renamed to 'databasefile'.accdb when copied to "C:\Restore").
the filename 'xyz'.accdb is a datetime stamped which is not unique at some point . example is 04-04-2021.accdb, 04-05-2021.accdb and so on.
My question is, how can use OpenFileDialog to copy a non-unique filename from D:\Backup\xyz.accd to C:\Restore\databasefile.accdb (filename renamed to 'databasefile'.accdb)
i am using VB.NET - Visual studio 2019
I will highly appreciate if someone can help me out with a code or directives how to archive this. Thanks
If all you need is to copy a file selected by the user through an OpenFileDialog, you can do it like this:
Dim open As New OpenFileDialog()
open.Title = "Select the file you wish to copy"
If open.ShowDialog() = Windows.Forms.DialogResult.OK Then
IO.File.Copy(open.FileName, "C:\Restore\databasefile.accdb", True)
End If

Automaticaly open xls file on program start?

To provide our users to edit excel files without ms excel, we have made a simple app with visual studio 2012 and devexpress Spreadsheet module.
It is very simple to open excel file and use it.
But now only one excel file is being used (with multiple sheets), and I would like the file being used to be opened always on startup.
If I add the path and filename to command line arguments, noting happens...
Using devexpress components is very different then vanilla code for me, I am a complete beginner here, so I have no idea how to continue - can someone, please point me in the right direction?
I have made a procedure, to open the file dialog and load the file - I don't know how to "pass" it to devexpress, so the file actually loads to workbook.
Private Sub OpenXls()
Dim ofd As OpenFileDialog = New OpenFileDialog
ofd.DefaultExt = "xls"
ofd.FileName = "FILE"
ofd.InitialDirectory = "C:\ref_files"
ofd.Title = "Select file"
End Sub
As you have pointed out - using the dialog is not the right way.
After some googling I have find out that this should be a better way:
Dim workbook As New Workbook
workbook.LoadDocument("C:\ref_files\file.xls", DocumentFormat.xls)
I do not get any error, but the file is also not shown...
Do I have to display the document manually after loading?
If you're using openfiledialogue to open file then you must use command to load the specific file at Form.Load event.
The backslash char in ofd.InitialDirectory = "C:\ref_files"escapes the letter R to carrige return.
Change this line to ofd.InitialDirectory = "C:\\ref_files" (add another backslash).

Open a .exe that is in project folder

The issue I am having is opening a program that's in my project folder after being published. here is my code:
Private Sub B_OpenCruc_Click(sender As Object, e As EventArgs) Handles B_OpenCruc.Click
System.Diagnostics.Process.Start("F:\Deploy\myprogram\Project\myprogram\myprogram\Crucible\Crucible.exe")
End Sub
now it works on my pc but not other pc's. I figured it's because its a full path. I also set properties to "content" and "copy always"
I tried using Dim Path As String = ("\My Project\Crucible\Crucible.exe") I would receive the following error
System.ComponentModel.Win32Exception: 'The system cannot find the file specified'
also this as well:
Severity Code Description Project File Line Suppression State
Warning Assembly 'Crucible\Crucible.exe' is incorrectly specified as a file. SCOfflineLoader
when I publish it.. the files are there but my program can't open it.
I can't seem to figure this out, is there a solution to this?
Basically the Goal I want to achieve is When I click a Button It will open a .exe
OR
When I click a button It will Open a .exe that the Target user already has (i.e notepad.exe)
Thanks.
You can use the Application.StartupPath property to get the directory where your application is running from.
Dim path As String = Path.Combine(Application.StartupPath, "Crucible.exe")
System.Diagnostics.Process.Start(path)

How to Auto Copy text file from one folder to another folder in vb 2010 console or windows application

I want to create a program that auto copy text file from one folder to another folder . is it possible to make in windows form in vb.net ? if not what about in console apps ? i tried to search but i didn't find an answer for both. please help me i'm new to to this. I want to copy all the text file that is being save to c:folder1\test1.text copy to c:folder2\test1.text then test2.text,test3.text all the text file that are being put in folder1. i want to copy in folder2.
now i only have this code:
it will only copy 1 specific textfile with file name test.txt.
enter code here
My.Computer.FileSystem.CopyFile("C:\CopyTo\test.txt",
"C:\CopyHere\test.txt")
Of course! First of all we need a function that search for files.
Public Sub SearchFiles(ByVal Pattern As String, ByVal Path As String, ByVal FilesFound As ArrayList)
FilesFound.AddRange(Directory.GetFiles(Path, Pattern))
End Sub
But where we should save the list of files? We can use a Array for it. Also we should define our output and input folder
Dim files As New ArrayList
Dim inDir As String = "input path"
Dim outDir As String = "output path"
We can now call this function like this:
SearchFiles("*.txt", inDir, files)
All .txt files in the folder are now saved in our Array List. But how we can work with it? We can now work with it like this:
Try
For Each file As String In files
Dim fName As String = Path.GetFileName(file)
My.Computer.FileSystem.CopyFile(file , outDir & "\" & fName, overwrite:=False)
Next
Catch ex As Exception
Console.WriteLine(ex.ToString)
End Try
This will copy every .txt file that where found in our inDir to our outDir. If something goes wrong then you will see this in the console. Try it out and understand how it works :)

VBA not working in 2016 as it did in 2013: suggested file name (InitialFileName) not showing up

I have a code to export an Excel tab and save it as a new file with a preset file name. The user has the option to review the file name before saving. This is what I have been using:
InitialName = SaveString & UniqueString
fileSaveName = Application.GetSaveAsFilename(InitialFileName:=InitialName)
If fileSaveName <> False Then
Export.SaveAs (fileSavename)
End If
SaveString is the save folder, and UniqueString is the specific file name, which changes each month. The user clicks Export, the tab is prepared, and the Save As folder pops up in the correct folder with the suggested file name. As long as the user hits "Save," then the exported tab is saved in the SaveString folder with the UniqueString name (with .xlsx already included in UniqueString).
Since upgrading to Office 2016, the UniqueString suggested file name no longer shows up. The Save As pop-up still opens in the SaveString folder, but there is no suggested file name. If the user isn't careful to manually add .xlsx to the end of the file name, then the file type is an unusable "File."
I've opened Excel 2013 in a virtual setting and run the code side-by-side, and it works perfectly in the older version. Does anyone have insight as to why this change happened, and how to correct it?
It appears that you now need to include a file filter that matches the initial name you provide, so the following will possibly work:
fileSaveName = Application.GetSaveAsFilename(InitialFileName:=InitialName, _
FileFilter:="Excel Files (*.xlsx),*.xlsx")