Attribute VB_Name = "Library" Option Base 0 Option Explicit #If Vba7 Then Declare PtrSafe Function GetCommandLine Lib "kernel32" Alias "GetCommandLineW" () As LongPtr Declare PtrSafe Function lstrlenW Lib "kernel32" (ByVal lpString As LongPtr) As Long Declare PtrSafe Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (MyDest As Any, MySource As Any, ByVal MySize As LongPtr) #Else Declare Function GetCommandLine Lib "kernel32" Alias "GetCommandLineW" () As Long Declare Function lstrlenW Lib "kernel32" (ByVal lpString As Long) As Long Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (MyDest As Any, MySource As Any, ByVal MySize As Long) #EndIf #If Vba7 Then Function CmdToSTr(Cmd As LongPtr) As String #Else Function CmdToSTr(Cmd As Long) As String #EndIf Dim Buffer() As Byte Dim 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 Function GetCmdPars() As String() Dim answer As String Dim s As Variant Dim CmdPars() As String answer = CmdToSTr(GetCommandLine) CmdPars = Split(answer, " /m") ReDim Preserve CmdPars(2) If CmdPars(0) = answer And CmdPars(1) = "" Then ' v tomto případě se jedná o Word 2010 CmdPars(0) = "dopis.xls" CmdPars(1) = "vystup.doc" Else CmdPars(0) = CmdPars(1) CmdPars(1) = CmdPars(2) End If ReDim Preserve CmdPars(1) GetCmdPars = CmdPars End Function Function InsPath(fn As String) As String Dim pth As String If InStr(fn, "\") > 0 Then pth = "" Else pth = VBA.Environ("Temp") & "\" End If InsPath = pth & fn End Function Sub GetInfo() MsgBox Join(GetCmdPars()) End Sub Sub GoMailMerge() Dim CurrDir As String Dim CmdPars() As String With Application .Visible = False .WindowState = wdWindowStateMinimize .ScreenUpdating = False '.AutomationSecurity = msoAutomationSecurityLow End With CmdPars = GetCmdPars() If CmdPars(0) = "" Or CmdPars(1) = "" Then MsgBox prompt:="Neplatné parametry volání makra.", Title:="Chyba při volání makra", buttons:=vbOKOnly + vbCritical Application.Quit wdDoNotSaveChanges End If With ActiveDocument 'CurrDir = .Path & "\" With .MailMerge .MainDocumentType = wdFormLetters .OpenDataSource Name:=InsPath(CmdPars(0)), OpenExclusive:=False, AddToRecentFiles:=False, ConfirmConversions:=True, SqlStatement:="select * from `dopis$`" .Destination = wdSendToNewDocument .SuppressBlankLines = True .Execute Pause:=False End With End With ActiveDocument.SaveAs InsPath(CmdPars(1)) 'Kill InsPath("dopis.txt") Kill InsPath("go.cmd") 'Kill InsPath(CmdPars(0)) Application.Quit wdDoNotSaveChanges 'Exit Sub End Sub