Solved

Access VBA email a spreadsheet with linked tables

Posted on 2011-03-06
3
401 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:Murray Brown
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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:Murray Brown
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

This article describes a method of delivering Word templates for use in merging Access data to Word documents, that requires no computer knowledge on the part of the recipient -- the templates are saved in table fields, and are extracted and install…
If you need a simple but flexible process for maintaining an audit trail of who created, edited, or deleted data from a table, or multiple tables, and you can do all of your work from within a form, this simple Audit Log will work for you.
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

635 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