运行Excel宏的VBS获得“调用的对象已与其客户端断开连接”

时间:2019-03-08 10:33:39

标签: excel vba windows vbscript

我在自动化过程中有一个vbs脚本。
该脚本运行一个Excel宏,根据行号将一个Excel拆分为多个文件。 发生的是:该脚本创建了一些分割的文件,然后引发错误:

  

MacroSubSplit.vbs(20,1)(空):L'oggetto invocato sièèdisconnesso   代客户corrispondenti。即“被调用的对象已断开连接   来自其客户”

宏来自以下答案: How to split spreadsheet into multiple spreadsheets with set number of rows?

我已对其进行修改以打开文件f。

Public Sub Split_Auto(f As String)
  Dim wb As Workbook
  Dim ThisSheet As Worksheet

  Dim NumOfColumns As Integer
  Dim RangeToCopy As Range
  Dim WorkbookCounter As Integer
  Dim wbDst As Workbook
  Dim RowsInFile
  Dim Prefix As String

  Application.ScreenUpdating = False
'Seleziona file

 Dim fDialog As Office.FileDialog
 Set fDialog = Application.FileDialog(msoFileDialogFilePicker)

 Set wbDst = Workbooks.Open(f)

  'Initialize data
  Set ThisSheet = wbDst.Worksheets("TotalData")
  NumOfColumns = ThisSheet.UsedRange.Columns.Count
  WorkbookCounter = 1
  RowsInFile = 40000                   'how many rows (incl. header) in new files?
  Prefix = "Split"                    'prefix of the file name

  For p = 1 To ThisSheet.UsedRange.Rows.Count Step RowsInFile
    Set wb = Workbooks.Add

'Paste the chunk of rows for this file
Set RangeToCopy = ThisSheet.Range(ThisSheet.Cells(p, 1), ThisSheet.Cells(p + RowsInFile - 1, NumOfColumns))
RangeToCopy.Copy wb.Sheets(1).Range("A1")

'Save the new workbook, and close it
strDate = Format(Now, "dd/MMM/yyyy")
wb.SaveAs ThisWorkbook.Path & "\" & Prefix & Format(Now, "yyyy-MM-dd") & "_SplitNum_" & WorkbookCounter
wb.Close

'Increment file counter
WorkbookCounter = WorkbookCounter + 1


 Next p

  Application.ScreenUpdating = True
  Set wb = Nothing

End Sub

从vbs文件调用宏

Option Explicit

if WScript.Arguments.Count = 0 then
    WScript.Echo "Missing parameters"
    WScript.Quit    
end if
Dim xlApp, xlBook, File, objShell, input_path

File = WScript.Arguments.Item(0)

Set xlApp = CreateObject("Excel.Application")
set objShell = WScript.CreateObject ("WScript.Shell")
'WScript.Echo(objShell.CurrentDirectory)
input_path = objShell.CurrentDirectory & "\Input\" & File
'WScript.Echo("Input" & input_path)
'close without prompting
xlApp.DisplayAlerts = False
'~~> Change Path here
Set xlBook = xlApp.Workbooks.Open(objShell.CurrentDirectory&"\"&"Macro.xlsm", 0, True)
xlApp.Run "Foglio1.Split_Auto", CStr(input_path)
xlBook.Close False 
xlApp.Quit

Set xlBook = Nothing
Set xlApp = Nothing

'WScript.Echo "Finished."
WScript.Quit

您能帮我找到解决此问题的方法吗?

0 个答案:

没有答案