Excel Automation Problem

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • goonsquad

    Excel Automation Problem

    I'm writing an Excel Automation app and I'm running to a problem early
    on with what should be a basic step.

    Dim oExcel As Object
    Dim oBook As Object
    Dim oSheet As Object
    Dim oRng As Excel.Range
    oExcel = CreateObject("E xcel.Applicatio n")
    oBook = oExcel.Workbook s.Add
    oSheet = oBook.Worksheet s(1)
    ....Populate Sheet...
    oRng(Selection, ActiveCell.Spec ialCells(xlLast Cell)).Select()

    My application will not compile due to the last line because
    Selection, ActiveCell and xlLastCell are not declared. I have added
    Excel 10.0 (2002) as a reference. What am I doing wrong?
  • Ken Tucker [MVP]

    #2
    Re: Excel Automation Problem

    Hi,

    I added a reference to the excel interop assembly. Here is an
    example on creating as worksheet. Try to avoid using latebinding.

    Dim oExcel As Microsoft.Offic e.Interop.Excel .Application

    Dim oBook, oBook1 As Microsoft.Offic e.Interop.Excel .Workbook

    Dim oSheet As Microsoft.Offic e.Interop.Excel .Worksheet

    'Start a new workbook in Excel.

    oExcel = New Microsoft.Offic e.Interop.Excel .Application

    oBook = oExcel.Workbook s.Add

    oBook1 = oExcel.Workbook s.Add

    'Add data to cells of the first worksheet in the new workbook.

    oSheet = CType(oBook.Wor ksheets(1),
    Microsoft.Offic e.Interop.Excel .Worksheet)

    oSheet.Range("A 1").Value = "Last Name"

    oSheet.Range("B 1").Value = "First Name"

    oSheet.Range("C 1").Value = "Price"

    oSheet.Range("A 1:B1").Font.Bol d = True

    oSheet.Range("A 2").Value = "Doe"

    oSheet.Range("B 2").Value = "John"

    oSheet.Range("C 2").Value = 12345.456

    oSheet.Range("C 2").Cells.Numbe rFormat = "$0.00"

    oSheet = CType(oBook.Wor ksheets(2),
    Microsoft.Offic e.Interop.Excel .Worksheet)

    oSheet.Range("A 1").Value = "Last Name"

    oSheet.Range("B 1").Value = "First Name"

    oSheet.Range("C 1").Value = "Price"

    oSheet.Range("A 1:B1").Font.Bol d = True

    oSheet.Range("A 2").Value = "Doe"

    oSheet.Range("B 2").Value = "John"

    oSheet.Range("C 2").Value = 12345.456

    oSheet.Range("C 2").Cells.Numbe rFormat = "$0.00"

    'Save the Workbook and quit Excel.

    oExcel.DisplayA lerts = False

    oBook.SaveAs("c :\Book1.xls")

    oSheet = Nothing

    oBook = Nothing

    oExcel.Quit()

    oExcel = Nothing

    GC.Collect()



    Ken
    ---------------------
    "goonsquad" <jburgoon@sakon .com> wrote in message
    news:e55b23eb.0 409130754.5e048 e5a@posting.goo gle.com...
    I'm writing an Excel Automation app and I'm running to a problem early
    on with what should be a basic step.

    Dim oExcel As Object
    Dim oBook As Object
    Dim oSheet As Object
    Dim oRng As Excel.Range
    oExcel = CreateObject("E xcel.Applicatio n")
    oBook = oExcel.Workbook s.Add
    oSheet = oBook.Worksheet s(1)
    ....Populate Sheet...
    oRng(Selection, ActiveCell.Spec ialCells(xlLast Cell)).Select()

    My application will not compile due to the last line because
    Selection, ActiveCell and xlLastCell are not declared. I have added
    Excel 10.0 (2002) as a reference. What am I doing wrong?


    Comment

    • Jorge

      #3
      Re: Excel Automation Problem

      Hi
      Try
      oSheet.oRng(Sel ection, ActiveCell.Spec ialCells
      (xlLastCell)).S elect()

      Kind Regards
      Jorge
      [color=blue]
      >-----Original Message-----
      >I'm writing an Excel Automation app and I'm running to a[/color]
      problem early[color=blue]
      >on with what should be a basic step.
      >
      >Dim oExcel As Object
      >Dim oBook As Object
      >Dim oSheet As Object
      >Dim oRng As Excel.Range
      >oExcel = CreateObject("E xcel.Applicatio n")
      >oBook = oExcel.Workbook s.Add
      >oSheet = oBook.Worksheet s(1)
      >....Populate Sheet...
      >oRng(Selection , ActiveCell.Spec ialCells[/color]
      (xlLastCell)).S elect()[color=blue]
      >
      >My application will not compile due to the last line[/color]
      because[color=blue]
      >Selection, ActiveCell and xlLastCell are not declared.[/color]
      I have added[color=blue]
      >Excel 10.0 (2002) as a reference. What am I doing wrong?
      >.
      >[/color]

      Comment

      • Herfried K. Wagner [MVP]

        #4
        Re: Excel Automation Problem

        * jburgoon@sakon. com (goonsquad) scripsit:[color=blue]
        > My application will not compile due to the last line because
        > Selection, ActiveCell and xlLastCell are not declared. I have added[/color]

        Make sure the enum containing 'xlLastCell' is imported.

        --
        M S Herfried K. Wagner
        M V P <URL:http://dotnet.mvps.org/>
        V B <URL:http://dotnet.mvps.org/dotnet/faqs/>

        Comment

        • Jeff Burgoon

          #5
          Re: Excel Automation Problem

          Thanks guys!

          Using the Excel Interop Assembly I was able to finally get it right.

          oRng = oSheet.Range(oE xcel.Selection,
          oExcel.Selectio n.End(Microsoft .Office.Interop .Excel.XlDirect ion.xlToRight))


          "Herfried K. Wagner [MVP]" <hirf-spam-me-here@gmx.at> wrote in message
          news:utkBaAcmEH A.1120@TK2MSFTN GP14.phx.gbl...[color=blue]
          > * jburgoon@sakon. com (goonsquad) scripsit:[color=green]
          > > My application will not compile due to the last line because
          > > Selection, ActiveCell and xlLastCell are not declared. I have added[/color]
          >
          > Make sure the enum containing 'xlLastCell' is imported.
          >
          > --
          > M S Herfried K. Wagner
          > M V P <URL:http://dotnet.mvps.org/>
          > V B <URL:http://dotnet.mvps.org/dotnet/faqs/>[/color]


          Comment

          Working...