Wednesday, May 30, 2007

Write formatted text in a spreadsheet using VB 6.0

Dim xlApp As Excel.Application
Dim wb As Workbook
Dim ws As Worksheet
Dim rowNum As Integer

Set xlApp = New Excel.Application
Set wb = xlApp.Workbooks.Add()
rowNum = 2
'Specify your worksheet name
Set ws = wb.Worksheets(1)

ws.Name = "BoxCheckReport"
ws.Range(Trim("A1")).Value = "Account No" 'First header row value
ws.Range(Trim("A1")).EntireRow.Font.Bold = True
ws.Range(Trim("A1")).EntireRow.Font.Color = vbBlue
ws.Cells.EntireColumn.ColumnWidth = 25
'ws.Columns.Column(1

ws.Range(Trim("A:A")).NumberFormat = "#####################"

'Set horizontal alignment to Left
xlApp.Range("A:A").HorizontalAlignment = 2
xlApp.Range("B:B").HorizontalAlignment = 2
xlApp.Range("C:C").HorizontalAlignment = 2
xlApp.Range("D:D").HorizontalAlignment = 2


ws.Range(Trim("B1")).Value = "Entry Date" 'header row value
ws.Range(Trim("C1")).Value = "Doc Type" 'header row value
ws.Range(Trim("D1")).Value = "Doc ID" 'header row value

ws.Range(Trim("A2").Value = "1010101010"
ws.Range(Trim("B2").Value = "01/01/02"
ws.Range(Trim("C2").Value = "NAP"
ws.Range(Trim("D2").Value = "001"

wb.SaveAs "c:\ExcelReport.csv"
If Not ws Is Nothing Then
Set ws = Nothing
End If
If Not wb Is Nothing Then
wb.Close
Set wb = Nothing
End If
If Not xlApp Is Nothing Then
xlApp.Quit
Set xlApp = Nothing
End If

'After writing text in the spreadsheet just open it for User to view
ShellExecute 0, vbNullString, "c:\ExcelReport.csv", vbNullString, vbNullString, vbNormalFocus
-----------
OTHER LINKS
-----------
http://www.codeproject.com/vbscript/AssetScan.asp?df=100&forumid=188977&exp=0&select=1268478
http://www.codeguru.com/vb/gen/vb_misc/tips/article.php/c8227/

3 comments:

Carly Fiorina said...
This comment has been removed by the author.
Carly Fiorina said...
This comment has been removed by the author.
Carly Fiorina said...

By using this XML spreadsheet format, you can create multi-sheet, formatted workbooks to Excel without open orders. Thanks for posting all the helpful information.

Website Spider Software