Interface with Microsoft Excel
I have written an application that writes output to a text file. I would prefer to launch Excel and write the output directly to a worksheet where I can have much more flexibility with page formatting for printing.
How do I write the output (numeric arrays, constants and strings) to the cells of an excel worksheet and launch Excel.
Alternatively, I could leave the output as a text file if I could get Excel to import it directly into a worksheet template with the required formatting (that is without having to go through the wizard for opening a non-Excel formatted file).
Any suggestions?
Re: Interface with Microsoft Excel
Welcome to the Forums.
Moved from Classic VB forum. :)
Re: Interface with Microsoft Excel
You can get the basics of opening a workbook and writting to it from my code.
VB Code:
Option Explicit
'Add a reference to MS Excel xx.0 Object Library
Private moApp As Excel.Application
Private Sub Command1_Click()
Dim oWB As Excel.Workbook
Dim strName As String
moApp.Visible = True
Set oWB = moApp.Workbooks.Open("C:\Book1.xls")
oWB.Sheets(2).Activate
strName = "RobDog888"
oWB.Sheets(2).Cells(1, 1).Value = strName 'Write to cell A1
oWB.Close SaveChanges:=True
Set oWB = Nothing
End Sub
Private Sub Form_Load()
Set moApp = New Excel.Application
End Sub
Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)
If TypeName(moApp) <> "Nothing" Then
moApp.Quit
End If
Set moApp = Nothing
End Sub
Re: Interface with Microsoft Excel
Thanks robdog.
I'm running VB3.0 and Excel 97 and it looks like VB3 doesn't support some of your code. Am I'm mising something, is there another way or do I just need to get a more recent release!?
Re: Interface with Microsoft Excel
Ouch, vb3. Well the Excel 97 stuff should be compatible but what errors are you getting?