T
TheVillagesBill
As part of an application I am trying to build an excel worksheet from
scratch using visual basic. When I try to set the column width I receive the
following error: "Exception from HRESULT: 0x800A03EC" on the first statement
that tries to set the columnwidth. This is very frustrating because I have
all the code working on the old Visual Basic 6, but can't get it going on
Visual Studio 2008 with Excel 2007. Note that changing the names of the
sheets just above the failing code works. This seems to be related to the
previous question that I posted today about getting the value of an existing
cell. I've extracted the code that fails and it is below: HELP
.............................................................................................
Imports Microsoft.Office.Interop.Excel
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Dim returnFile As String = "C:\MyPool\MyPoolData\Test"
Dim k, j As Integer
Dim schedDate As String
Dim schedTime As String
Dim xla As New Application
Dim xlb = xla.Workbooks.Add
xlb.Activate()
j = 0
For Each ws As Worksheet In xlb.Worksheets
j += 1
ws.Name = "Week" & Trim(j.ToString)
Next
For Each ws As Worksheet In xlb.Worksheets
For k = 0 To 1
'ws.Activate()
'ws.Select()
ws.Range(ws.Columns(1 + (5 * k))).ColumnWidth = 10 'Number
for selection
ws.Range(ws.Columns(2 + (5 * k))).ColumnWidth = 12 'Number
for selection
Next
Next
schedDate = Format(Date.Today, "yyyyMMdd")
schedTime = Format(TimeOfDay, "hhmm")
returnFile = returnFile & schedDate & schedTime
xlb.SaveAs(returnFile, FileFormat:=XlFileFormat.xlWorkbookNormal)
xlb.Close(SaveChanges:=False)
xla.Quit()
End Sub
End Class
scratch using visual basic. When I try to set the column width I receive the
following error: "Exception from HRESULT: 0x800A03EC" on the first statement
that tries to set the columnwidth. This is very frustrating because I have
all the code working on the old Visual Basic 6, but can't get it going on
Visual Studio 2008 with Excel 2007. Note that changing the names of the
sheets just above the failing code works. This seems to be related to the
previous question that I posted today about getting the value of an existing
cell. I've extracted the code that fails and it is below: HELP
.............................................................................................
Imports Microsoft.Office.Interop.Excel
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Dim returnFile As String = "C:\MyPool\MyPoolData\Test"
Dim k, j As Integer
Dim schedDate As String
Dim schedTime As String
Dim xla As New Application
Dim xlb = xla.Workbooks.Add
xlb.Activate()
j = 0
For Each ws As Worksheet In xlb.Worksheets
j += 1
ws.Name = "Week" & Trim(j.ToString)
Next
For Each ws As Worksheet In xlb.Worksheets
For k = 0 To 1
'ws.Activate()
'ws.Select()
ws.Range(ws.Columns(1 + (5 * k))).ColumnWidth = 10 'Number
for selection
ws.Range(ws.Columns(2 + (5 * k))).ColumnWidth = 12 'Number
for selection
Next
Next
schedDate = Format(Date.Today, "yyyyMMdd")
schedTime = Format(TimeOfDay, "hhmm")
returnFile = returnFile & schedDate & schedTime
xlb.SaveAs(returnFile, FileFormat:=XlFileFormat.xlWorkbookNormal)
xlb.Close(SaveChanges:=False)
xla.Quit()
End Sub
End Class