【问题标题】:Write value to text file, not name of object将值写入文本文件,而不是对象的名称
【发布时间】:2018-02-23 15:15:16
【问题描述】:

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

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,所以如果这是一个微不足道的问题,我深表歉意。由于搜索词非常广泛,我一直在努力在以前的问题/答案中找到任何相关的内容。

我的问题是:如何让它写入实际值而不是对象的名称?

【问题讨论】:

  • oFile.WriteLine objAddressEntry.Name &amp; vbNewLine 无引号

标签: vba excel string-concatenation


【解决方案1】:

如果你只想写 .Name 那么,

oFile.WriteLine objAddressEntry.Name & vbNewLine

...但是如果你想在引号中写下.Name,那么,

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

【讨论】:

  • 完美,谢谢。我真的应该自己弄清楚这一点,但这是漫长的一周:)
  • 不用担心;很高兴你得到了排序。
猜你喜欢
  • 2019-05-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-11-06
  • 2016-09-10
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多