Table Export Issue

juneau730

Board Regular
Joined
Jun 7, 2018
Messages
111
Hey all, having a bit of a hiccup up with my table export function from my dBase. It's set to export to .xls, but I would really like it to be .xlsx since we are running Office 365/2016. However, when I change the output parameters to .xlsx, I get an error in my formatExcellSS code.

Any thoughts on why this is happening would be great.

Here is my cmdExport code

Code:
Private Sub cmdExport_Click()    Dim arrCurrentFormInfo
    Dim strCurrentForm
    Dim strCaption
    Dim strOutputFileName
    Dim strRecordSource
    Dim strOutputFile
    Dim objShell
    
    Set objShell = VBA.CreateObject("wscript.shell")
    
    arrCurrentFormInfo = Split(txtCurrentForm.Value, ":")
    If UBound(arrCurrentFormInfo) = 1 Then
        strCurrentForm = Replace(arrCurrentFormInfo(0), "form.", "")
        strOutputFileName = arrCurrentFormInfo(1)
    Else
        strCurrentForm = Replace(txtCurrentForm.Value, "form.", "")
        strOutputFileName = strCurrentForm
    End If
    
    strRecordSource = Me!NavigationSubform.Form.RecordSource


    strOutputFile = Application.CurrentProject.Path & "\" & strOutputFileName & ".xlsx"
    'DoCmd.OutputTo acOutputForm, frmCurrentForm, acFormatXLSX, Application.CurrentProject.Path & "\" & txtOutputFileName & ".xlsx", -1
    If Left(strRecordSource, 3) = "tbl" Then
        DoCmd.OutputTo acOutputTable, strRecordSource, acFormatXLS, strOutputFile
    Else
        DoCmd.OutputTo acOutputQuery, strRecordSource, acFormatXLS, strOutputFile
    End If
    
    Call FormatExcelSS(strOutputFile, "B1")
    objShell.Run "Excel.exe" & " " & Chr(34) & strOutputFile & Chr(34), , True
End Sub

Here is the FormatExcelSS coding, and it errors our on the Bold/Red line.

Code:
Sub FormatExcelSS(sXLFile, strSortRange)    Dim xlApp, xlWB
    Dim objRange, objRange2
    
    Set xlApp = VBA.CreateObject("excel.application")
[COLOR=#ff0000][B]    Set xlWB = xlApp.Workbooks.Open(sXLFile)[/B][/COLOR]
    With xlWB 'open file        With .ActiveSheet
            .Rows(1).Font.Bold = True '1.  Bold the headers (always in row 1)
            .Rows(1).Interior.ColorIndex = 15
            .AutoFilterMode = False 'turn off any existing autofilter just in Case
            Set objRange = .UsedRange
            objRange.Columns.ColumnWidth = 100
            .Rows.AutoFit
            .Rows(1).AutoFilter '2. Turn on AutoFilter for all coloms
            .Columns.AutoFit '3. Set Column width to AutoFit Selection
            '4. Set a freeze under column 1 so that the header is always present at the top
            .Range("A2").Select
            xlApp.ActiveWindow.FreezePanes = True
            'Sort on specified column
            Const xlAscending = 1
            Const xlYes = 1
            Set objRange2 = .Range(strSortRange)
            objRange.Sort objRange2, xlAscending, , , , , , xlYes
        End With
        .Worksheets(1).Activate
        .Close True 'save and close
    End With
    xlApp.Quit
    Set xlApp = Nothing
End Sub
 

Excel Facts

Lock one reference in a formula
Need 1 part of a formula to always point to the same range? use $ signs: $V$2:$Z$99 will always point to V2:Z99, even after copying
Thanks Joe4, that partially fixed it. Now the data that was being exported as .xls is output as .xlsx
However, now other data isn't exporting correctly. Let me dig into it some and if I can't see why, I'll throw something up in a bit.
 
Upvote 0
Note that you had that line in multiple places.
 
Upvote 0
I think I got it all fixed now.
It was exporting the tables, but it wasn't opening them automatically like it should. When I tried to open it, there were errors, come to find out the date/time fields in the tables weren't set properly, so it didn't like the output.

Again, thank you for your help! Awesome resources on this site.
 
Upvote 0

Forum statistics

Threads
1,214,400
Messages
6,119,292
Members
448,885
Latest member
LokiSonic

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