How to identify a table in outlook through VBA

Kuljack

Active Member
Joined
Aug 14, 2015
Messages
327
Hello Forum,

This is a repost, originally asked in the Excel form. I believe this location may be more appropriate as it is geared toward outlook VBA behaviors.

---

Hello forum,

Using MS 2010 programs here.

Currently, I have a macro in excel that triggers from a user form to reach out to an outlook template, pull data from the user form and update the template. This is working perfectly, but I have multiple templates for certain scenarios.

I would like to determine how I could rewrite the formula to evaluate these scenarios in the user form and dictate the necessary template through one email template. So, rather than having six templates for the varied formats needed, I would have all 6 formats in one email template. The macro in excel would read certain fields to determine the correct scenario and after the email template is opened, delete the other 5 templates from the email.

I'm attempting to do this by creating the template variations in separate tables and hoping Excel/Outlook VBA can find these tables to delete the appropriate ones but having no luck.

Here is the current working code, values generalized for obvious reasons:

Code:
Sub Email_1()
Application.ScreenUpdating = False
    Dim myOlApp As Outlook.Application
    Dim MyItem As Outlook.MailItem
    
    Dim oVal1 As String
    Dim oVal2 As String
    Dim oVal3 As String
    Dim oVal4 As String

    oVal1 = UserForm2.Controls("TextBox6").Value
    oVal2 = UserForm2.Controls("TextBox2").Value
    oVal3 = UserForm2.Controls("TextBox15").Value & ", " & UserForm2.Controls("TextBox16").Value
    oVal4 = UserForm2.Controls("TextBox17").Value
    
    Dim Hyperlink As Range
            Set myOlApp = CreateObject("Outlook.Application")
            Set MyItem = myOlApp.CreateItemFromTemplate( _
            "[URL="file://dtcnas-mnmi004/C_MTG_Groups/Default_Proj_Team/Change"]mydocuments\template[/URL].oft")
            With MyItem
                .CC = Replace(.CC, "dropval3", oVal3)
                .CC = Replace(.CC, "dropval4", oVal4)
                .Subject = Replace(.Subject, "dropval1", "This " & oVal1)
                .Subject = Replace(.Subject, "dropval2", oVal2)
                .HTMLBody = Replace(.HTMLBody, "Link", " & oVal1 & "> oVal1# " & oVal1& " - " & oVal2 & " ")
                 .Display
             End With
 Application.ScreenUpdating = True
 End Sub

What I'd like is to have each table be referenced, so if circumstances dictate table 1 should be used - tables 2-6 are removed. The same for every other scenario.. scenario 3 appropriate 1-2/4-6 be deleted.

I've gotten as far as this:

Code:
...extending from previous code
 
             Set myOlApp = CreateObject("Outlook.Application")
             Set MyItem = myOlApp.CreateItemFromTemplate( _
             "mydocuments\template.oft")
             With MyItem
                 Dim atabl As Outlook.Table
                
                 .Display
             End With[
CODE]

However, I'm unable to find the right path to identify individual tables. Thinking it would look something like this, but it's not working...
Set atabl = myitem.table(1)
Select.atabl
Selection.Delete
[/CODE]

An alternative approach I thought might be to drop anchors like table1_start & table1_end, and write the code to select the contents between those two fields but finding nothing that works to do this in outlook.

Appreciate any help!
 

Excel Facts

Create a Pivot Table on a Map
If your data has zip codes, postal codes, or city names, select the data and use Insert, 3D Map. (Found to right of chart icons).

Forum statistics

Threads
1,223,793
Messages
6,174,635
Members
452,575
Latest member
Fstick546

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