excel - Excel VBA 代码在一台计算机之外工作 - 错误 91
问题描述
我有一个 Excel VBA 子程序,用于在 Outlook 中搜索联系人详细信息。
该函数可在许多计算机上运行,但该函数的主要用户除外,它会在该计算机上产生错误:
Error 91: Object variable or With block variable not set
有人能帮助我吗?
'Function to import Outlook contacts according to their client code
Sub ExportOutlookAddressBook()
Application.ScreenUpdating = False
Dim olApp As Outlook.Application
Dim olNS As Outlook.Namespace
Dim olAL As Outlook.AddressList
Dim olEntry As Outlook.AddressEntry
Dim CodeClient As String
Dim RCompanyName As String
Dim i As Integer
Dim AccountCount As Integer
Set olApp = Outlook.Application
Set olNS = olApp.GetNamespace("MAPI")
CodeClient = 0
RCompanyName = 0
i = 0
AccountCount = olNS.Accounts.Count
Range("AA6:AF10").ClearContents
For i = 1 To AccountCount
Set olAL = olNS.AddressLists(i) 'Change name if different contacts list name
Set olEntry = olAL.AddressEntries(1)
ActiveWorkbook.ActiveSheet.Range("K6").Select
CodeClient = ActiveCell.Value
ActiveWorkbook.ActiveSheet.Range("AA6").Select
For Each olEntry In olAL.AddressEntries
' your looping code here
RCompanyName = Left(Right(olEntry.GetContact.CompanyName, 7), 6)
If RCompanyName = CodeClient Then
ActiveCell.Value = olEntry.GetContact.FullName
ActiveCell.Offset(0, 1).Value = olEntry.GetContact.BusinessTelephoneNumber 'business phone number
ActiveCell.Offset(0, 2).Value = olEntry.Address 'email address
ActiveCell.Offset(0, 3).Value = olEntry.GetContact.CompanyName
ActiveCell.Offset(0, 4).Value = olEntry.GetContact.BusinessAddress
ActiveCell.Offset(1, 0).Select
End If
Next olEntry
Next i
Set olApp = Nothing
Set olNS = Nothing
Set olAL = Nothing
Application.ScreenUpdating = True
ActiveWorkbook.ActiveSheet.Range("K7").Select
End Sub
解决方案
尝试这个。
除了添加If Nothing...
's,我还整理了一些其他重复的代码。
Option Explicit 'this line is recommended at the very top of every module.
'Function to import Outlook contacts according to their client code
Sub ExportOutlookAddressBook()
Dim olApp As Outlook.Application, olNS As Outlook.Namespace, olAL As Outlook.AddressList
Dim olEntry As Outlook.AddressEntry, CodeClient As String, RCompanyName As String, i As Long
Set olApp = Outlook.Application
Set olNS = olApp.GetNamespace("MAPI")
Application.ScreenUpdating = False
Range("AA6:AF10").ClearContents
For i = 1 To olNS.Accounts.Count
Set olAL = olNS.AddressLists(i) 'Change name if different contacts list name
Set olEntry = olAL.AddressEntries(1)
CodeClient = ActiveWorkbook.ActiveSheet.Range("K6")
ActiveWorkbook.ActiveSheet.Range("AA6").Select
For Each olEntry In olAL.AddressEntries
' your looping code here
RCompanyName = Left(Right(olEntry.GetContact.CompanyName, 7), 6)
If RCompanyName = CodeClient Then
With ActiveCell
.Value = olEntry.GetContact.FullName
.Offset(0, 1) = olEntry.GetContact.BusinessTelephoneNumber 'business phone number
.Offset(0, 2) = olEntry.Address 'email address
If Not olEntry.GetContact Is Nothing Then
If Not olEntry.GetContact.CompanyName Is Nothing Then
.Offset(0, 3) = olEntry.GetContact.CompanyName
End If
If Not olEntry.GetContact.BusinessAddress Is Nothing Then
.Offset(0, 4) = olEntry.GetContact.BusinessAddress
End If
End If
.Offset(1, 0).Select
End With
End If
Next olEntry
Next i
Set olApp = Nothing
Set olNS = Nothing
Set olAL = Nothing
Application.ScreenUpdating = True
ActiveWorkbook.ActiveSheet.Range("K7").Select
End Sub
推荐阅读
- azure-web-app-service - Azure 上的 SignalR 长时间断开连接超时
- angular - 三级域上的角度直接链接错误
- html - 羽毛。如何将初始页面重定向到视图文件夹而不是公用文件夹中的页面
- angular - 具有多个动作角度的效果
- responsive-design - 如何在 Tailwind 中将元素设置为显示在中屏及下方?
- ios - 我们如何在 Swift(iOS) 中的 AppleWatch 扩展中进行水平滚动
- powerbi - 任何人都知道为什么最近与 SharePoint 在线列表的连接停止了?
- android - 传感器总是在一秒钟内给我 100 个事件值
- dataframe - 从数据框的行中删除仅包含数字的字符串
- python - 没有权限。joblib 将以串行模式运行