B4J Tutorial Generate Word document and save as PDF from B4J via VBS-script

This one generates a Word document (you need to have Word installed!) and saves it as a PDF (for other formats just change the vbs-script)

To start see here: https://www.b4x.com/android/forum/threads/execute-windows-vbs-scripts-from-b4j.73612/

B4J:

B4X:
'Non-UI application (console / server application)
#Region  Project Attributes
    #CommandLineArgs:  
    #MergeLibraries: True
#End Region

Sub Process_Globals
  
End Sub

Sub AppStart (Args() As String)
  
  
    Dim WordSh As Shell
    WordSh.Initialize("Word", "c:\windows\system32\cscript.exe", Array As String("//nologo", "C:\Users\Klaus\Documents\B4JApps\word\wordarg.vbs", "C:\Users\Klaus\Documents\B4JApps\word\test.pdf","Arg 1", "Arg 2"))
    WordSh.WorkingDirectory = "C:\Users\Klaus\Documents\B4JApps\word\"
    WordSh.Run(120000)
  
    StartMessageLoop
End Sub



Sub Word_ProcessCompleted (Success As Boolean, ExitCode As Int, StdOut As String, StdErr As String)
   If Success And ExitCode = 0 Then
         Log("OK...")
   Else
     Log("Error: " & StdErr & StdOut)
   End If
End Sub


It uses this array:

Array As String("//nologo", "C:\Users\Klaus\Documents\B4JApps\word\wordarg.vbs", "C:\Users\Klaus\Documents\B4JApps\word\test.pdf","Arg 1", "Arg 2"))

"C:\Users\Klaus\Documents\B4JApps\word\test.pdf" = Dir & Filename where the PDF is stored = arg(0)
"Arg 1", "Arg 2" = These args will be printed on on the document. Expand it if you like = arg(1) to arg(n)

VBS "wordarg.vbs":

B4X:
Set objWord = CreateObject("Word.Application")
objWord.Caption = "Test Caption"
objWord.Visible = True

Set objDoc = objWord.Documents.Add()
Set objSelection = objWord.Selection


objSelection.Font.Name = "Arial"
objSelection.Font.Size = "18"
objSelection.TypeText "This is a B4J Test"
objSelection.TypeParagraph()

objSelection.Font.Size = "14"
objSelection.TypeText "" & Date()
objSelection.TypeParagraph()
objSelection.TypeParagraph()

objSelection.Font.Size = "10"

' Get count of arguments
argscount = WScript.Arguments.Count

  ' Output each specified argument
For i = 0 To (argscount - 1)
    objSelection.Font.Size = "14"
    objSelection.TypeText i+1 & " " & WScript.Arguments(i) 'our args...
    objSelection.TypeParagraph()
Next



'objDoc.SaveAs("testdoc.pdf",  wdFormatPDF)
objDoc.SaveAs2 WScript.Arguments(0), 17
objWord.Quit false
 
Top