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
Related
System.Runtime.InteropServices.COMException: 'Retrieving the COM class
factory for component with CLSID
{00020819-0000-0000-C000-000000000046} failed due to the following
error: 80040154 Class not registered (Exception from HRESULT:
0x80040154 (REGDB_E_CLASSNOTREG)).'
I was just trying to export a Database using vb.net to an Excel file, I have tried to install and reinstall microsoft excel, adding and readding the imports of microsoft. can someone please helpppp
code:
Imports Excel = Microsoft.Office.Interop.Excel
Private Sub Button6_Click(sender As Object, e As EventArgs) Handles Button6.Click
Dim cnn As SqlConnection
Dim cnnst As String
Dim sql As String
Dim i, j As Integer
Dim xlapp As New Excel.Application
Dim xlworkbook As New Excel.Workbook
Dim xlworksheet As New Excel.Worksheet
Dim misvalue As Object = Reflection.Missing.Value
xlapp = New Excel.Application
xlworkbook = xlapp.Workbooks.Add(misvalue)
xlworksheet = xlworkbook.Sheets("sheet1")
cnnst = "datasource=localhost;port=3306;username=root;password=;database=myconnector"
cnn = New SqlConnection(cnnst)
cnn.Open()
sql = "select * from attendance"
Dim cmd As New SqlDataAdapter(sql, cnn)
Dim ds As New DataSet
cmd.Fill(ds)
For i = 0 To ds.Tables(0).Rows.Count - 1
For j = 0 To ds.Tables(0).Columns.Count - 1
xlworksheet.Cells(i + 1, j + 1) = ds.Tables(0).Rows(i).Item(j)
Next
Next
xlworksheet.SaveAs("D:\Report.xlsx")
xlworkbook.Close()
xlapp.Quit()
Myobject(xlapp)
Myobject(xlworkbook)
Myobject(xlworksheet)
cnn.Close()
MsgBox("success", "Exported", MessageBoxButtons.OK)
End Sub
CLSID {00020819-0000-0000-C000-000000000046} is the WorkBook Class.
Do not try to instantiate a Workbook by using the New keyword in the variable declaration statement.
Dim xlworkbook As New Excel.Workbook
should be
Dim xlworkbook As Excel.Workbook
The same also applies to the Worksheet variable declaration.
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
Through Access VBA i'm opening the workbook and make visible=False, and then i do something and closing. But it is still running in the Taskmanager.
Dim ExcelApp As New Excel.Application
Dim ExcelBook As New Excel.Workbook
Dim rng As Excel.Range
Dim rngDefine As Excel.Range
Set ExcelBook = ExcelApp.Workbooks.Open("C\temp\find.xlsm")
ExcelApp.Visible = False
'//Do something
ExcelApp.Quit
ExcelBook.Close SaveChanges:=False
Set ExcelBook = Nothing
Set ExcelApp = Nothing
The commenters above are correct, your new workbook isn't associated with your ExcelApp instance. So at the end you're closing your new ExcelApp instance and closing the new workbook you created, but I think the new workbook is opening in a new instance of Excel... maybe, so you'd still see an orphaned Excel process in the task manager. I'd change your code to something more like this:
Dim ExcelApp as Excel.Application
Dim ExcelBook as Excel.Workbook
Dim rng As Excel.Range
Dim rngDefine As Excel.Range
Set ExcelApp = New Excel.Application
Set ExcelBook = ExcelApp.Workbooks.Open("C\temp\find.xlsm")
'ExcelApp.Visible = False You could maybe comment this out unless you use ".Activate" somewhere. It should be the default anyway.
...
'At the end:
ExcelApp.DisplayAlerts = False
ExcelBook.Close
ExcelApp.DisplayAlerts = True
ExcelApp.Quit
'If right before "End Sub" the next two lines are of arguable necesity, but likely good practice
Set ExcelBook = Nothing
Set ExcelApp = Nothing
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
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