【发布时间】:2017-05-17 23:50:39
【问题描述】:
我有一个有效的 Outlook 宏,可以将当前用户的任务列表导出到 Excel 电子表格,但我想将其更改为使用后期绑定以便于分发(即我不必向其他用户解释设置库引用等)
我按照示例 Convert Early Binding VBA to Late Binding VBA : Excel to Outlook Contacts 将我的 Excel 变量设置为对象。
下面是我如何在绑定更改前后声明变量的比较:
'Late binding variables and their early binding equivilants
Dim objExcel As Object 'Dim objExcel As New Excel.Application
Dim exWB As Object 'Dim exWb As Excel.Workbook
Dim sht As Object 'Dim sht As Excel.Worksheet
Dim Range As Object 'Dim Range As Excel.Range
Dim r As Object 'Dim r As Range
Dim cell As Object 'Dim cell As Range
'set application
Set objExcel = CreateObject("Excel.Application")
我的代码的以下部分现在出现运行时 1004 错误:
With objExcel.ActiveSheet
Set r = .Range(.Cells(2, col), .Cells(.Rows.Count, col).End(xlUp)) 'runtime 1004 error here after late binding modification
End With
For Each cell In r
s = cell.Text
If Len(Trim(s)) > 0 Then
iloc = InStr(1, s, sChar, vbTextCompare)
If iloc > 1 Then
s1 = Left(s, iloc - 1)
cell.Value = s1
Else
If iloc <> 0 Then
cell.ClearContents
End If
End If
End If
Next cell
y = y + 1
stat_string = ""
End If
Next x
'Autofit all column widths
For Each sht In objExcel.ActiveWorkbook.Worksheets
sht.Columns("A").EntireColumn.AutoFit
sht.Columns("B").EntireColumn.AutoFit
sht.Columns("C").EntireColumn.AutoFit
sht.Columns("D").EntireColumn.AutoFit
sht.Columns("E").EntireColumn.AutoFit
sht.Columns("F").EntireColumn.AutoFit
Next sht
exWB.Save
exWB.Close
Set exWB = Nothing
'this kills the excel program from the task manager so the code will not double up on opening the application
'sKillExcel = "TASKKILL /F /IM Excel.exe"
'Shell sKillExcel, vbHide
objExcel.Application.Quit
我已经在错误行之后包含了其余代码,因此,如果还有其他运行时问题,它们可能会被 SO 上令人难以置信的人发现。
我假设声明“范围”的方法不正确,但我不确定原因,因此不确定如何解决。
有没有人提出建议?
谢谢!
【问题讨论】: