Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
将早期绑定VBA转换为晚期绑定VBA:Excel到Outlook联系人_Excel_Vba_Outlook_Late Binding_Early Binding - Fatal编程技术网

将早期绑定VBA转换为晚期绑定VBA:Excel到Outlook联系人

将早期绑定VBA转换为晚期绑定VBA:Excel到Outlook联系人,excel,vba,outlook,late-binding,early-binding,Excel,Vba,Outlook,Late Binding,Early Binding,每位员工都会得到一份更新的联系人列表。我正在Excel中创建一个宏,该宏将删除所有outlook联系人,然后将该工作表上的所有联系人导入其主要outlook联系人。并非所有用户都在同一个outlook版本上,因此我不能使用早期绑定方法,因为outlook OBJ库不能在不同版本之间引用 我设法使我的delete循环很容易地进入后期绑定,但是我很难让导入代码在后期绑定中工作。以下是我目前用于导入的工作早期绑定方法: Dim olApp As Outlook.Application Dim olNa

每位员工都会得到一份更新的联系人列表。我正在Excel中创建一个宏,该宏将删除所有outlook联系人,然后将该工作表上的所有联系人导入其主要outlook联系人。并非所有用户都在同一个outlook版本上,因此我不能使用早期绑定方法,因为outlook OBJ库不能在不同版本之间引用

我设法使我的delete循环很容易地进入后期绑定,但是我很难让导入代码在后期绑定中工作。以下是我目前用于导入的工作早期绑定方法:

Dim olApp As Outlook.Application
Dim olNamespace As Outlook.Namespace
Dim olFolder As Outlook.MAPIFolder
Dim olConItems As Outlook.Items
Dim olItem As Object

'Excel objects.
Dim wbBook As Workbook
Dim wsSheet As Worksheet

'Location in the imported contact list.
Dim lnContactCount As Long

Dim strDummy As String

'Turn off screen updating.
Application.ScreenUpdating = False

'Initialize the Excel objects.
Set wbBook = ThisWorkbook
Set wsSheet = wbBook.Worksheets(1)

'Format the target worksheet.
With wsSheet
    .Range("A1").CurrentRegion.Clear
    .Cells(1, 1).Value = "Company / Private Person"
    .Cells(1, 2).Value = "Street Address"
    .Cells(1, 3).Value = "Postal Code"
    .Cells(1, 4).Value = "City"
    .Cells(1, 5).Value = "Contact Person"
    .Cells(1, 6).Value = "E-mail"
    With .Range("A1:F1")
        .Font.Bold = True
        .Font.ColorIndex = 10
        .Font.Size = 11
    End With
End With

wsSheet.Activate

'Initalize the Outlook variables with the MAPI namespace and the default Outlook folder of the current user.
Set olApp = New Outlook.Application
Set olNamespace = olApp.GetNamespace("MAPI")
Set olFolder = olNamespace.GetDefaultFolder(10)
Set olConItems = olFolder.Items

'Row number to place the new information on; starts at 2 to avoid overwriting the header
lnContactCount = 2

'For each contact: if it is a business contact, write out the business info in the Excel worksheet;
'otherwise, write out the personal info.
For Each olItem In olConItems
    If TypeName(olItem) = "ContactItem" Then
        With olItem
            If InStr(olItem.CompanyName, strDummy) > 0 Then
                Cells(lnContactCount, 1).Value = .CompanyName
                Cells(lnContactCount, 2).Value = .BusinessAddressStreet
                Cells(lnContactCount, 3).Value = .BusinessAddressPostalCode
                Cells(lnContactCount, 4).Value = .BusinessAddressCity
                Cells(lnContactCount, 5).Value = .FullName
                Cells(lnContactCount, 6).Value = .Email1Address
            Else
                Cells(lnContactCount, 1) = .FullName
                Cells(lnContactCount, 2) = .HomeAddressStreet
                Cells(lnContactCount, 3) = .HomeAddressPostalCode
                Cells(lnContactCount, 4) = .HomeAddressCity
                Cells(lnContactCount, 5) = .FullName
                Cells(lnContactCount, 6) = .Email1Address
            End If
            wsSheet.Hyperlinks.Add Anchor:=Cells(lnContactCount, 6), _
                                   Address:="mailto:" & Cells(lnContactCount, 6).Value, _
                                   TextToDisplay:=Cells(lnContactCount, 6).Value
        End With
        lnContactCount = lnContactCount + 1
    End If
Next olItem

'Null out the variables.
Set olItem = Nothing
Set olConItems = Nothing
Set olFolder = Nothing
Set olNamespace = Nothing
Set olApp = Nothing

'Sort the rows alphabetically using the CompanyName or FullName as appropriate, and then autofit.
With wsSheet
    .Range("A2", Cells(2, 6).End(xlDown)).Sort key1:=Range("A2"), order1:=xlAscending
    .Range("A:F").EntireColumn.AutoFit
End With

'Turn screen updating back on.
Application.ScreenUpdating = True

MsgBox "The list has successfully been created!", vbInformation

End Sub

要使用后期绑定,您应该将所有Outlook特定对象声明为
对象

Dim olApp As Object, olNamespace As Object, olFolder As Object, olConItems As Object
然后:

这将使每台计算机从安装在其上的Outlook库中创建olApp对象。这样可以避免在要分发的工作簿中设置对Outlook14的显式引用(在分发Excel文件之前从项目中删除该引用)


希望这有帮助:)

要使用后期绑定,您应该将所有Outlook特定对象声明为
对象

Dim olApp As Object, olNamespace As Object, olFolder As Object, olConItems As Object
然后:

这将使每台计算机从安装在其上的Outlook库中创建olApp对象。这样可以避免在要分发的工作簿中设置对Outlook14的显式引用(在分发Excel文件之前从项目中删除该引用)


希望这能有所帮助:)

所有Outlook对象声明都必须首先成为与Olook无关的对象声明

Dim olApp As Object 
Dim olNamespace As Object 
Dim olFolder As Object 
Dim olConItems As Object 
Dim olItem As Object 
你需要一个新的电话号码


其他一切都应该准备就绪。

所有Outlook对象声明首先必须成为与Olook无关的对象声明

Dim olApp As Object 
Dim olNamespace As Object 
Dim olFolder As Object 
Dim olConItems As Object 
Dim olItem As Object 
你需要一个新的电话号码


其他一切都应该安排妥当。

你到底遇到了什么麻烦?发布您不太适合工作的后期绑定代码以获取评论会更快。我在您的早期绑定代码中没有看到任何东西阻止您将
Dim x作为[someOutlookType]
切换到
Dim x作为对象
这里的
strDummy
角色是什么?声明它,但不给它赋值。strDummy在我的For-Each语句中用于olConItems,真正用作快速占位符。这不是最好的习惯,但它现在起作用了。Tim-我遇到的最大问题是将olConItems转换为Outlook.Items到后期绑定。所以我会将olconitems暗显到obj,然后让olApp创建outlook.application?很简单你到底遇到了什么麻烦?发布您不太适合工作的后期绑定代码以获取评论会更快。我在您的早期绑定代码中没有看到任何东西阻止您将
Dim x作为[someOutlookType]
切换到
Dim x作为对象
这里的
strDummy
角色是什么?声明它,但不给它赋值。strDummy在我的For-Each语句中用于olConItems,真正用作快速占位符。这不是最好的习惯,但它现在起作用了。Tim-我遇到的最大问题是将olConItems转换为Outlook.Items到后期绑定。所以我会将olconitems暗显到obj,然后让olApp创建outlook.application?很简单