How to start multiple process shell commands in VB.NET 2008 - vb.net

I need to open several pdf, word and excel files from process.start like command, but only open one file at time.

You can use WaitForExit with a process to pause execution until the application that handles the pdf, word, etc. file is closed. This will work if the user closes, for example, word instead of closing only the word document and leaving the word application running.
Dim proc As Process
proc = Process.Start("c:\tmp.jpg")
proc.WaitForExit()
proc = Process.Start("c:\tmp1.jpg")
proc.WaitForExit()

Its work without "proc.WaitForExit()", I can get several documents open at the same time with the following code:
Private Sub OpenDocument(ByVal strDocName as String)
Dim proc as Process
proc = Process.Start(strDocName)
End Sub

Related

How to run a .cmd file from VBA

I am writing a macro for a Solidworks PDM Task.
I have manage to write into the script in the Task, that it should start a specified macro. This works.
In the macro I would like to open a PcSchematic file (.pro) and the run a .cmd file, which will ask PcSchematic to convert the file into a PDF. And then close the program.
I am novice in VBA - I have found and edited this macro, which opens PcSchematic, but not the file and I don't know how to proceed.
Sub Auto_Open()
Dim x As Variant
Dim Path As String
' Set the Path variable equal to the path of your program's installation
Path = "C:\Pcselcad\Pcselcad.exe"
x = Shell(Path, vbNormalFocus)
End Sub
Right now we use a Dispatch to do the trick, with a Shell execute:
Where the commandline says: C:\Pcselcad\Pcselcad.exe "%PathToSelectedFile%" C:\ODIN\administration\Dispatch\SaveAsPDF.cmd
This method is working, but it is not closing down the PcSchematics when it is done. And I would like to start it with a task, which gives me some other benefits, this is why I want to do it with a macro.

How to read VB formatted code from a text file?

I writing a pair of programs in visual basics that will consist of a client and receiver. The client is completed making an output text file similar to below.
Dim FileName As String = "Text.txt"
Dim message As String
Dim file As System.IO.StreamWriter
'lblmessage.text says "Call MsgBox("Hello!", 0, "Version Beta 0.3")"
lblmessage.text = message
Dim Drive As String = "C:\hello world\" & FileName
file = My.Computer.FileSystem.OpenTextFileWriter(Drive, True)
file.WriteLine(message)
file.Close()
A sister program that is designed to be a reader will read the generated file.
The program will then take the text located in the selected file and use it as code in the readers programming.
Best example I can show...
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim fileReader As String
fileReader = My.Computer.FileSystem.ReadAllText("C:\hello world\Text.txt")
fileReader
End Sub
where "fileReader" is suppose to run the generated code from the previous program and use it as code it the Reader.
The point of this program is to create help tickets in the client and have a tool for reviewing these ticket in the same way they were submitted through the second app.
vba and vb.net is different. I'm not sure in which language your doing your stuff.
vba: use the ScriptControl.Eval command to execute a bunch of commands.
vb.net: it's a bit more code. you can use VBCodeProvider Class. s this SO Question: Load VB.net code from .txt file and execute it on fly using System.CodeDom.Compiler
UPDATE
I found a perfekt resource if you are interested in how to do it right and how it works in the background: http://www.codemag.com/article/0211081

Save and open vbs script programmatically

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"

Releasing memory held in objects in VB.NET

I am using a 3rd party DLL from pdf-tools to parse PDF files inside my VB.NET application and write the extracted data to a SQL localDB database. I'm giving the user two options: to select a PDF file for parsing or to point to a folder and the application will loop through all PDF files inside it. Both options call the same procedure doPDFFile() as below.
The problem is: if I import a number of files individually by selecting a file every time, the program runs fine. However, If I select a folder that contains the same files, the memory used by the program will keep growing further and further. In Windows task manager, it can reach to 1 GB after importing around 30 files.
I used ANTS memory profiler from redgate, and it showed that one object called "GraphicsState" which is part of the pdf-tools object is growing too big when looping inside a folder. This does not happen if I select the same files one by one. Besides the memory problem, the application becomes very slow after parsing some files. My questions is: why is this happening? and how to prevent it? The user should be able to point the program to a folder with hundreds of PDF files, how can I achieve this?
Below is a snapshot of the code:
'When user selects one file
Private Sub OpenToolStripMenuItem_Click(...)
OpenFileDialog1.FileName.ToString
doPDFFile()
End Sub
'When user selects a folder
Private Sub LoopToolStripMenuItem_Click() Handles LoopToolStripMenuItem.Click
FolderBrowserDialog1.ShowDialog()
sPath = FolderBrowserDialog1.SelectedPath
For Each fileName As String In IO.Directory.GetFiles(...)
sPath = fileName
doPDFFile()
Next
End Sub
Inside the doPDFFile() procedure, I'm doing the following, I'm using the document object from pdf-tools and I'm passing it byRef to another procedure:
Public Sub doPDFFile()
Using document As New Pdftools.PdfExtract.Document
document.open(sPath)
findFirstPage(document) 'passing by reference
ParseFirstPage(document) 'passing by reference
'storing the parsed text in an array
'.......
do
'extracting the colors from the graphicsStateObject inside the document object:
Using objGraphicsState As Pdftools.PdfExtract.GraphicsState = content.GraphicsState
sColor = objGraphicsState.FillColorRGB
End Using
'save text and color in an array of objects
until endOfText
end using
end sub

How do I kill all open powerpoint process?

I am trying to kill all open powerpoint processes but the code I wrote kills only one open process.
'-- get a collection of processes running
Dim foo() As System.Diagnostics.Process = System.Diagnostics.Process.GetProcesses
'-- go through each one looking for the internet explorer name
For Each temp As Diagnostics.Process In foo
'For Word Files opened in Office
If temp.ProcessName = "POWERPNT" Then
temp.Kill() '-- if I find it, kill it.
' Exit For '-- exit the for loop
End If
Try
Dim foo() as process = Process.GetProcessByName("POWERPNT")
For Each temp As Process In foo
temp.Kill()
Next
You are only going to find the single instance of the POWERPNT.exe, because
Multiple instances of Word (Winword.exe), Excel (Excel.exe), and Microsoft Access (MSAccess.exe) can run simultaneously. Therefore, these servers are defined as Single Use (Multiple Instances) servers. Only one instance of PowerPoint (Powerpnt.exe) can run at any given time. Therefore, PowerPoint is a Multiuse (Single Instance) server.
Read How to use Visual C# to automate a running instance of an Office program for complete documentation.