The following code will process a document and export each individual report tab to MS Excel. It is currently done in the “AfterRefresh” event but could be called as a subroutine instead.
Original Author: Unknown
Original Author Comments:
Private Sub Document_AfterRefresh()
On Error GoTo error_handler
Dim Doc As Document
Dim Rep As Report
Dim i As Integer
Dim ExcelDoc As String 'Stores the name of the Excel document to copy all report tabs to
Dim Path As String 'Stores the path in while the Excel files are created.
Dim MailTo As String 'Stores the name of the person who is to receive the report via e-mail
ExcelDoc = "ex_multi_tab" 'The name of the Excel document you plan to send to user(s) through e-mail.
Path = "\\ServerOrFolderName\" 'The path on your PC that you wish to save the Excel document to. Leave off the letter and just include the path.
'Path = "\\Server\FolderName" 'The path if you plan to scheduling this document on the Broadcast Agent server
MailTo = "Outlook UserName" ' Use this MailTo address if e-mailing an internal user who does not have access to the internet.
'MailTo = "username@company.com" 'Use this MailTo if e-mailing someone who has an internet address.
'Save report tabs as text files
Set Doc = ActiveDocument
For i = 1 To Doc.Reports.Count
Set Rep = Doc.Reports.Item(i)
If Dir(Path & Rep.Name & ".txt") <> " " Then
Kill Path & Rep.Name & ".txt"
End If
Rep.ExportAsText (Path & Rep.Name & ".txt")
Next i
Set vbExcel = CreateObject("Excel.Application")
With vbExcel
'Create a new workbook to import text files into
If Dir(Path & ExcelDoc & ".xls") <> " " Then
Kill Path & ExcelDoc & ".xls"
End If
.Workbooks.Add
.ActiveWorkbook.SaveAs Path & ExcelDoc & ".xls"
'Open source workbook, save as an Excel file, and import into destination Excel file.
For i = 1 To Doc.Reports.Count
Set Rep = Doc.Reports.Item(i)
.Workbooks.Open Path & Rep.Name & ".txt"
If Dir(Path & Rep.Name & ".xls") <> " " Then
Kill Path & Rep.Name & ".xls"
End If
.ActiveWorkbook.SaveAs Path & Rep.Name & ".xls"
'Copy worksheets to the destination Excel document
.Workbooks(Rep.Name & ".xls").Sheets(Rep.Name).Move _
Before:=.Workbooks(ExcelDoc & ".xls").Sheets("Sheet1")
Next i
'Save workbooks
'.Workbooks(ExcelDoc & ".xls").Sheets("Sheet1").Delete
'.Workbooks(ExcelDoc & ".xls").Sheets("Sheet2").Delete
'.Workbooks(ExcelDoc & ".xls").Sheets("Sheet3").Delete
'Close both workbooks
.ActiveWorkbook.Save
Workbooks.Close
'Close Excel application object
.Quit
End With
'Release object variable to free memory
Set vbExcel = Nothing
'Send Through Outlook
Set OlkApp = CreateObject("Outlook.Application")
Set NewMail = OlkApp.CreateItem(olMailItem)
Set Attachments = NewMail.Attachments
Attachments.Add (Path & ExcelDoc & ".xls")
With NewMail
.To = MailTo
.Body = "Your multiple-tab report is attached and available for viewing in Excel."
.Subject = "Sample - download multiple tab report and send via email"
.Importance = 1
.Send
End With
Set OlkApp = Nothing
'Delete files used to create the e-mail attachment
Kill Path & ExcelDoc & ".xls"
For i = 1 To Doc.Reports.Count
Set Rep = Doc.Reports.Item(i)
Kill Path & Rep.Name & ".txt"
Kill Path & Rep.Name & ".xls"
Next i
'Export Completed
Exit Sub
error_handler:
If Err.Number = 53 Then Resume Next
MsgBox Err.Number & " - " & Err.Description
Workbooks.Close
vbExcel.Quit
Set vbExcel = Nothing
Set OlkApp = Nothing
End Sub
Private Sub Document_BeforeSave(Cancel As Boolean)
End Sub
ExportExcelMail.zip (20.0 KB)
BOB Downloads (BOB member since 2003-05-05)