URGENT Please help! create excel workbook & 3 spreadsheets

Posted on 2006-04-27
Last Modified: 2010-04-23
Hi Im stuck with generating some excel files in a directory folder using VB.NET

Please help me to:
2. Create an excel workbook (Im using Office 2003)
3. Create 3 named sheets in the workbook

Just looking for some code that works & dont care if its messy as long as it works

Thanks heaps!
Question by:craigdev
    LVL 44

    Accepted Solution

    Hello craigdev,

    used a button on a form for this
    Public Class Form1

        Function CreateNewFolder(ByVal Path, ByVal NewFolderName)

            Dim objFSO As Object
            objFSO = CreateObject("Scripting.FileSystemObject")
            Dim FullPath As String
            FullPath = Path & NewFolderName
            If Not objFSO.FolderExists(FullPath) Then
                CreateNewFolder = True
                CreateNewFolder = False
            End If
            objFSO = Nothing

        End Function

        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            Dim xlObj As Object, mysheet As Object
            Dim strSheet As String, sheetCount As Integer
            Dim strPath As String, strFolder As String

            strPath = "c:"
            strFolder = "myfolder"
            CreateNewFolder(strPath, strFolder)
            xlObj = CreateObject("Excel.Application")
            xlObj.visible = True
            ' now we first need a count of the default sheets on this pc
            sheetCount = xlObj.workbooks(1).sheets.count
            Select Case sheetCount
                Case 1
                    xlObj.workbooks(1).sheets(1).name = "MySheet1"
                    mysheet = xlObj.workbooks(1).sheets.add()
           = "MySheet2"
                    mysheet = xlObj.workbooks(1).sheets.add()
           = "MySheet3"
                Case 2
                    xlObj.workbooks(1).sheets(1).name = "MySheet1"
                    xlObj.workbooks(1).sheets(2).name = "MySheet2"
                    mysheet = xlObj.workbooks(1).sheets.add()
           = "MySheet3"
                Case 3
                    xlObj.workbooks(1).sheets(1).name = "MySheet1"
                    xlObj.workbooks(1).sheets(2).name = "MySheet2"
                    xlObj.workbooks(1).sheets(3).name = "MySheet3"
            End Select
        End Sub
    End Class

    hope this helps a bit
    LVL 1

    Author Comment

    Awesome!!! Thanks Buddy!
    LVL 44

    Expert Comment

    good luck! and thanks for the grade :)
    LVL 1

    Author Comment

    ... just another quick q,

    how to I save the workbook to the folder, in the example above, C:/myfolder?
    LVL 1

    Author Comment

    nah its fine, all finished, CHEERS

    Featured Post

    Top 6 Sources for Identifying Threat Actor TTPs

    Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

    Join & Write a Comment

    This article explains how to create and use a custom WaterMark textbox class.  The custom WaterMark textbox class allows you to set the WaterMark Background Color and WaterMark text at design time.   IMAGE OF WATERMARKS STEPS Create VB …
    The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (…
    To add imagery to an HTML email signature, you have two options available to you. You can either add a logo/image by embedding it directly into the signature or hosting it externally and linking to it. The vast majority of email clients display l…
    Sending a Secure fax is easy with eFax Corporate ( First, Just open a new email message.  In the To field, type your recipient's fax number You can even send a secure international fax — just include t…

    728 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    23 Experts available now in Live!

    Get 1:1 Help Now