Excel в pdf макрос командной строки - PullRequest
0 голосов
/ 12 марта 2019

Этот макрос Excel будет использоваться для преобразования входного файла в PDF, я хочу запустить макрос с помощью команды bash, например

"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
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...