此excel宏将用于将输入文件转换为pdf,我想通过bash命令(如pp)运行该宏
"C:\Program Files\Microsoft Office\root\Office16\excel.exe" /e /q "C:\Macro.xlsm" inputFile.xls
但是,宏似乎忽略了输入文件,而是尝试打印Macro.xlsm文件,该文件为空。我找不到错误,下面是该宏的相关代码:
Option Explicit
Private Sub Workbook_Open()
Dim CmdRaw As Long
Dim CmdLine As String
Dim Args As String
Dim ArgArray As Variant
CmdRaw = GetCommandLine
CmdLine = CmdToStr(CmdRaw)
Args = ""
If InStr(CmdLine, " /a") > 0 Then
Args = Mid(CmdLine, InStr(1, CmdLine, " /a") + 4)
If Args <> "" Then
Call Module2.Xlsx2Pdf(Args)
Application.OnTime Now + TimeValue("00:00:05"), "ThisWorkbook.Save_Exit"
End If
End If
End Sub
Sub Save_Exit()
Application.Quit
ThisWorkbook.Close Saved = True
End Sub
处理命令行,这是我怀疑错误的地方:
Option Explicit
'declare the kernel32 functions to extract the commandline arguments
'GetCommandLineW returns the command line argument in number format
Declare PtrSafe Function GetCommandLine Lib "kernel32" Alias "GetCommandLineW" () As Long
'This function is for getting the length of the commandline arguments
Declare PtrSafe Function lstrlenW Lib "kernel32" (ByVal lpString As Long) As Long
'RtlMoveMemory will copy the specified data to the buffer
Declare PtrSafe Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (MyDest As Any, MySource As Any, ByVal MySize As Long)
'Convert the command line from number format to string format
Public Function CmdToStr(Cmd As Long) As String
Dim Buffer() As Byte, StrLen As Long
If Cmd Then
StrLen = lstrlenW(Cmd) * 2
If StrLen Then
ReDim Buffer(0 To (StrLen - 1)) As Byte
CopyMemory Buffer(0), ByVal Cmd, StrLen
CmdToStr = Buffer
End If
End If
End Function
PDF转换:
Sub Xlsx2Pdf(wkFile As String)
'
' Xlsx2Pdf Macro
'
Workbooks.Open Filename:=wkFile
NewFilename = (Replace(wkFile, ".xlsx", ".pdf"))
ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:= _
NewFilename, Quality:=xlQualityStandard, _
IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:= _
False
Application.Quit
End Sub