Hallo,
ich habe ein Excel-Makro gebastelt um eine Input-Datei zu einer PDF-Datei zu konvertieren und zu speichern, das alles soll über die Kommandozeile ohne weitere Bestätigungen ablaufen.
Das Makro wird wie folgt abgerufen:
1 | "C:\Program Files\Microsoft Office\root\Office16\excel.exe" /e /q "C:\Macro.xlsm" inputFile.xls /a
|
Nach Aufruf des Markos, scheint die Input-Datei aber ignoriert zu werden, und es wird versucht die Macro.xlsm zu drucken, welche leer ist.
Hier ist der vollständige Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | 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
|
CMD-Modul
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | Option Explicit
Declare PtrSafe Function GetCommandLine Lib "kernel32" Alias "GetCommandLineW" () As Long
Declare PtrSafe Function lstrlenW Lib "kernel32" ( ByVal lpString As Long ) As Long
Declare PtrSafe Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (MyDest As Any, MySource As Any, ByVal MySize As Long )
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
|
Xlsx2Pdf Modul
1 2 3 4 5 6 7 8 9 10 11 12 | Sub Xlsx2Pdf(wkFile As String )
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
|
Ist für jemanden ersichtlich wo der Fehler liegt? Ich scheitere leider selber schon seit einer gewissen Zeit daran.
Danke!
|