I want to keep open the PowerShell console once the execution has finished.
Actually, after the .exe execution, the PowerShell shut down. I would like to review the outputs once the execution stops, but I can't with the current code.
Sub Button_run_st()
Dim strPName As String
Dim strA As String
strPName = Range("C14").Text
strA = "--dd=" & Application.ActiveWorkbook.Path
Call Shell("" & strPName & " " & strA & "", vbNormalNoFocus)
End Sub
Does anyone know how to do it?
Related
Here is my code.
My purpose is, open a test excel and then save as a filename contained within the 'Test' sheet. I simply want to automate the task of saving an Excel for each of a list of filenames.
Sub POPButton1_Click()
Dim i As Long, LastRow As Long
LastRow = Test.Range("A" & Rows.Count).End(xlUp).Row
Dim filename As String
filename = ThisWorkbook.Path & Application.PathSeparator & "Test.xlsx"
Dim sjk As Workbook
Set sjk = Workbooks.Open(filename)
Dim saveName As String
For i = 1 To LastRow
saveName = Test.Cells(i, "D").Value
sjk.SaveAs ThisWorkbook.Path & "\" & saveName
sjk.Close
Next i
End Sub
The first excel is saved just fine, then I hit the bug. -2147221080 Automation error.
The line of code that highlights on debug is:
sjk.SaveAs ThisWorkbook.Path & "\" & saveName
I have looked around on this site and many others, as it appears a common bug, and I get the feeling it is an easy fix, but nothing I have tried has worked. I have re-written the code many times to get it to this point - I just can't see where the error is...
Ah! I think the answer is that you close the file but then never re-open it.
Move your Set sjk = Workbooks.Open(filename) inside your For loop at the top.
That should fix it for you.
I am trying to debug the following code :
Public Sub StartExeWithArgument()
Dim strProgramName As String
Dim strArgument As String
strArgument = ""
'Call Shell("""" & strProgramName & """ """ & strArgument & """", vbNormalFocus)
Call Shell(Application.ActiveWorkbook.Path & "\eclipse.exe ", vbReadOnly)
End Sub
This code runs when I click on a particular button in excel
However, when I run the code/ click the button, I get the following error:
Run time error 5
Invalid procedure call or argument
I'm modifying a Gantt chart excel template I found online by Vertex42 for added functionality.
One of these modifications is a checkbox inside a sheet called "Config" that, when ticked, creates a backup of the Gantt chart whenever the document is opened.
For some reason, I cannot get this simple task to work.
I've tried using both the Form control and ActiveX control check boxes, with different error messages. As far as I can tell, the Form controls are unrecommended, so I'm using the code below in the ThisWorkbook excel object, based on what I've seen online.
Private Sub Workbook_open()
Dim backupFilename As String
Dim formattedDateTime As String
If Sheets("Config").OLEObjects("AutoBackupCheckbox").Object.Value = True Then
formattedDateTime = Format(Now, "d-MMMM-yyyy, h:mm:ss")
backupfilename = Replace(ActiveWorkbook.Name, ".xlsm", " - backup " & DateTime & ".xlsm")
ActiveWorkbook.SaveCopyAs (backupfilename)
End If
End Sub
This code is getting me the error message whenever I open the document or run the debugger,
Run-time error '1004':
Sorry, we couldn't find the <filename> - backup <day>-<month>-<year>, <hour>:<minute>:<seconds>.xlsm. Is it possible it was moved, renamed or deleted?
Any ideas?
UPDATE: After running the debugger, it's complaining on the ActiveWorkbook.SaveAs line.
UPDATE 2: Changed format of 'backupFilename' to remove the '.xlsm' in the middle.
UPDATE 3: Replaced Date with date/time without slashes, and replaced SaveAs with SaveCopyAs. Updated error message.
The argument for the SaveCopyAs call is missing the path of the file.
Replace code with
Private Sub Workbook_open()
Dim backupFilename As String
Dim formattedDate As String
Dim tempFilename As String
Dim workingPath As String
Dim i As Integer
i = 1
If Sheets("Config").OLEObjects("AutoBackupCheckbox").Object.Value = True Then
formattedDate = Format(Date, "d-MMMM-yyyy, ver " & i)
workingPath = Application.ActiveWorkbook.FullName
backupFilename = Replace(workingPath, ".xlsm", " - backup " & formattedDate & ".xlsm")
tempFilename = Dir(backupFilename)
While tempFilename <> "" ' if file already exists
i = i + 1
formattedDate = Format(Date, "d-MMMM-yyyy, ver " & i)
backupFilename = Replace(workingPath, ".xlsm", " - backup " & formattedDate & ".xlsm")
tempFilename = Dir(backupFilename)
Wend
ActiveWorkbook.SaveCopyAs (backupFilename)
End If
End Sub
I'm needing to implement a macro that runs after autoit and finished running the program it runs the rest of the macro.
I tried the Shellandwait(), but I did not find documentation explaining about it.
I took other examples of code that forum and got this:
Sub autoit()
Dim hProcess As Long
Dim xPath As String
Dim wsh As Object
Dim waitOnReturn As Boolean: waitOnReturn = True
Dim windowStyle As Integer: windowStyle = 1
Set wsh = CreateObject("WScript.Shell")
xPath = Application.ActiveWorkbook.Path
hProcess = wsh.Run("D:\Program Files\autoit-v3\install\AutoIt3_x64.exe " _
& xPath & "\leandro.au3", 0, True)
Workbooks.Open (xPath & "\Mudança " & Format(Date, "dd_mm_yyyy") & ".csv")
End Sub
When I run it returns me this error:
"Run-time error '-2147024894 (80070002)': Method 'Run' of object 'IWshShell3' failed"
I do not know what it means and I have no idea solution.
If xPath has any spaces in it you will need to wrap the expression in quotes.
Try something like this instead:
xPath = ActiveWorkbook.Path
With CreateObject("WSCript.Shell")
.Exec "CMD /C START /WAIT ""D:\Program Files\autoit-v3\install\AutoIt3_x64.exe"" """ & xPath & "\leandro.au3"""
End With
Let me share two examples of VBA code for capturing the output stream from the AutoIt script (reads from the STDOUT stream)
VBA code:
Dim sFilePath As String
Dim objShell As Object, objCmdExec
sFilePath = String(1, 34) & Application.ActiveWorkbook.path & Application.PathSeparator & "test0.au3" & String(1, 34)
Set objShell = CreateObject("WScript.Shell")
Set objCmdExec = objShell.exec("""C:\Program Files (x86)\AutoIt3\AutoIt3.exe"" " & sFilePath)
MsgBox (objCmdExec.StdOut.ReadAll)
This is the AutoIt script for the first test (test0.au3):
Global $iPID = Run("notepad.exe", "", #SW_SHOWMAXIMIZED)
WinWait("[CLASS:Notepad]", "", 10)
Sleep(2000)
Send("Hello")
Sleep(1000)
ProcessClose($iPID)
ConsoleWrite("Done" & #CRLF)
Another option i'm finding useful is this vba class:
VBA Run Application - Capture Output. Author: dmaruca. Last updated 2012
Here an example:
Dim sFilePath As String
sFilePath = ThisWorkbook.path & Application.PathSeparator & "test.au3"
Dim RunApp As New CRunApp
RunApp.Command = "C:\Program Files (x86)\AutoIt3\AutoIt3.exe"
RunApp.AddParamater sFilePath
RunApp.AddParamater "Buenos días"
MsgBox RunApp.RunAppWait_CaptureOutput()
And the script for testing (test.au3):
MsgBox(4096, 'AutoIt MsgBox', $CmdLine[1])
ConsoleWrite($CmdLine[1] & #CRLF & "From: " & #ScriptName)
I'm new to programming. I've made a simple codeblock that runs a Windows program POSTOPEN when I open an application in Lotus Notes 8.5.3 (basic)
' [ML]Check if user is member of the [ConnectClient] role.
ipConnect = HasRole("[ConnectClient]")
If ipConnect = True Then
Dim result As Integer
Print "Postopen: Has [ConnectClient] role - starting Connect Client"
result = Shell("c:\program files (x86)\ipvision\Connect\connect.exe", 1)
End If
Is there any way for Lotus Script to check if a Windows process is already running?
Thanks
According to this snippet: http://blog.panagenda.com/pub/panablog.nsf/d6plinks/FLOR-7D5KZR
I assume following code could work: http://coderstalk.blogspot.sk/2009/10/list-windows-xp-running-process-and.html
Option Explicit
Dim oProc, oWMIServ, colProc
Dim strPC, strList
Dim StrSpace
strPC = "."
Set oWMIServ = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strPC & "\root\cimv2")
Set colProc = oWMIServ.ExecQuery("Select * from Win32_Process")
strSpace = string(20," ")
strList = "ProcName" & strSpace & vbTab & "ProcID" & vbCrLf & string(45,"-")
For Each oProc In colProc
strSpace = string(28 - len(oProc.Name)," ")
strList = strList & vbCrLf & oProc.Name & strSpace & vbTab & oProc.ProcessId
Next
So once you have process ID (assuming you have ran your task by shellid function), execute chceck for all running processes and determine whether there is processid you have started.
BTW: the same approach can be used to terminate such process. Just google for >"winmgmts" lotusscript<.
Use Shell to run tasklist and direct the output to a file. Then read the file in LotusScript and check if the process is listed there.
edit
Looks like you can even check if a specific process if running using tasklist:
tasklist /FI "IMAGENAME eq connect.exe" /FO CSV > search.log
How to check if a process is running via a batch script