除一台计算机外,Excel VBA代码正常工作-错误91

时间:2018-11-09 14:17:24

标签: excel vba outlook outlook-vba

我有一个Excel VBA子程序,用于在Outlook中搜索联系人详细信息。

该功能可在许多计算机上运行,​​但该计算机的主要用户除外,该计算机会在该计算机上产生错误:

  

public ActionResult Index() { List<Books> books = db.Books.ToList() List<ChapterList> chapterList = (from a in db.Books join b in db.Charpters on a.id equals b.BookId //where a.id==3 you can also query for specific book select new ChapterList { chapter=b.chapter }).ToList(); BookViewModel bvm = new BookViewModel(); bvm.books = books; bvm.chapterLists = chapterLists return view(); }

有人可以帮我吗?

img

Error 91: Object variable or With block variable not set

1 个答案:

答案 0 :(得分:1)

尝试一下。

除了添加If Nothing...之外,我还整理了一些其他重复代码。

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