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
Solved

Access VBA email a spreadsheet with linked tables

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

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

Question has a verified solution.

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

A little background as to how I came to I design this code: Around 5 years ago I designed an add-in that formatted Excel files to a corporate standard, applying different cell colours and font type depending on whether the cells contained inputs,…
In a multiple monitor setup, if you don't want to use AutoCenter to position your popup forms, you have a problem: where will they appear?  Sometimes you may have an additional problem: where the devil did they go?  If you last had a popup form open…
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
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…

829 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