When Sending from Excel to Outlook?

JeffGrant

Well-known Member
Joined
Apr 7, 2021
Messages
558
Office Version
  1. 365
Platform
  1. Windows
Hi All,

Can somebody please tell me why ?

In all the examples that I find on the web, I see that when sending information from Excel (or word) to Outlook, there are these 4 steps for sending variables.

'Step 1 - Declare the Variable
Dim rngTo As Excel.Range

'Step 2 - Set the Variable
Set rngTo = .Range("B11")

'Step 3 - use the Variable
.To = rngTo.Value

'Step 4 - Release the Variable
Set rngTo = Nothing


Please excuse my ignorance, but I don't understand why combining Steps 2 & 3 into one line, like below, is not a good way of doing this.
.To = .Range("B11")

Can I assume that if I use .To = .Range("B11"), that the ".To" may not be explicity declared as an Excel Range, which could be contributing periodic errors?

Can somebody please help me understand?

Thanks in advance
 
Last edited:

Excel Facts

Add Bullets to Range
Select range. Press Ctrl+1. On Number tab, choose Custom. Type Alt+7 then space then @ sign (using 7 on numeric keypad)
Generally you declare a variable, or in this case an object of type range, to use the object in different parts of the code. That way you can get all the properties and methods of the object without having to type the full cell name.
I explain with the following example:

VBA Code:
Sub ex()
  'Step 1. Declare the object
  Dim rng As Range
  
  'Step 2. You set the cell object to the rng object
  Set rng = Sheets("Sheet1").Range("B11")
  
  With CreateObject("Outlook.Application").CreateItem(0)
    'Step 3. Use the object (Notice how it is using the Value property of the object to pass the value to the .To statement)
    .To = rng.Value
    .Display
  End With
  
  'Step 4. Release
  Set rng = Nothing
End Sub

You use variables to have everything perfectly organized, but sometimes when you are going to use the object only once, as in this example, it is not necessary to declare the object, and it is perfectly valid to use it in the following way:

VBA Code:
Sub ex2()
  With CreateObject("Outlook.Application").CreateItem(0)
    'Everything in one step
    .To = Sheets("Sheet1").Range("B11").Value
    .Display
  End With
End Sub

In VBA it is more comfortable to work with simplified code than to be abundant in declarations.

I hope the above helps you.
🤗
 
Upvote 0
Thanks Dante. I just checked my code. So I am not specifically doing anything wrong that would cause the occasional errors I am getting.

Because to redo the code as in the example I put up, would be adding hundreds of lines to my model.
 
Last edited:
Upvote 0

Forum statistics

Threads
1,224,811
Messages
6,181,081
Members
453,021
Latest member
Justyna P

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