Solved

Access VBA email a spreadsheet with linked tables

Posted on 2011-03-06
3
394 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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Introduction The Visual Basic for Applications (VBA) language is at the heart of every application that you write. It is your key to taking Access beyond the world of wizards into a world where anything is possible. This article introduces you to…
Workbook link problems after copying tabs to a new workbook? David Miller (dlmille) Intro Have you either copied sheets to a new workbook, and after having saved and opened that workbook, you find that there are links back to the original sou…
The viewer will learn how to use the =DISCRINV command to create a discrete random variable, use this command to model a set of probabilities and outcomes in a Monte Carlo simulation, and learn how to find the standard deviation of a set of probabil…
This Micro Tutorial demonstrates in Microsoft Excel how to consolidate your marketing data by creating an interactive charts using form controls. This creates cool drop-downs for viewers of your chart to choose from.

708 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

19 Experts available now in Live!

Get 1:1 Help Now