转换为后期绑定会导致运行时1004错误 - Outlook

时间:2017-05-17 23:50:39

标签: vba excel-vba outlook outlook-vba excel

我有一个工作的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上的不可思议的人所接受。

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

有任何建议吗?

谢谢!

1 个答案:

答案 0 :(得分:4)

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

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