【问题标题】:Converting to late binding causes Runtime 1004 error - Outlook转换为后期绑定会导致运行时 1004 错误 - Outlook
【发布时间】: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 上令人难以置信的人发现。

我假设声明“范围”的方法不正确,但我不确定原因,因此不确定如何解决。

有没有人提出建议?

谢谢!

【问题讨论】:

    标签: excel vba outlook


    【解决方案1】:

    xlUp 是 Excel 库中定义的 Excel 常量。如果您删除了引用,那么xlUp 将是一个未声明的变量。

    如果你设置了Option Explicit,那么你应该在编译时找到它。

    【讨论】:

    • 啊忘了那个小栗子.....那么我应该把xlUp声明为一个对象吗?
    • Const xlUp = -4162,或者定义自己的枚举Enum XlDirection...
    • 要查找任何其他值,只需在 Excel 即时窗口中键入,例如,?xlUp
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-10-14
    • 1970-01-01
    • 1970-01-01
    • 2018-07-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多