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.
Related
So I am looking to open an access database on a daily basis using task scheduler. I want this script to open the .aacdb and then open a specific form. The problem is, this database has an auto executable that opens a different form that will close the program when exiting out of it. I need to somehow bypass this executable. I've been trying to write a .vbs script that will use the hold down shift key (to bypass the auto executable) > open database > release shift key > open form > close database. After days of searching, I can't seem to find any answers on whether this is possible. Here is what I have so far (it opens the database but doesn't bypass the auto executable). The aim of this script is to hold down the shift key for 10 seconds while opening the database.
b = DateAdd("s", 10, Time)
Set WshShell = CreateObject("WScript.Shell")
Set appAccess = CreateObject("Access.Application")
appAccess.Visible = True
strDBNameAndPath = "C:\FileFolder\file.aacdb"
Do While (Time < b)
WshShell.SendKeys "+"
Loop
appAccess.OpenCurrentDatabase strDBNameAndPath
Set WshShell = Nothing
I can't mess with the actual database (the macros), this has to happen by somehow bypassing the actual auto executable upon opening a database. Is this possible in .vbs? .vba? .vb? vb.net? any language?? I'm only familiar with .vbs. But any insight would be helpful. Thanks!!!
So I am using this code in excel to read environment parameters on startup:
Dim ExcelArgs As String
Dim arg As String
ExcelArgs = Environ("ExcelArgs")
MsgBox ExcelArgs
If InStr(UCase(ExcelArgs), "CREO") >= 0 Then
Application.DisplayAlerts = False
If Len(ExcelArgs) > Len("CREO") Then
arg = Split(ExcelArgs, ",")(1)
Call Creo.addNewPartToPartslist(arg)
End If
Application.DisplayAlerts = True
End If
and this line in my batch script:
echo "Launch excel"
Set "ExcelArgs=CREO,DXFWITHOUTDRW
"C:\Program Files (x86)\Microsoft Office\OFFICE16\Excel.exe" /r "%APPDATA%\Microsoft\Excel\XLSTART\PERSONAL.XLSB"
exit 0
The problem is that if i run the batch file once, keep excel open change the excelargs to CREO,wqhatever in batch file and rerun batch file the excelargs, dos not get updated!!!
So my theory is that excel either caches out the environment variable or that if it is being used by one instance the batch script can not set it
link with some info about passing arguments to excel:
https://superuser.com/questions/640359/bat-file-to-open-excel-with-parameters-spaces
Usually excel sees if there is a previous instance running and let this instance handle the file opening.
Is this important? Yes, in your case both requests to open the file are handled by the same excel process.
How does it make a difference? Environment variables are not shared. Each process has it own environment block that is initialized when the process is created (can be a customized block or a copy of the environment of the parent process) and once the environment is created for a process, only this process can change its environment.
In your case, when you start excel the new process gets a copy of the environment block of the cmd process. Later, when you change the cmd environment, the already running excel instance sees no changes in environment and, as the new request to open excel is converted to a request to the previous process, there is not a new process with a new copy of the cmd environment with the changes.
The only way I see to make it work is to force excel to start a new process (that will inherit the changes in the cmd instance) instead of reusing the previous one.
But this depends on the excel version. As far as I know, the 2013 version includes an /x switch to force separate process usage. For previous versions, maybe this question, or this one could help you.
Excel is open
Then i start the batch script:
The it does not open it as read only by default, but prompt me instead, not a big issue but a bit annoying, and it also make it impossible to loop through to run the batch several times for different input parameters.
A bit unsure how I should post this, couldnt paste images in comments, and to edit the the original question, which was how to start excel with enviroment variable in new instance (/x did the trick), but now /r does not work, Should I post as new question and refer to this one or can I leave it as an answer?
I have a program that takes a template .docx file and populates it with data, saving a copy afterwards. The program itself works fine and I got a nice try..catch in main sub in case something fails, so the file is closed regardless.
The problem is that if it crashes completely, i.e. is forced to close (or manually force closed if it hangs), it will keep the process running with the opened template, so next time it;s launched, you'll get the read only error when trying to open it.
So the question: Is there a way to clean up afterwards, without having to end process via task manager? Or maybe a way of opening it without locking it out? Making a temp copy maybe?
Fixed with this:
Sub KillUnusedWordProcess()
Dim oXlProcess As Process() = Process.GetProcessesByName("Winword")
For Each oXLP As Process In oXlProcess
If Len(oXLP.MainWindowTitle) = 0 Then
oXLP.Kill()
End If
Next
End Sub
I am having a text file in this path "C:\Test\test.txt" when this was openeed I need to close this.
When I am trying to use the below code all the instances of notepad are closing and I don't want that to be happened and I want to close only the ".txt" file:
Any help would be appreciated!
Here is my code:
Dim Process() As Process = System.Diagnostics.Process.GetProcessesByName("notepad")
For Each p As Process In Process
p.Kill()
Next
You can look at the
Process.MainWindowTitle
property of p.
Notepad's title will be Filename.txt - Notepad
If you started the process yourself, you can kill it using the Process.Kill() method.
Note that in many (most?) circumstances, killing all instances of a process isn't really a good user experience since the user may have started instances of that process on their own in addition to the instance your program launched / is attempting to close.
You could do something as mentioned about using an if statement. Assuming you opened the file called test.
Dim Process() As Process = System.Diagnostics.Process.GetProcessesByName("notepad")
For Each p As Process In Process
If p.MainWindowTitle.Contains("test") Then
p.Kill()
End If
Next
EDIT:
To check for multiple files
simply add or to the .Contains line
If p.MainWindowTitle.Contains("test") Or ("blahblah") Then
p.kill()
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