Click here to Skip to main content
15,886,110 members
Please Sign up or sign in to vote.
2.50/5 (2 votes)
See more:
The following code snippet was taken from the Visual Studio 2010 (trial) walk through.
It is noted that:
1. The excel sheet created does not include all the values declared within [values]
2. Although the first time the application runs a file named SampleWorkBooks.xls
is created, the second time round, the application prompts me for a default location for Book.xlsx, which leads me to think this is some default behavior which
the application has not overridden.
VB
Imports Excel = Microsoft.Office.Interop.Excel
Module Module1
    Sub Main()
        Dim values = {4, 6, 18, 2, 1, 76, 0, 3, 11}
        CreateWorkbook(values, "C:\SampleFolder\SampleWorkbook.xls)
    End Sub
    Sub CreateWorkbook(ByVal values As Integer(), ByVal filePath As String)
        Dim excelApp As Excel.Application = Nothing
        Dim wkbk As Excel.Workbook
        Dim sheet As Excel.Worksheet
        Try
            Start Excel and create a workbook and worksheet.
            excelApp = New Excel.Application
            wkbk = excelApp.Workbooks.Add()
            sheet = CType(wkbk.Sheets.Add(), Excel.Worksheet)
            sheet.Name = "Sample Worksheet"
            ' Write a column of values.
            For i = 1 To values.Length
                sheet.Cells(Row,Column)
                sheet.Cells(i, 1) = values(i) 
               //question: shouldn't this loop iterate through the values declared
               //and write the list of values in row (i,1)?
            Next
            ' Suppress any alerts and save the file. Create the directory
            ' if it does not exist. Overwrite the file if it exists.
            excelApp.DisplayAlerts = False
            Dim folderPath = My.Computer.FileSystem.GetParentPath(filePath)
            If Not My.Computer.FileSystem.DirectoryExists(folderPath) Then
                My.Computer.FileSystem.CreateDirectory(folderPath)
            End If
            wkbk.SaveAs(filePath)
        Catch
        Finally
            sheet = Nothing
            wkbk = Nothing
            ' Close Excel.
            excelApp.Quit()
            excelApp = Nothing
        End Try
    End Sub
End Module


Article sourced from: http://msdn.microsoft.com/en-us/library/ee317478.aspx
Microsoft Visual Studio 2010
Version 10.0.30319.1 RTMRel
Microsoft .NET Framework
Version 4.0.30319 RTMRel
Installed Version: Professional
Microsoft Office Developer Tools   01018-169-2660007-70603
Microsoft Office Developer Tools
Microsoft Visual Basic 2010   01018-169-2660007-70603
Microsoft Visual Basic 2010
Microsoft Visual C# 2010   01018-169-2660007-70603
Microsoft Visual C# 2010
Microsoft Visual C++ 2010   01018-169-2660007-70603
Microsoft Visual C++ 2010
Microsoft Visual F# 2010   01018-169-2660007-70603
Microsoft Visual F# 2010
Microsoft Visual Studio 2010 Team Explorer   01018-169-2660007-70603
Microsoft Visual Studio 2010 Team Explorer
Microsoft Visual Web Developer 2010   01018-169-2660007-70603
Microsoft Visual Web Developer 2010
Crystal Reports Templates for Microsoft Visual Studio 2010
Crystal Reports Templates for Microsoft Visual Studio 2010
Microsoft Visual Studio 2010 SharePoint Developer Tools   10.0.30319
Microsoft Visual Studio 2010 SharePoint Developer Tools
Posted
Updated 20-Mar-11 6:07am
v2
Comments
OriginalGriff 20-Mar-11 12:10pm    
And is there any particular reason you are telling us this?
Did you have a question on it?
jon-80 20-Mar-11 12:22pm    
Yes, because I am thinking that it might be a bug.
Richard MacCutchan 20-Mar-11 16:28pm    
Well I think that you should tell Microsoft.
jon-80 20-Mar-11 17:58pm    
ok

1 solution

I see 3 reasons to bugs:
1)
Dim sheet As Excel.Worksheet

Never use variable named "sheet", becouse it is Excel name.
Excel have Worksheets and Sheets collections. Read more about differences on msdn website.
Dim oSheet As Excel.Worksheet


2)
VB
oSheet.Cells(Row,Column)

I have'nt see declarations for: "Row" and "Column"
You should always use Option Explicit keyword to enforce declaration of variables.

3)
VB
For i=1 To values.Length
    oSheet.Cells(i, 1) = values(i)
Next i

When you use arrays in For ... To ... loop, the default lower bound is 0 (zero), not 1. Use Option Base 1 keyword to enumerate arrays from 1.

And one advise:
Use Range("A" & i) object. This is better and quickest then: sSheet.Cells(1,i).
oRng = oSheet.Range("A" & i) 'direct access
oRng.Value = "Example 1"


To change column in iteration you can use Offset object. For example (using sheet object):
With oSheet
    .Range("A1").Offset(RowOffset:=0,ColumnOffset:=i)
    //other instructions
End With
 
Share this answer
 
v3
Comments
jon-80 27-Mar-11 2:04am    
Thanks

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900