将值写入文本文件,而不是对象名称

Write value to text file, not name of object

我有一些代码,如果将值放入 Excel sheet 中,效果很好,但是当我试图让它写入文本文件时,它只会一遍又一遍地写入以下内容:

objAddressEntry.Name

objAddressEntry.Name

代码如下。我留下了最初写给 Excel 的位,但它被注释掉了。

Sub GetOutlookAddressBook()

' Need to add reference to Outlook
'(In VBA editor Tools References MS Outlook #.# Library)
' Adds addresses to existing Sheet called Address and
' defines name Addresses containing this list
' For use with data Validation ListBox (Source as =Addresses)

On Error GoTo error

Dim objOutlook As Outlook.Application
Dim objAddressList As Outlook.AddressList
Dim objAddressEntry As Outlook.AddressEntry
Dim intCounter As Integer

Application.ScreenUpdating = False

' Setup connection to Outlook application
Set objOutlook = CreateObject("Outlook.Application")
Set objAddressList = objOutlook.Session.AddressLists("Global Address List")

Application.EnableEvents = False

' Clear existing list
' Sheets("Address").Range("A:A").Clear

' Create text file
Dim fso As Object
Set fso = CreateObject("Scripting.FileSystemObject")
Dim oFile As Object
Set oFile = fso.CreateTextFile("C:\Users\username\Desktop\test.txt")

'Step through each contact and list each that has an email address
For Each objAddressEntry In objAddressList.AddressEntries
If objAddressEntry.Address <> "" Then
intCounter = intCounter + 1
Application.StatusBar = "Address no. " & intCounter & " ... " & objAddressEntry.Address

' Write to text file
oFile.WriteLine "objAddressEntry.Name" & vbNewLine

' Sheets("Address").Cells(intCounter, 1) = objAddressEntry.Name
DoEvents
End If
Next objAddressEntry

' Close the text file
oFile.Close
Set fso = Nothing
Set oFile = Nothing

' Define range called "Addresses" to the list of emails
' Sheets("Address").Cells(1, 1).Resize(intCounter, 1).Name = "Addresses"
error:
Set objOutlook = Nothing
Application.StatusBar = False
Application.EnableEvents = False
End Sub

请注意,我很少使用 VBA,所以如果这是一个微不足道的问题,我深表歉意。我在之前的 questions/answers 中很难找到任何相关内容,因为搜索词非常广泛。

My question is: How do I make it write the actual value rather than the name of the object?

如果你只想写 .Name 那么,

oFile.WriteLine objAddressEntry.Name & vbNewLine

...但是如果您想将 .Name 写在引号中,

oFile.WriteLine chr(34) & objAddressEntry.Name & chr(34) & vbNewLine