Save and open vbs script programmatically - vb.net

I have searched a lot on this method but no way
I want my VB Program as once opened it gives user some messages using vbs script file
so once program is opened the vbs file saved in temp and be ran to say the message to user
i have used this code with the vbs file imported to Resources
but unfortunately it works only with one line script not script with many lines
Dim Variable As String = Environ("temp") & "Message.vbs"
If Not System.IO.File.Exists(Variable) Then
System.IO.File.WriteAllBytes(Variable, My.Resources.Message)
End If
Process.Start(Variable)
i have used this second code as well but it gives error in compilation because of Save
Dim Variable As String = Environ("temp") & "\Message.vbs"
IO.File.Delete(Variable)
My.Resources.Access.Save(Variable )
Shell("explorer" & Variable )
Please Help me with this code, i have spend a long time to get solution but nothing
Thanks in advance

I made some minor changes to your first example and created a simple vbs file. The first change is because a VB script file is a just a text file I added the resource as a text file and changed the WriteAllBytes statement to WriteAllText. Second, because WriteAllText, (and WriteAllBytes also), overwrite the file if it already exists I eliminated the If statement. You may still want that, in case you really do not want to overwrite the file. Finally, I added a backslash to the beginning of the filename to create the file in the temp folder. Otherwise you get drive:\temppath\tempMessage.vbs". This executed my simple script file just fine.
.NET code:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim Variable As String = Environ("temp") & "\Message.vbs"
System.IO.File.WriteAllText(Variable, My.Resources.hello)
Process.Start(Variable)
End Sub
My VBScript file:
MsgBox "Hello World", 0,"Messagebox #1"
MsgBox "My name is Fred", 0,"Messagebox #2"
MsgBox "I have to go now", 0,"Messagebox #3"

Related

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

Use VBA to retrieve and call VBA code from a text file

I am working on a project to process some incoming Outlook messages using rule-triggered VBA code.
However, I don't want to have to manually update the code for each user's inbox any time the code needs to change. So my idea is to put a text file on a shared drive and have the VBA pull that text file down and treat it like code. Essentially, I want to use that text file like a little code library.
I was able to find this link that has gotten me very close to my goal. However, I'm having a few issues with it.
Here is the code I put together. It is attached to the click event of a Rectangle shape I inserted into an Excel file. Eventually, I'll move this over to Outlook, but I'm just doing a basic test with Excel VBA first.
Sub Rectangle1_Click()
On Error GoTo Err_Handler
Dim enviro As String
Dim myFile As String
'Pull code "library" from text file on user's desktop
'This will eventually be changed to reside on a shared drive
enviro = CStr(Environ("USERPROFILE"))
myFile = enviro & "\Desktop\hello_vba.txt"
'If the "Library" module already exists, delete it
For Each a In Modules
If a.Name = "Library" Then
a.Delete
Exit For
End If
Next
'Add a new module
Set m = Application.Modules.Add
'Rename it to "Library"
m.Name = "Library"
'Insert the text from the other file to this new module
m.InsertFile myFile
'Call the hello() subroutine from the retrieved text file
Library.Hello
Exit_Here:
'Cleanup code goes here
Exit Sub
Err_Handler:
MsgBox Err.Description
Resume Exit_Here
Exit Sub
And here is the content of the external text file named "hello_vba.txt":
Sub Hello()
MsgBox "Hello"
End Sub
The first time I run this, using the debugger I can see that it creates the new module and then gets to the line that says:
m.Name = "Library"
And then a window pops up in the debugger that says:
Can't enter break mode at this time
When I click continue on that message, I get an
Object Required
error message. If it run it again, then I get some more error messages but I do eventually get a successful "Hello" message box pop up.
I'm wondering if I may not have "Dim"ed the "a" or "m" variables properly or if there is a problem trying to pull in a text file and immediately treat it like code?
Any ideas?
To run your new code, try using Application.Run instead of Library.Hello
It would be written:
Application.Run("Hello")
Does that work?

Get file from "Windows Explorer - Open With"

I'm not quite sure where to start with this. On right-clicking on a generic file in Windows Explorer (e.g. *.doc for a Word document) one can choose "Open with...". I 'd like to know how the program knows what file has been "passed" (is that the right word?). Is it done via arguments? How can I implement this in my own application?
I tried manually adding a file path to the arguments of one of my applications when it is run, but the path includes spaces (which denotes a new argument). How does Windows get round this/what do I need to do to solve this?
Regards,
Robbie
To retrieve the arguments used from the command line:
Private Sub Form1_Load(sender As Object, e As System.EventArgs) Handles Me.Load
Dim sMsg As String = ""
For Each sArg As String In My.Application.CommandLineArgs
sMsg &= sArg & ": "
Next
MsgBox(sMsg)
End Sub
Place the code in the WinForm _Load, Console Main, etc.
If the above was run with: Hello World as the command line Hello: World: would display.
Here is some VB code to open a file:
Case Keys.F4
Process.Start("H:\OIS\PROCEDUR\OIS8ProcedureManual.doc")
In this case Windows looks up .doc in the file types and uses the .doc entry to run Word and pass it the filename.
Process.Start has a second parameter that contains Arguments so you could provide a path to an .exe in the first param and the argument(s) in the second. Actually there are 5 signatures for Process.start. The most powerful ones uses the ProcessStartInfo class to provide you with the most control.
Post the code you wrote for the second group of questions if the above didn't help.

the process cannot access the file because it is being used by another process in vb.net

help me.. i'm new in visual basic....
when i'm running the update it shows the error
The process cannot access the file 'C:\Documents and Settings\Macky\My Documents\Visual Studio 2008\Projects\Marcelo 2.2.3\Marcelo\bin\Debug\Students\MIC953867.jpg' because it is being used by another process.
my code is this
Public Sub copingfile()
If inFileName = Nothing Then
studpic.Image = Nothing
Else
outFileName = inFileName
pos = inFileName.LastIndexOf(".")
If (pos > 0) Then
outFileName = outFileName.Substring(0, pos)
End If
outFileName += ".jpg"
str = Application.StartupPath & "\Students\"
saveJPEGFile.FileName = str & StudID.Text & ".jpg" '& outFileName
fil1.Copy(inFileName, saveJPEGFile.FileName, True) 'the error shows here...
outFileName = saveJPEGFile.FileName()
End If
End Sub
I can save new student information with picture.. but when it comes in updating the picture these codes didn't work......
fil1.Copy(inFileName, saveJPEGFile.FileName, True)
You're attempting overwrite a file that's open or being used. If the file is open in a viewer/editor, then it can't be copied over. Either you opened it manually, or did so through code and it's still "attached" to something running.
If it's not open in a window, try stopping your code and deleting that file manually. If you can, it's pretty obvious something in code is still using it when you get to the line that errored. You'll need to figure out where that file is still being used (Open stream somewhere? Open in VS, itself?), as it doesn't appear to be in the code you provided.
You are going to need to show more code, you are using variables not in your code listing. Plus you do not show the code that originally saves your image.
But here is my guess...are you sure you closed the file when you saved it for the first time? You cannot generally copy to, or from, a file that is open.
(Files can be opened as shared, but I don't think you are doing that).
Post more code if you get a chance.

Strange Error When Using Tesseract in VB.net

I have the current code:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim Bitmap As New Bitmap("image.png")
Dim ocr As tessnet2.Tesseract = New tessnet2.Tesseract()
ocr.SetVariable("tessedit_char_whitelit", "0123456789")
ocr.Init("c:\", "fra", False)
Dim result As List(Of tessnet2.Word) = ocr.DoOCR(Bitmap, Rectangle.Empty)
For Each word As tessnet2.Word In result
RichTextBox1.Text &= word.Text & "(" & word.Confidence & ") "
Next
End Sub
I just have a normal RichTextBox and a button on the form. I also have an image in the debug directory called "image.png".
Every time I run this, the program just closes. I did a step through and all of a sudden a file locater came up asking for "tessnet2.cpp"
I have a reference to the dll. I also don't know what the ocr.Init(...) line is for.
Any help would be nice!
First of all, thank you very much for your simple but effective code. After 3 days search I found this code for VB (not VC). Of course I copied and pasted it immediately and the same problem occured for me, too. Then:
I uninstalled Tesseract 3.xx
Checked RegEdit for Tesseract 3.xx and deleted them (whoever want to do this step; please be careful not to destroy anything)
Copied tessdll.dll in the same folder.
The main problem is:
ocr.Init("c:\", "fra", False) it should be something like this:
ocr.Init("c:\tessdata", "fra", False) in fact my real line is:
ocr.Init(Application.StartupPath & "\tessdata", "eng", False)
Noticed that in the folder "...\Visual Studio 2008\Projects...." I still had the same problem and then copied all folder in "D:\Test" folder (of course in this folder I have one more folder: tessdata)
It worked!!!
Hope it helps for you or anyone searching for this problem like me :)
Nes
If you put your code inside a Try/Catch block, you should be able to find out what the error is without your program closing. You could also debug the program instead of running it, and instead of the program crashing, the debugger will show you exactly where the error is happening.
The first parameter of Init method specifies the location of tessdata folder. If you have it at the default location, which is the same as that of Tesseract binary, it should be null, or Nothing in VB.NET.