0

I'm writing script that will loop through all macro-enabled Word documents in a folder and run a macro from the command line.

My macro counts pages and if the count is exactly 1, sends the document to the printer.

Because I'll be doing a lot of documents (sometimes up to 200 at a time) I'd like to let it run through and exit with a non-zero code so I can catch those events and report them back to the user.

But Application.Quit doesn't provide a way to specify an exit code. Is there another way to for the document to exit in such a way that my script can know about it?

EDIT: Code as requested. Replace /path/to stuff with actual paths.

function bulkPrint() {
  // Get folder of files
  $files = scandir("/path/to/my/files/");
  $output = "";
  $exitCode = -1;
  // Loop through each file
  foreach($files as $file) {
    // Not real paths
    exec("/path/to/winword.exe /q /x /mMyMacro \"/path/to/my/files/" . $file . "\"", $output, $exitCode);
    echo $exitCode; // Always returns 0 because Word exited cleanly
  }
}
Grayda
  • 1,870
  • 2
  • 25
  • 43
  • Can you post your code? How are you running the "macro from the command line"? I seems like it would be a lot easier to just have the macro return status. – Comintern Nov 17 '16 at 00:48
  • maybe you want is : `Sub Quit() MsgBox "quit" Exit Sub End Sub ` – Andy Nov 17 '16 at 01:01
  • @Andy: That just exits the sub and doesn't quit Word and return a non-zero code. – Grayda Nov 17 '16 at 01:12
  • @Comintern: Code posted, but doesn't really change much, because it doesn't matter how my app calls the macro, as long as it does, and VBA can return a non-zero exit code for my code to pick up on. – Grayda Nov 17 '16 at 01:13
  • 1
    Is VBScript an option? – Comintern Nov 17 '16 at 01:15
  • @Comintern: The less hacky, the better, but VBScript could be an option. I also thought about use VBA to write a file in the same directory on error. Then I can `scandir` through, find the error files and display those to the user What did you have in mind with VBScript? – Grayda Nov 17 '16 at 01:56
  • With VBScript, you have access to the Application object, so you have many more options - return a value from the macro itself, test to see what the `Application.BackgroundPrintingStatus` is, etc. – Comintern Nov 17 '16 at 02:10
  • @Comintern: Interesting idea. I'll research that and see how I go. Thanks! – Grayda Nov 17 '16 at 02:13
  • One method without changing your language (though it is a bit "hacky") might be to have the macro create a temporary file (e.g. `/path/to/my/files/$file.tmp`) and write it's "exit code". The calling script could read from the file and delete it when done. – Drew Chapin Nov 17 '16 at 02:19
  • @druciferre That's what I ended up doing. It's not too hacky, as it's just writing and deleting files, but it's not as clean as `Application.Quit(1)` or something similar. . I wound up writing the actual number of pages (and the expected number) into the file and making the presence of the file (called `$file.err`) indicative of an error. – Grayda Nov 17 '16 at 02:59
  • 1
    @Grayda, I completely overlooked where you had mentioned the idea earlier in the comments. Great minds think alike. – Drew Chapin Nov 17 '16 at 03:14
  • might be possible without VBA http://stackoverflow.com/questions/8986067/how-to-get-the-number-of-pages-in-a-word-document-on-linux, but not sure about the printing part. – Slai Nov 17 '16 at 04:07

1 Answers1

0

This is not really an answer, but it's a passable workaround for my situation.

As a few people suggested in the comments, you can use VBA to create a file in the same folder, then use that as your exit code.

In my case, the code ended up looking like this:

Sub MyMacro()
    On Error GoTo ErrHandler
    ' My code that may or may not fail
    ' More code here to exit if no error
    Exit Sub
ErrHandler:
    WriteError Err.Description
    End
End Sub

Sub WriteError(errText As String)
    Dim fso As Object
    Set fso = CreateObject("Scripting.FileSystemObject")
    Dim oFile As Object
    Set oFile = fso.CreateTextFile(ActiveDocument.Path & "\" & ActiveDocument.Name & ".err")
    oFile.WriteLine errText
    oFile.Close
    Set fso = Nothing
    Set oFile = Nothing
End Sub

So when MyMacro() carks it, the error description is written to a file with the same folder and name (but with .err at the end) and then exits. When my script is done running Word, it goes through all the .err files in the directory and outputs the filename and contents to screen so I can see which items passed and which failed.

I can expand this further to check for almost anything and catch the specific errors I'm after.

I'm not going to mark this as the answer though. While it does solve my specific issue in a way that I'm mostly happy with, it doesn't answer my core question of Can VBA exit with an error code

Grayda
  • 1,870
  • 2
  • 25
  • 43