Invalid Qualifier for String.Add in Outlook VBA

社会主义新天地 提交于 2019-12-11 04:24:25

问题


You all have been so helpful, and I was wondering whether I might trouble you a bit more. I have nearly completed my conversion from VB.net to VBA for Outlook, and in order to complete that, I need some information on what exactly a particular piece of code is returning. If you all can help out with that, the problem may go away; if not, I might need some help on this invalid qualifier error.

From what I understand, I declare an 'array' in VBA with a command like this:

Dim Computers(1, 1) As String

Which produces a 2x2 array. I then try to fill it like this:

Computers.Add ComputerName, ErrorState

where ComputerName and ErrorState are variables which I have already filled. This results in the error. I will provide you with the entire relevant section below. I need to know how many relevant items are in the outlook inbox, which means checking if they were sent on today's date and by the correct sender. I need this in order to get the correct dimensions on the array Computers. (The array is filled in right now as I know the correct dimensions, but in practise I will not.) I took a piece of code which I found through google, the Scripting Dictionary output, but I do not fully understand it. I need just the number of emails, without any irrelevant text, and I was hoping that the debug line would be able to tell me which command I would need to return the number and nothing else. Unfortunately, because of the above error, I cannot get to this line. Even by stepping through the code, the very first thing it tells me is that there is a problem here. The entire relevant section is below. I know that the formatting is problematic, but I have only been working with VB.NET for 4 days, and this is my second day of working with VBA, so I do not quite have everything down yet. There's some irrelevant stuff in here, I'm pretty sure, but I do not have the experience to make the call of whether something is relevant or not, so I left it all. Sorry for the length!

Private Sub Main()
    Dim objOutlook As Outlook.Application
    Dim Inbox As Outlook.MAPIFolder
    Dim InboxItems As Outlook.Items
    Dim Mailobject As Object
    Dim strDate As String
    Dim ComputerName As Object
    Dim ErrorState As Object
    Dim DateToday As String: DateToday = Format(Date, "yyyy/MM/dd")
    Dim SenderEmail As String
    Dim Computers(1, 1) As String
    Dim compLength As Integer
    Dim disabledList As Collection
    Dim enabledList As Collection
    Dim unknownList As Collection
    Dim notpresentList As Collection
    Dim problemList As Collection
    Dim cArrayLength As Integer
    Dim I As Integer
    Dim J As Integer
    Dim objOutlookMsg As Outlook.MailItem
    Dim objOutlookRecip As Outlook.Recipient
    Dim EmailCount As Integer
    Dim compArray() As String
    '\\ load csv declarations
    Dim file_name As String
    Dim fnum As Integer
    Dim whole_file As String
    Dim lines As Variant
    Dim one_line As Variant
    Dim num_rows As Long
    Dim num_cols As Long

    Dim R As Long
    Dim C As Long
    Set disabledList = New Collection
    Set enabledList = New Collection
    Set unknownList = New Collection
    Set notpresentList = New Collection
    Set problemList = New Collection
    '\\\\\
    '\\Retrieve info from outlook, add to sorted list Computers
    '\\\\\

    objOutlook = CreateObject("Outlook.Application")
    Inbox = objOutlook.GetNamespace("Mapi").GetDefaultFolder(6)
    InboxItems = Inbox.Items
    InboxItems.SetColumns ("SentOn")
    Dim myItems As Outlook.Items
    Dim dict As Object
    Dim msg As String
    Set dict = CreateObject("Scripting.Dictionary")
    myItems.SetColumns ("SentOn")
    EmailCount = InboxItems.Count
    For Each Mailobject In InboxItems
        strDate = GetDate(Mailobject.SentOn)
        If Not dict.Exists(strDate) Then
        dict(strDate) = 0
        End If
        dict(strDate) = CLng(dict(strDate)) + 1
        Next Mailobject
    '\\ need redo to assign number of objects to CompLength
    msg = ""
    For Each o In dict.Keys
        msg = msg & o & ": " & dict(o) & " items" & vbCrLf
        Next
    Debug.Print msg
    For Each Mailobject In InboxItems
        ComputerName = Mailobject.Subject
        ErrorState = Mailobject.Body
        strDate = GetDate(Mailobject.SentOn)
        SenderEmail = Mailobject.SenderEmailAddress
        If strDate = DateToday And SenderEmail = "itadmin@email.org" Then
            '\\ Syntax is (key, value)
            Computers.Add ComputerName, ErrorState
        End If
        'MsgBox(Mailobject.Subject)
        'MsgBox(Mailobject.SenderName)
        'MsgBox(Mailobject.To)
        'MsgBox(Mailobject.Body)
    Next Mailobject 

This website has been incredibly helpful as not only a place for me to ask questions but also as a place for me to find relevant information without having to trouble you all. Unfortunately, it is all too often that I do have to trouble you all, but every time you have been very kind and helpful. And I would like to thank you for that.


回答1:


you're trying to use the Add() method from a Collection to assign an element in an array. To assign an element in a 2-D array you'd use arr(a,b)=someValue where a and b are numeric values. – Tim Williams

Question with no answers, but issue solved in the comments



来源:https://stackoverflow.com/questions/17032589/invalid-qualifier-for-string-add-in-outlook-vba

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!