Home » excel » excel vba – Move VBA code to standalone VBS file

excel vba – Move VBA code to standalone VBS file

Posted by: admin May 14, 2020 Leave a comment

Questions:

I have a macro in excel vba that runs a shell to execute a simple pdf editor by command line, exports a list of the page names and numbers, searches for a keyword to find the page we want, gets the page num, and then extracts that page from the pdf file.

I’ve realised this macro would serve better as a standalone entity, and was wondering what is required to take the code out of excel, and run it from a .vbs file.

I tried direct copy and paste and it didnt like the letter “A” in the word “As” in the first line.

Sub PDF_GetUFPlan()

Dim wsh As Object
Set wsh = VBA.CreateObject("WScript.Shell")
Dim waitOnReturn As Boolean: waitOnReturn = True
Dim windowStyle As Integer: windowStyle = 1
Dim errorCode As Integer

wsh.Run "C:\Users\johnmcs\Desktop\pdftk.exe C:\Users\johnmcs\Desktop\FULL.PDF burst output C:\Users\johnmcs\Desktop\output.txt", windowStyle, waitOnReturn

Dim hf As Integer: hf = FreeFile
Dim lines() As String, i As Long

Open "C:\Users\johnmcs\Desktop\doc_data.txt" For Input As #hf
    lines = Split(Input$(LOF(hf), #hf), vbNewLine)

Close #hf

For i = 0 To UBound(lines)

If InStrRev(lines(i), "UPPER FLOOR PLAN") > 0 Then

For x = i To UBound(lines)
    If InStrRev(lines(x), "BookmarkPageNumber: ") > 0 Then

    Dim UFpagenum As Integer, PagenumPosi As Integer
    PagenumPosi = InStrRev(lines(x), " ")


    PagenumPosi = Len(lines(x)) - PagenumPosi

    UFpagenum = Right(lines(x), PagenumPosi)

    GoTo extractpage

    End If

Next


End If
Debug.Print "Line"; i; "="; lines(i)
Next



extractpage:

wsh.Run "C:\Users\johnmcs\Desktop\pdftk.exe C:\Users\johnmcs\Desktop\FULL.PDF cat " & UFpagenum & " output C:\Users\johnmcs\Desktop\page" & UFpagenum & ".pdf", windowStyle, waitOnReturn



End Sub
How to&Answers:
Sub PDF_GetUFPlan()

Dim wsh 
Set wsh = CreateObject("WScript.Shell") ' NB we also have wscript.createobject
Dim waitOnReturn 
waitOnReturn = True
Dim windowStyle 
windowStyle = 1
Dim errorCode 

So here, the beginning is fixed. You cannot Dim as anything. Everything is a variant. Everything MUST be late bound so all sets must be done with CreateObject – no set x = new thing but set x = CreateObject("thing.application").

Remember VB6/VBA supports vbs feature set. vbs is compatable (a design goal) with vb6/vba.

Answer:

VBscript does not support

Dim variable_name As variable_type 

variable declaration….all variables are of type variant.

Just type:

Dim variable_name

Answer:

Not technically a necessity, but (sigh… I can’t believe I’m saying this) you should consider hungarian notation for VBScript. As the other answerers pointed out, VBScript is not a strongly typed language. You should rename your variables so it is clear what type they are to any future maintainer.

Dim wsh As Object
Set wsh = VBA.CreateObject("WScript.Shell")
Dim boolWaitOnReturn : waitOnReturn = True
Dim intWindowStyle : windowStyle = 1
Dim intErrorCode