Solved

Access VBA email a spreadsheet with linked tables

Posted on 2011-03-06
3
395 Views
Last Modified: 2012-05-11
Hi

I have an Excel spreadsheet that contains tables that are linked to my Access database.
The spreadsheet contains the code  ThisWorkbook.RefreshAll to automatically refresh
the data when it is opened. I need to automatically refresh these links from within Access
and email the spreadsheet via Outlook.
What Access VBA code would I use to do this
Thanks
0
Comment
Question by:murbro
3 Comments
 
LVL 30

Accepted Solution

by:
SiddharthRout earned 500 total points
ID: 35046918
Here is some code which I wrote on the fly. Please amend it for realistic situations :)

UNTESTED

'~~> Set a reference to Outlook Object Library
Sub RefreshWBook()
    Dim ObjOutlook As Outlook.Application
    Dim ObjOutlookMsg As Outlook.MailItem
    Dim objOutlookRecip As Outlook.Recipient
    Dim objOutlookAttach As Outlook.Attachment
    Dim oXLApp As Object
    Dim wbTest1 As Object
        
    '~~> Establish an EXCEL application object
    On Error Resume Next
    Set oXLApp = GetObject(, "Excel.Application")
    
    '~~> If not found then create new instance
    If Err.Number <> 0 Then
        Set oXLApp = CreateObject("Excel.Application")
    End If
    Err.Clear
    On Error GoTo 0
    
    '~~> Hide Excel
    oXLApp.Visible = False
    
    '~~> Open files
    Set wbTest1 = oXLApp.Workbooks.Open("C:\MyFile.xls")
    
    '~~> Refresh
    wbTest1.RefreshAll
    
    '~~> Close and save
    wbTest1.Close savechanges:=True
 
    Set ObjOutlook = New Outlook.Application
    Set ObjOutlookMsg = ObjOutlook.CreateItem(olMailItem)
 
    With ObjOutlookMsg
       Set objOutlookRecip = .Recipients.Add("'TO' ADDRESS GOES HERE")
       objOutlookRecip.Type = olTo
       .Subject = "SUBJECT GOES HERE"
       Set objectlookAttach = .Attachments.Add("C:\MyFile.xls")
    
       For Each objOutlookRecip In .Recipients
            If Not objOutlookRecip.Resolve Then
                 ObjOutlookMsg.Display
            End If
       Next
       .Display
       '~~> Uncomment the below to send the email
       '.Send
    End With
    
    '~~> CLEANUP (VERY IMPROTANT)
    Set ObjOutlookMsg = Nothing
    'ObjOutlook.Quit
    Set ObjOutlook = Nothing
    Set wbTest1 = Nothing
    Set wbTest2 = Nothing
    oXLApp.Quit
    Set oXLApp = Nothing
    
    MsgBox "DONE"
End Sub

Open in new window


Sid
0
 
LVL 74

Expert Comment

by:Jeffrey Coachman
ID: 35060369
<What Access VBA code would I use to do this>
Can you post the code you have so far?
0
 

Author Closing Comment

by:murbro
ID: 35073159
thanks very much. Sorry about late reply
0

Featured Post

Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This Article briefly covers methods of calculating the NPV and IRR variants in Excel as well as the limitations in calculating and interpreting IRR results. Paraphrasing Richard Shockley, author of my favourite finance reference tex…
This code takes an Excel list of URL’s and adds a header titled “URL List”. It then searches through all URL’s in column “A”, looking for duplicates. When a duplicate is found, it is moved to the top of the list. The duplicate URL’s are then highlig…
The viewer will learn how to use a discrete random variable to simulate the return on an investment over a period of years, create a Monte Carlo simulation using the discrete random variable, and create a graph to represent the possible returns over…
This Micro Tutorial will demonstrate the scrolling table in Microsoft Excel using the INDEX function.

895 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now