Excel VBA: Pass value of string variable to variable of another type

dougdrex

Board Regular
Joined
Sep 27, 2013
Messages
79
I'm using Excel 2010.

Maybe this is something that cannot be done because I have searched the internet for instructions, but to no avail.

I have a string variable that gets entered into the "Cc" field of an email. However, before I send out the email message (through Outlook), I need to check it in our internal global address book to make sure it is a valid ID (sometimes employees leave the company) so it won't cause an error message.

In order to use the .ResolveAll command, the value needs to be in a variable defined as an Outlook.Recipient (at least that is my understanding). How can I pass the value of the string variable to the other variable without it generating an error (I've tried several methods).

If posting the significant portions of my code would help, please let me know.


Thanks!
Doug
 

Excel Facts

Will the fill handle fill 1, 2, 3?
Yes! Type 1 in a cell. Hold down Ctrl while you drag the fill handle.
Doug

Are you adding recipients to the email?

You can do that using the Recepients collection.
Code:
Dim olEmail As Object
Dim olRecpt As Object

    Set olEmail = Application.CreateItem(0) ' olMailItem

    Set olRecpt = olEmail.Recipients.Add ("Jon Grande") 

    olRecpt.Type = 2 ' olCC
You should then be able to use ResolveAll like this.
Code:
If olEmaile.Recipients.ResolveAll Then
    MsgBox "All recipients resolved."
Else
    MsgBox "All recipients not resolved."
    Exit Sub
End If

' code to send email

PS If all recipients aren't resolved you can loop through the collection to find which ones weren't.
Code:
For Each olRecpt In olEmail.Recipients
    If Not olRecpt.Resolved Then
        MsgBox olRecpt.Name
    End If
Next olRecpt
 
Upvote 0
Hi Norie,

So I may have created my macro in a way that is less than optimal. Perhaps you can help answer that question. Let me try to summarize what I need to accomplish. If more information is required, just let me know.

I have two columns where employee IDs are provided. One column will need to be inserted in the "To" field of an email, while the values in the other column will be inserted in the "Cc" field. There are other details, but they may be unnecessary for the purpose of this discussion. I need to figure out a way to identify when one or more of the values is not recognized by Outlook and display the Employee IDs to the user.

Below is the code where I am declaring and defining the variables being used for creating and sending the emails.

Code:
    Dim OlApp As Outlook.Application
    Dim OlNS As Outlook.Namespace
    Dim OlFolder As Outlook.MAPIFolder
    Dim OlMailItem As Outlook.MailItem
    Dim strMaker As String
    Dim strChecker As String

    strMaker = GetVisibleMaker(Range("H2", Cells(Rows.Count, "H").End(xlUp)))
    strChecker = strAuthID

    strTo = strChecker
    strCC = strMaker


    With OlMailItem
        .To = strTo
        .CC = strCC

P.S. - GetVisibleMaker is a function that retrieves the visible cells in a specific column after it has been filtered so that only the visible values are assigned to the variable.
 
Last edited:
Upvote 0
To you want to set one email to multiple recipients?
 
Upvote 0
Any ideas on how to identify the Employee ID that Outlook is not recognizing with VBA code?

If necessary, I can post more of the code that I have so far.
 
Upvote 0
I posted code that will show you which recipients aren't resolved.
Code:
For Each olRecpt In olEmail.Recipients
    If Not olRecpt.Resolved Then
        MsgBox olRecpt.Name
    End If
Next olRecpt
Obviously this uses a message box to show the unresolved names but it would be easy to adapt to output a list, or whatever.
 
Upvote 0
I posted code that will show you which recipients aren't resolved.
Code:
For Each olRecpt In olEmail.Recipients
    If Not olRecpt.Resolved Then
        MsgBox olRecpt.Name
    End If
Next olRecpt
Obviously this uses a message box to show the unresolved names but it would be easy to adapt to output a list, or whatever.

A couple more questions...

How should the variables be defined/declared?
How can I pass the values in the current string variables to the ones mentioned in your code?
 
Last edited:
Upvote 0
olEmail, or whatever the name of the variable you are using for the mail item, would be declared as Outlook.MailItem.

olRecpt, or whatever variable you want to use for the recipients, would be declared as Outlook.Recipient.

You wouldn't really pass the string variables to the code I posted.

What you would do is create the email and add all the recipients, i.e. the Employee IDs, to it and then ResolveAll on the Recipients collection of the email like this.
Code:
If olEmail.Recipients.ResolveAll Then
    MsgBox "All recipients resolved."
Else
    MsgBox "All recipients not resolved."

    ' find out which Employee IDs weren't resolved
    For Each olRecpt In olEmail.Recipients

        ' output name of unresolved recipient to the Immediate Window (CTRL+G)
        If Not olRecpt.Resolved Then
            Debug.Print olRecpt.Name
        End If

    Next olRecpt

    Exit Sub

End If
 
Upvote 0
olEmail, or whatever the name of the variable you are using for the mail item, would be declared as Outlook.MailItem.

olRecpt, or whatever variable you want to use for the recipients, would be declared as Outlook.Recipient.

You wouldn't really pass the string variables to the code I posted.

What you would do is create the email and add all the recipients, i.e. the Employee IDs, to it and then ResolveAll on the Recipients collection of the email like this.
Code:
If olEmail.Recipients.ResolveAll Then
    MsgBox "All recipients resolved."
Else
    MsgBox "All recipients not resolved."

    ' find out which Employee IDs weren't resolved
    For Each olRecpt In olEmail.Recipients

        ' output name of unresolved recipient to the Immediate Window (CTRL+G)
        If Not olRecpt.Resolved Then
            Debug.Print olRecpt.Name
        End If

    Next olRecpt

    Exit Sub

End If

That makes sense. But how would the VBA code send the different values to the "To" and "Cc" fields?
 
Upvote 0

Forum statistics

Threads
1,221,310
Messages
6,159,176
Members
451,543
Latest member
cesymcox

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top