Unable to pass Excel worksheet to a function in VBA Access - ms-access

I am using VBA in an access application to create an Excel file. I am creating the excel application and worksheet just fine but when I try to pass that worksheet to another function, I get this error:
Run-time error '438':
Object doesn't support this property or method
Here is the code where I set up my excel application:
Public Sub setUpExcel()
Dim XLAPP As Excel.Application
Set XLAPP = New Excel.Application
XLAPP.Visible = True
Dim WKB As Excel.Workbook
Dim WKS As Excel.Worksheet, WKS2 As Excel.Worksheet
Set WKB = XLAPP.Workbooks.Open(excelFile)
Set WKS = WKB.ActiveSheet '.Worksheets(1)
WKS.Range("A1").Value = "Test Value"
WKS.Range("A1").Select
WKS.Range("A1").Value = ""
makeMasterPage (WKS) '<--WHERE I GET ERROR
End Sub
The call to makeMasterPage is where I get my error. Here is that function:
Private Sub makeMasterPage(ByRef WKS As Excel.Worksheet)
...
End Sub

Related

Export Excel values to HTML using VB.Net

I have a Windows Forms with button click operation.
On button click the specified columns in excel should be exported to html.
I used "SaveAs" method. But not able to open the file format in any browser. Please help me how to export specific columns('B','D') to hmtl with cell color.
Dim xlApp As Excel.Application
Dim xlWB As Excel.WorkBook
Dim xlSH As Excel.WorkSheet
Dim str_File As String = "C:\Sample.xlsx"
xlApp = New Excel.Application
xlWB = xlApp.Workbooks.Open(str_File)
xlSH = xlWB.WorkSheets("merge")
xlSH.SaveAs("C:\Sample.html")
Just add the FileFormat paramater to the .SaveAs method and Bob's your uncle. I also closed and quit the Workbook and Application respectively. I added the GC code to the calling method to get rid of ghost Excel when debugging.
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
OpCode()
GC.Collect()
GC.WaitForPendingFinalizers()
End Sub
Private Sub OpCode()
Dim str_File As String = "C:\Users\xxx\Documents\Excel\Politics\2020 Election.xlsx"
Dim xlApp As New Excel.Application
Dim xlWB As Excel.Workbook = xlApp.Workbooks.Open(str_File)
Dim xlSH As Excel.Worksheet = CType(xlWB.Worksheets(1), Excel.Worksheet)
xlSH.SaveAs("C:\Users\xxx\Documents\Excel\Sample.html", Excel.XlFileFormat.xlHtml)
xlWB.Close()
xlApp.Quit()
End Sub

How to export the results of a MS Access query to a specific sheet of the excel

I am new to MS access and VBA and need help.
what I do is export the results of the MS access Query in the Excel sheet manually. I am trying to automate this. So what I am trying to do is to create a VBA code in the Module of MS access where once I run the code, it will run the said query , then save the query results in the specific sheet of the excel file. Also it will delete the existing results that is stored in the sheet and will paste the new data.
I tried writing this code but its not working -
Sub Test()
Dim xlApp As Excel.Application
Dim xlBook As Excel.Workbook
Dim xlQuery As String
Dim xlfile As String
strDate = Format(Date, "yymmdd")
Set xlApp = New Excel.Application
xlApp.Visible = True
'Open the Master Workbook Template
Set xlBook = xlApp.Workbooks.Open("C:\Testing\Template.xls", , False)
xlQuery = "qry_1"
xlfile = "C:\Testing\Template.xls"
DoCmd.OpenQuery "qry_1"
DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel8, xlQuery, xlfile, True, Worksheets("hello")
Set xlApp = Nothing
Set xlBook = Nothing
End Sub
All you need is:
Dim xlQuery As String
Dim xlfile As String
xlQuery = "qry_1"
xlfile = "C:\Testing\Template.xls"
DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel8, xlQuery, xlfile, True, "hello$"

Add new sheets to the exported excel using vba and do a lookup between two sheets using access VBA [duplicate]

I am running a few modules of code in access and am writing data into
Excel. When I write the first time, data gets written properly. But again
when I try, the new data is written on top of the old data. What should I do to
insert a new sheet?
My existing code is
Dim objexcel As Excel.Application
Dim wbexcel As Excel.Workbook
Dim wbExists As Boolean
Dim objSht As Excel.Worksheet
Dim objRange As Excel.Range
Set objexcel = CreateObject("excel.Application")
On Error GoTo Openwb
wbExists = False
Set wbexcel = objexcel.Workbooks.Open("C:\REPORT1.xls")
Set objSht = wbexcel.Worksheets("Sheet1")
objSht.Activate
wbExists = True
Openwb:
On Error GoTo 0
If Not wbExists Then
objexcel.Workbooks.Add
Set wbexcel = objexcel.ActiveWorkbook
Set objSht = wbexcel.Worksheets("Sheet1")
End If
I think that the following code should do what you want. It's very similar to yours, except it uses the return values from the .Add methods to get the objects you want.
Public Sub YourSub()
Dim objexcel As Excel.Application
Dim wbexcel As Excel.Workbook
Dim wbExists As Boolean
Set objexcel = CreateObject("excel.Application")
'This is a bad way of handling errors. We should'
'instead check for the file existing, having correct'
'permissions, and so on, and actually stop the process'
'if an unexpected error occurs.'
On Error GoTo Openwb
wbExists = False
Set wbexcel = objexcel.Workbooks.Open("C:\REPORT1.xls")
wbExists = True
Openwb:
On Error GoTo 0
If Not wbExists Then
Set wbexcel = objexcel.Workbooks.Add()
End If
CopyToWorkbook wbexcel
EndSub
Private Sub CopyToWorkbook(objWorkbook As Excel.Workbook)
Dim newWorksheet As Excel.Worksheet
set newWorksheet = objWorkbook.Worksheets.Add()
'Copy stuff to the worksheet here'
End Sub

Trying to copy an Excel sheet to another workbook from within Access

I'm trying to copy an excel worksheet to another workbook from within Access and keep getting the subscript out of range error. I've tried a few different things but can't seem to nail it. Any help would be appreciated. Using Access and Excel 2010 and my code is as follows:
Dim strTaxMonth As String
Dim strTaxYear As String
Dim strTabName As String
Dim objExcel As Excel.Application
Dim objWB As Workbook
Dim objWS As Worksheet
Dim strExcelFile0 As String
Dim strExcelFile1 As String
Dim strExcelFile2 As String
strTaxMonth = Forms!frm_PayrollTax_Report!ReportMonth
strTaxYear = Forms!frm_PayrollTax_Report!ReportYear
strTabName = strTaxMonth & strTaxYear & "_PTAX"
strExcelFile0 = "C:\File0.xlsm"
strExcelFile1 = "C:\File1.xlsx"
strExcelFile2 = "C:\File2.xlsm"
'Copy Worksheet to Yearly File
Set objExcel = New Excel.Application
objExcel.Visible = True
objExcel.DisplayAlerts = False
If Len(Dir(strExcelFile1)) > 0 Then Kill strExcelFile1
Set objWB = objExcel.Workbooks.Open(strExcelFile0)
objWB.Activate
Set objWS = objExcel.Sheets("PTAX")
objWS.Activate
objWS.Unprotect
objWS.Select
objWS.Name = strTabName
objWS.Range("A1:I16").Select
objWS.Range("A1:I16").Copy
objWS.Range("A1:I16").PasteSpecial xlPasteValues, xlPasteSpecialOperationNone
objWS.Range("B19:I28").Select
objWS.Range("B19:I28").Copy
objWS.Range("B19:I28").PasteSpecial xlPasteValues, xlPasteSpecialOperationNone
objWS.Protect
objWS.Select
objExcel.Workbooks.Open(strExcelFile0).Sheets(strTabName).Copy After:=objExcel.Workbooks.Open(strExcelFile2).Sheets("YTD PTAX")
objExcel.Workbooks(strExcelFile0).Activate
objExcel.ActiveWorkbook.SaveAs strExcelFile1
objExcel.ActiveWorkbook.Close False
objExcel.Quit
Set objExcel = Nothing
Set objWB = Nothing
Set objWS = Nothing
End Sub
Just use this piece of code
Sub test()
Dim xlapp as New Excel.Application
Dim xlwkb as Workbook
Dim xlsht as Worksheet
Dim xlwkb2 as Workbook
Dim xlsht2 as Worksheet
xlapp.DisplayAlerts=False
'First workbook and Sheet
Set xlwkb=xlapp.Workbooks.Open(strExcelFile0)
Set xlsht=xlwkb.Worksheets(1)
'Second workbook and Sheet
Set xlwkb2=xlapp.Workbooks.Open(strExcelFile0)
Set xlsht2=xlwkb.Worksheets(1)
xlsht.Range("A1:B16").Copy Destination:=xlsht2.Range("A1")
Set xlsht=Nothing
xlwkb.Close
Set xlwkb=Nothing
xlwkb2.Saveas "C:\File.xls"
Set xlsht2=Nothing
xlwkb2.Close
xlapp.DisplayAlerts=True
Set xlapp=Nothing
xlapp.Quit
End Sub

VBA conversion Office 2007 to Office 2010

I have a function in an Access 2007 database, which has been working fine until my PC was upgraded to Office 2010. The procedure is below and the offending line is that where 'originalFolder' is set:
Function ExportToSharePoint()
Dim oFs As New FileSystemObject
Dim originalFolder As Folder
Dim destinationPath As String
Dim ofile As file
Dim XLApp As Excel.Application
Dim xlwb As Excel.Workbook
Dim strFileName As String
Dim oFolder As String
oFolder = "//chs114file1/dovpasres/Public/Script/InfoCentre/Delays"
Set oFs = CreateObject("Scripting.FileSystemObject")
Set XLApp = New Excel.Application
Kill "K:\Public\Script\InfoCentre\Delays\*.xlk"
Set originalFolder = oFs.GetFolder(oFolder)
destinationPath = "https://companyname.sharepoint.com/PRR/Documents/"
For Each ofile In originalFolder.Files
strFileName = oFs.GetFileName(ofile)
Set xlwb = XLApp.Workbooks.Open(ofile)
xlwb.SaveAs (destinationPath + strFileName)
Next
xlwb.Close True
XLApp.Quit
Set xlwb = Nothing
Set XLApp = Nothing
End Function
The error I'm getting is:
Error 13: data type mismatch
I'm mystified as this is a string, as required?
The Microsoft documentation lists the GetFolder return type as being Folder, but I suspect your problem is being caused by mixing late binding (via CreateObject) with early binding (via the strongly-typed Folder variable).
Either import the reference to avoid the CreateObject call, or change the type to a variant, or perhaps object:
Dim originalFolder As variant