Save an Excel Workbook from Code

koolwaters

Active Member
Joined
May 16, 2007
Messages
403
Hi!

The code below is used to export data from an Access table to a specific cell in an Excel worksheet.

Code:
Public Sub ExpExcel()
Dim cnn As ADODB.Connection
Dim MyRecordset As New ADODB.Recordset
Dim MySQL As String
Dim MySheetPath As String
Dim Xl As Object
Dim XlBook As Object
Dim XlSheet As Object
Dim db As DAO.Database
Set cnn = CurrentProject.Connection

MyRecordset.ActiveConnection = cnn

    If Not IsNothing(Me.StartDate) Then
        If Not IsDate(Me.StartDate) Then
            MsgBox "You must enter a valid 'Beginning' date.", vbExclamation, gstrAppTitle
            Me.StartDate.SetFocus
            Exit Sub
        End If
    End If
    If Not IsNothing(Me.EndDate) Then
        If Not IsDate(Me.EndDate) Then
            MsgBox "You must enter a valid 'Ending' date.", vbExclamation, gstrAppTitle
            Me.EndDate.SetFocus
            Exit Sub
        End If

        If Not IsNothing(Me.StartDate) Then
            If Me.EndDate < Me.StartDate Then
                MsgBox "'Ending' Date must not be earlier than 'Beginning' Date.", _
                    vbExclamation, gstrAppTitle
                Me.EndDate.SetFocus
                Exit Sub
            End If
        End If
    End If


DoCmd.SetWarnings False
DoCmd.OpenQuery ("qmtblXptFundingBySegmentRawData")
MySQL = "SELECT * FROM tblXptFundingBySegmentRawData;"
MyRecordset.Open MySQL

MySheetPath = GetFEPath & "Excel Files\Payments Allocated Raw Data.xltx"

Set Xl = CreateObject("Excel.Application")
Set XlBook = GetObject(MySheetPath)

Xl.Visible = True
XlBook.Windows(1).Visible = True
XlBook.Activate = True

Set XlSheet = XlBook.Worksheets("RawData")
XlSheet.Range("RangeRawData").ClearContents
XlSheet.Range("A4").CopyFromRecordset MyRecordset

Set XlSheet = XlBook.Worksheets("Main")
XlSheet.Range("B12") = "Payments Allocated Raw Data for the period " & Format(StartDate, "dd-mmm-yyyy") & " to " & Format(EndDate, "dd-mmm-yyyy")
XlBook.SaveAs GetFEPath & "Excel Files\Payments Allocated Raw Data- " & Format(Now(), "dd-mmm-yyyy") & ".xlsx"
    
MyRecordset.Close
MyRecordset.Close
Set cnn = Nothing
Set Xl = Nothing
Set XlBook = Nothing
Set XlSheet = Nothing

DoCmd.SetWarnings True

End Sub

The code works fine with the exception of the SaveAs .

Code:
XlBook.SaveAs GetFEPath & "Excel Files\Payments Allocated Raw Data- " & Format(Now(), "dd-mmm-yyyy") & ".xlsx"

I just want to be able to save the template but the workbook opens but it is not saved with the new name.

The database is split and GetFEPath returns the path of the frontend.

Thanks for any help.
 
To Xenou:

Based on what you said regarding the template, I change the file to an excel workbook
Code:
MySheetPath = GetFEPath & "Excel Files\Payments Allocated Raw Data.xlsx"

That seems to have done the trick. This is the code that I have used and I have just added a few lines to delete the file if it one exists with the name I want to Save with before the SaveAs is executed. Just need to add my error handling.

Code:
Public Sub ExpExcel()
Dim cnn As ADODB.Connection
Dim MyRecordset As New ADODB.Recordset
Dim MySQL As String
Dim MySheetPath As String
Dim MySavePath As String
Dim Xl As Object
Dim XlBook As Object
Dim XlSheet As Object
Dim db As DAO.Database
Set cnn = CurrentProject.Connection

MyRecordset.ActiveConnection = cnn

    If Not IsNothing(Me.StartDate) Then
        If Not IsDate(Me.StartDate) Then
            MsgBox "You must enter a valid 'Beginning' date.", vbExclamation, gstrAppTitle
            Me.StartDate.SetFocus
            Exit Sub
        End If
    End If
    If Not IsNothing(Me.EndDate) Then
        If Not IsDate(Me.EndDate) Then
            MsgBox "You must enter a valid 'Ending' date.", vbExclamation, gstrAppTitle
            Me.EndDate.SetFocus
            Exit Sub
        End If

        If Not IsNothing(Me.StartDate) Then
            If Me.EndDate < Me.StartDate Then
                MsgBox "'Ending' Date must not be earlier than 'Beginning' Date.", _
                    vbExclamation, gstrAppTitle
                Me.EndDate.SetFocus
                Exit Sub
            End If
        End If
    End If


DoCmd.SetWarnings False
DoCmd.OpenQuery ("qmtblXptFundingBySegmentRawData")
MySQL = "SELECT * FROM tblXptFundingBySegmentRawData;"
MyRecordset.Open MySQL

MySheetPath = GetFEPath & "Excel Files\Payments Allocated Raw Data.xlsx"
MySavePath = GetFEPath & "Excel Files\Payments Allocated Raw Data, " & Format(Now(), "dd-mmm-yyyy") & ".xlsx"

Set Xl = CreateObject("Excel.Application")
Set XlBook = GetObject(MySheetPath)

Xl.Visible = True
XlBook.Windows(1).Visible = True
XlBook.Activate = True

Set XlSheet = XlBook.Worksheets("RawData")
XlSheet.Range("RangeRawData").ClearContents
XlSheet.Range("A4").CopyFromRecordset MyRecordset

Set XlSheet = XlBook.Worksheets("Main")
XlSheet.Range("B12") = "Payments Allocated Raw Data for the period " & Format(StartDate, "dd-mmm-yyyy") & " to " & Format(EndDate, "dd-mmm-yyyy")

If "" <> Dir(MySavePath) Then
    Kill MySavePath
End If

XlBook.SaveAs MySavePath
    
MyRecordset.Close
MyRecordset.Close
Set cnn = Nothing
Set Xl = Nothing
Set XlBook = Nothing
Set XlSheet = Nothing

DoCmd.SetWarnings True

End Sub

To Norie:

C is my Windows 7 partition and D is my Vista partition on my laptop.

Thanks as usual for all of the help. It is appreciated. :)
 
Upvote 0

Excel Facts

When did Power Query debut in Excel?
Although it was an add-in in Excel 2010 & Excel 2013, Power Query became a part of Excel in 2016, in Data, Get & Transform Data.

Forum statistics

Threads
1,224,518
Messages
6,179,253
Members
452,900
Latest member
LisaGo

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top