Automaticaly open xls file on program start? - vb.net

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).

Related

How to properly import a txt file into a rich text box in visual basic?

I am trying to import a text file into a rich text box for my Visual Basic project (Its a Windows Forms App (.Net Framework)). I copied the text information from the internet, and pasted it into notepad. Then I saved this in a folder I created called txtFiles inside the Bin folder of my project. However, when I used the FileOpen command, it gives me an error.
The code I used:
Dim strTemp As String = ""
rtbReference.Text = Nothing
FileOpen(1, "../txtFiles/Thriller.txt", OpenMode.Input)
Do Until EOF(1) = True
Input(1, strTemp)
rtbReference.Text = strTemp
Loop
FileClose(1)
Picture:
The error that I got when running my program
Any help would be appreciated, thank you so much
There are more easier methods of working with files in the 21st century. I mean the IO namespace provides a handful of methods.
Try any of the following. Add this at the top of your code file
Imports System.IO
Method 0: File.ReadAllText
Since the file you want to load is a plain tet file, I suggest you use this.
RichTextBox1.Text = File.ReadAllText(pathtofile)
Method 1: Richtextbox.LoadFile()
Someone (or something) has already posted the link to an article on this in the comments but i'm pretty sure you've not gone through it. Well here's how to use it
RichTextBox1.LoadFile(pathtofile)
I use this method for only rtf files but it was completely my decision.
Method 2 To n - 1
You need to know how to use a search engine as there are many methods out there that can be used such as the StreamReader and so on.

Activate window of another Visio instance

Currently I have a file name stored in string called filename. The file stored in the string is currently open. Issue is, this file could some times be opened in another instance of Visio.
I want to activate the file that is stored in filename string
My current method does not capture this - The code below only checks if the filename exists among the current/one instance of Visio.
For Each objDoc In objVisio.Documents
If objDoc.Name = filename Then
objDoc.activate
Exit for
End If
Next
How can I activate this file to bring it forward?
windows(filename & " - Microsoft Visio").activate
is not working either
I've tried
Dim objVisio as Visio.Application
Set objVisio = GetObject(filename).Application
which isn't working (maybe due to filename string only having the file name and not the entire file path as well)
Any other brute force methods available out there?
Any help is appreciated!
Try something like this:
objVisio.Application.Caption
Or
AppActivate "Microsoft Visio"
I guess another option is to look into this: https://msdn.microsoft.com/en-us/library/office/ff766749.aspx
I haven't worked extensively with Visio in VBA, so I am interested to see the true answer here.

multiple programming try to access same text file

I had created and text file using (AFL SCRIPTING LANGUAGE) this script will update (write) to a text file every 5 seconds. I will try to read file using vb.net, when I run the vb.net code form visual studio, everything works fine but (AFL script not able to update the text file), here is my vb.net code:
Dim FILE_NAME As New FileStream("C:\myreport\myfile.TXT", FileMode.Open, FileAccess.Read, FileShare.Read)
REM Dim FILE_NAME As String = "C:\myreport\myfile.TXT"
REM Dim TextLine As String
REM If System.IO.File.Exists(FILE_NAME) = True Then
Dim objReader As New System.IO.StreamReader(FILE_NAME)
Do While objReader.Peek() <> -1
MYSTRING(I) = objReader.ReadLine()
I = I + 1
Loop
REM End If
When I run the code above, ( AFL script not able to update the text file),
Put simply:
When I run the vb.net code (for accessing the text file), AFL script not able to update.
I had share read/write the folder (where the text file exists), no effect, same problem I am facing.
Unchecked "Enable visual studio hosting process", still problem not solved.
In .NET, the FileSystemWatcher class can help you with this problem. You can read the file each time the file watcher says that the files has changed. Here is the reference documentation for it.
You cannot read and write to a file from two processes at the same time. Well, technically you can, but it can lead to a race condition which is bad.
You need to implement some kind of shared locking mechanism around the file to prevent your two programs from fighting over it. Or, if you can guarantee that the consumer VB.NET program will have the file open for less than 5 seconds, you can go with MrAxel's solution and simply have the VB.NET program read the file every time it is updated.

I have the file name open...Now how do I actually get the file itself to open?

I have a code that allows me to use an open file dialog to select a file. Once selected, the file path displays in Form3. What I'm trying to do from there is click Command_Button1 (Open) and physically open the file.
I can provide codes and JPEGs...but won't cloud this up until it is necessary :) I'm not using Common Dialog so the code is somewhat lengthy.
I looked online for a few hours yesterday and the closest I got was how to get it to open an Excel file...but this isn't Excel.
P.S. I'm new to VBA...so if this isn't as simple as a few lines of code and I'm missing the difficulty of it, please let me know. It seems like it should be a simple enough process...
Edit 1:
#mehow, Do you think it could be because the whole path name isn't displaying? I double clicked Open and nothing happened like you said
If you already know the path then..
Private Sub CommandButton1_Click()
dim myPath as String
myPath = "C:\files\file1.exe"
Dim shell As Object
Set shell = CreateObject("Shell.Application")
shell.Open myPath
End Sub

Attaching Excel File to finished Visual Basic project

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