Open Windows Command Script with VBA - vba

Can I use a macro in Outlook to open a Windows Command Script file? The file I have is called Build_Order_File.CMD
I did try the Shell command but I got the error "Invalid procedure call or argument"
Here is what I tried
Dim Q2CFile
Q2CFile = Shell("C:\Users\SESA27956\Desktop\Build_Order_File.cmd", 1)
The attached image shows that it is expecting an = after the pathname.

Related

Is there a way to write a command directly by code in the immediate window?

I'm trying to save all lines from the output window into a log-file. That works fine when I enter the command directly into the immediate window. However, I need a way to enter the command line by code.
I searched for a way to enter a command into the immediate window so the log-file I want will be created and filled with data. That's not happening and I don't know if this is even possible. Searching Google didn't help so far.
My command line is:
> Tools.LogCommandWindowOutput C:\Users\user\AppData\test.log
And I try to get it into the immediate window by
Debug.Print("> Tools.LogCommandWindowOutput C:\Users\user\AppData\test.log")
The command line works fine when put directly into the immediate window (either by typing it in or pasting it in). When I try to do that by code, nothing happens. Is there a way to do that or do I have to try another option and if so, what option is it?
EDIT:
I've tried the solution provided by RobertBaron with the following changes:
'Dim dummy = $"C:\\log\\outputfileAddOn_{Date.Now:yyyy_MM_dd_HH_mm_ss}.log"
'cmdWindow.SendInput($"Tools.LogCommandWindowOutput {dummy}", True)
cmdWindow.SendInput("Tools.LogCommandWindowOutput C:\log\outputfile_AddOn.log", True)
(I want a new file to be written every time, so I tried to add the date at the end to have unique file names)
It creates the file but doesn't log anything in it. What do I do wrong?
Another solution I have found was to add a command parameter in project-properties-debug-command parameter:
> C:\log\outputfile.log
This creates the file and inserts all of the data from the output window. The only problem that I have now is that this file will be overwritten every time the program is started. Is there a way I can set the logging from the second, third, … start at the end of the file? (adding /on at the end of the command Parameter didn't help) Or can I provide something like "outputfile_yyyy_MM_dd_HH_mm_ss.log" (for example: outputfile_2019_07_23_15_47_45.log)?
Thank you for your help!
You need to create a Visual Studio Extension project.
Add a new Custom Command item to the VSIX project.
This will create the Command1.vb. This is where you implement the code that you want to execute inside Visual Studio. The code executes whenever you select the entry Invoke Command1 from the Tools menu.
At the bottom of the Command1.vb file, there is the Execute() Sub. It displays a message just to show that the command is working. You can remove this eventually. But for now, just run the project. Another instance of Visual Studio will start. This is where you can test and debug your code. Once the second instance of Visual Studio has started, go to its Tools menu, and select the Invoke Command1 entry. A message box will display. Stop execution.
Now we want to modify the Execute() Sub so that our code gets executed when Invoke Command1 is selected. Here is the Sub that will execute the command that you want. Add it to the Command1 class.
Public Sub ExecCommandWindow()
Dim dte As EnvDTE.DTE = CType(Microsoft.VisualStudio.Shell.Package.GetGlobalService(GetType(Microsoft.VisualStudio.Shell.Interop.SDTE)), EnvDTE.DTE)
Dim cmdWindow As CommandWindow = CType(dte.Windows.Item(EnvDTE.Constants.vsWindowKindCommandWindow).Object, CommandWindow)
'Display some text in the command window
cmdWindow.OutputString("Executing command from the automation OM...")
'Send some command strings to the command window and execute them...
'This command will start logging all input/output in the command window to the specified file
cmdWindow.SendInput("Tools.LogCommandWindowOutput cmdwindow.log", True)
''Open a file in a code editor:
'' 1. We use an alias, 'of', for the File.OpenFile command
'' 2. This command takes quote-delimited parameters (in this case,
'' the name of the editor to load the file in)
'Dim cmd As String = "of "
'cmd = (cmd + """""C:\Contoso\ContosoCommonFramework\Integration.cs""""")
'cmd = (cmd + "/e:""""CSharp Editor""""")
'cmdWindow.SendInput(cmd, True)
'cmdWindow.SendInput("Edit.Find MessageTrxId", True)
'Turn off logging
cmdWindow.SendInput("Tools.LogCommandWindowOutput /off", True)
End Sub
Change the Execute() Sub to call ExecCommandWindow().
You can change the name of the command by changing its title in the Command1 class.
The Visual Studio extension needs to be installed by each user. Just distribute the .vsix file. To install, double-click it.
It's not the best solution, but it works (for now):
adding
>> C:\log\outputfile.log
(with two '>' before the path for the log file) attaches every log at the end of the file. So I get all Information and nothing is being overwritten.
As I want to know if there is a better solution, I will keep this thread open if that is permitted.

Is there any way for VBA (in Office 2011 for Mac) to run an AppleScript?

This question has been asked before, but the suggested answers(to "VBA Shell function in Office 2011 for Mac") don't seem to work.
My Applescript, Run_Script.scpt, creates a small txt file. It runs perfectly in the editor. It also runs perfectly when compiled as an app and double-clicked on the desktop.
I need to invoke it from a VBA procedure. I have tried these:
Sub sub_scripttest()
Dim i As Integer
Dim result_long As Long
Dim result_str As String
'//== the original question was this:
result_str = Shell("Macintosh HD:Applications:Run_Script.app", vbNormalFocus)
result_str = Shell("Macintosh HD/Applications/Run_Script.app", vbNormalFocus)
'//== gives a modal dialog box: "Runtime error 53: File not found"
'//== the suggested solution was this:
result_str = MacScript("do shell script ""Macintosh HD:Applications:Run_Script.app""")
result_str = MacScript("do shell script ""Macintosh HD/Applications/Run_Script.app""")
'//== gives a modal dialog box: "Runtime error 5: Invalid Procedure call or Argument"
'//== another suggested solution was this:
result_long = system("Macintosh HD:Applications:Run_Script.app")
result_long = system("Macintosh HD/Applications/Run_Script.app")
'//== appears to run, but returns result_long=32512, which is not a successful termination code.
'//== also the desktop shows that the script did not run. The txt file was not created.
'//== I even tried:
result_str = AppleScriptTask("Run_Script.scpt", "my_command", "")
'//== and got: "Compile Error: Sub or Function not defined."
End Sub
I have OS X El Capitan 10.11.6. I am running Office 2011 for Mac. Excel has been updated to 14.7.2
What am I doing wrong?
On ElCapitain with Excel 2011 (14.0.0) the 2 following macro are running.
You must adjust the path to your drive / users / script file.
Calling a script in VBA macro:
s = "run script (""ElCapitain:Users:imac27:Desktop:Test_Script.scpt"" as alias)"
Temp = MacScript(s)
Calling an app in VBA macro:
s = "run script (""ElCapitain:Users:imac27:Desktop:Test_App.app"" as alias)"
Temp = MacScript(s)

Crystal Reports Object reference not set to an instance of an object

VB.net program that gets report name, login info and parameters from a command line. Within the Visual Studio debug environment it runs without error. When the program is run directly from a command prompt I get a blank preview screen and an "error 91 Object reference not set to an instance of an object" on the third line:
Dim cryRpt As New ReportDocument
WriteErrorToLog(666, "got new report doc ") 'this gets logged
cryRpt.Load(sReportName)
WriteErrorToLog(666, "report loaded") 'this does not, error 91 is logged
The arguments in the command line run are copied from the same text file into the project's Start Options command line arguments and the actual cmd window.
Any ideas on why it works in the IDE but not on it's own?
TIA,
Phil

Call .reg File from Excel-VBA script

I want to call a .reg File out of my VBA script. I'm using Office/Excel 2013.
I know Excel can't run these files by itself, so i need to call the file via shell. The code i wrote doesn't work:
Sub deactivateHyperlinkWarnings()
Dim x
x = Shell("cmd /C C:\TEMP\DisableHyperlinkWarnings.reg")
End Sub
I found this piece of code somewhere on the web, but its not working. I don't even get an error message. The .reg File is located in C:\TEMP
What do i need to write to make it work?
Plus: Is it possible to suppress the MessageBoxes that are displayed when i run the .reg-File? When i start the file manually, i need to Hit "OK" like 3 Times. The people who are working with the Excelsheet later on shouldn't be seeing these things.
Instead of running cmd try to run reg. So in your case it should be x = Shell("reg import C:\TEMP\DisableHyperlinkWarnings.reg")
More info here

Need to Run VBA code in Command Prompt

I have a Tiff file splitter DLL that has a VBA code:
Dim tsplit As New SCTiffPageSplitNetDLL.TiffPageSplitterDLL
Dim rslt as Integer
To call the function and split TIFF files, you could use the following:
rslt = tsplit.Tiff_PageSplit("C:\test\test.tif", "C:\outputdir")
MsgBox rslt
How can I run this line of code in a command prompt? I need to make a batch file(.bat) out of this code.
You could put it in a VBScript (.vbs) file and run it using cscript. See this thread How to run a .vbs from a .bat