Eu tenho uma macro do Outlook em funcionamento que exporta olista de tarefas do usuário atual para uma planilha do Excel, mas quero alterá-la para usar a ligação tardia para facilitar a distribuição (ou seja, não preciso explicar a outros usuários sobre como definir uma referência de biblioteca etc.)
Eu segui o exemplo Converter VBA de ligação antecipada em VBA de ligação tardia: Excel para contatos do Outlook definir minhas variáveis do Excel como objetos.
Abaixo está uma comparação de como eu declarei as variáveis antes / depois da alteração de ligação:
"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")
Agora estou recebendo um erro de tempo de execução 1004 na seguinte seção do meu código:
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
Eu incluí o restante do código após a linha de erro, portanto, se houver mais problemas em tempo de execução, eles poderão ser detectados pelas pessoas incríveis no SO.
Estou supondo que a metodologia para declarar meu "intervalo" esteja incorreta, mas não tenho muita certeza do motivo e, portanto, não tenho certeza de como corrigi-lo.
Algum corpo por aí com uma sugestão?
Obrigado!
Respostas:
4 para resposta № 1xlUp
é uma constante do Excel definida na biblioteca do Excel. Se você removeu a referência, então xlUp
será uma variável não declarada.
Se você tem Option Explicit
definido, você deve descobrir isso ao compilar.