Results 1 to 5 of 5

Thread: [Excel,VB2010 Automation] Excel keywords not defined in VB2010

  1. #1
    New Member
    Join Date
    May 12
    Posts
    4

    [Excel,VB2010 Automation] Excel keywords not defined in VB2010

    Hi,

    Before I say anything further I have already added a reference to Microsoft Excel 11.0 (I am using Excel 2003) and have added the line Imports Microsoft.Office.Interop.Excel.

    I am using VB2010 to copy data from a csv file into a Excel workbook which contains a macro that performs a number of tasks. The output data is then copied into a new worksheet and saved. Excel is then closed.

    I am using VB2010 to copy the data from the first workbook into the workbook containing the macro. It appears when I use certain Excel keywords such as xlToLeft and Excel keywords which are within parenthesis I get an error saying the variable is not defined or may not be accessible due to its protection level. Why, if I have imported the Excel object library, are these keywords not being picked up by VB2010?

    Thanks,
    Tikk

    Code:
    Imports Microsoft.Office.Interop.Excel
    
    Public Class Form1
        Dim strMacroBook As String
        Dim xlapp As Object
        Dim xlworkbook1 As Excel.Workbook
        Dim xlSheet1 As Excel.Worksheet
        Dim strFileName As String
        Dim strName1, strName2 As String
    
    
    
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnTestData.Click
    
    
    
            If OpenFileDialog1.ShowDialog = DialogResult.OK Then
                strFileName = OpenFileDialog1.FileName
    
            End If
    
        End Sub
    
        Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnQuit.Click
            End
    
        End Sub
    
        Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAddTS.Click
            Try
                Dim MacroBook As String
                Dim First_Row As Long, Last_Row As Long
                Dim First_Col As Integer, Last_Col As Integer
    
    
                xlapp = CreateObject("Excel.Application")
                xlworkbook1 = xlapp.workbooks.open(strFileName)
                strName1 = xlapp.activesheet.name
    
    
                Dim First_Row, Last_Row As Long
                Dim First_Col, Last_Col As Integer
                Dim xlsheet1 = xlapp.ActiveBook.Worksheets(strName1)
                'Check there is at least one non-empty cell
                If WorksheetFunction.CountA(Cells) > 0 Then
                    'Determine actual used range of worksheet
                    First_Row = xlsheet1.Cells.Find(What:="*", After:=Cells(Rows.Count, Columns.Count), _
                        SearchDirection:=xlNext, SearchOrder:=xlByRows).Row
                    First_Col = xlsheet1.Cells.Find("*", After:=Cells(Rows.Count, Columns.Count), _
                        SearchDirection:=xlNext, SearchOrder:=xlByColumns).Column
                    Last_Row = xlsheet1.Cells.Find(What:="*", SearchDirection:=xlPrevious, SearchOrder:=xlByRows).Row
                    Last_Col = xlsheet1.Cells.Find(What:="*", SearchDirection:=xlPrevious, SearchOrder:=xlByColumns).Column
                End If
    
                xlapp.visible = True
                xlapp.activeworkbook.close(False)
                strMacroBook = "C:\TimeStamp_10Hz\MacroBook.xls"
                System.Diagnostics.Process.Start(strMacroBook)
                xlapp.visible = True
                MacroBook = xlapp.ActiveWorkbook.Name
                strName2 = xlapp.activesheet.name
                xlapp.sheets(strName2).Range("A1").PasteSpecial()
                xlapp.run("TimeStamp_10Hz")
                xlapp.workbooks(MacroBook).close(False)
                xlapp.quit()
                xlapp = Nothing
            Catch
                MsgBox("Please reload test data")
                xlapp.quit()
            End Try
    
        End Sub
    End Class

  2. #2
    Fanatic Member
    Join Date
    Oct 08
    Location
    Midwest Region, United States
    Posts
    1,022

    Re: [Excel,VB2010 Automation] Excel keywords not defined in VB2010

    Are you getting the error(s) in VB2010 or in the Excel macro?

    If in Excel, can you run it standalone (not thru VB) successfully?

  3. #3
    PowerPoster
    Join Date
    Dec 04
    Posts
    18,593

    Re: [Excel,VB2010 Automation] Excel keywords not defined in VB2010

    It appears when I use certain Excel keywords such as xlToLeft
    try xlapp.xltoleft
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  4. #4
    New Member
    Join Date
    May 12
    Posts
    4

    Re: [Excel,VB2010 Automation] Excel keywords not defined in VB2010

    Sorry for the delayed reply. I needed it to run off vb. I found that in order to use certain keywords such as xlprevious I needed to define them fully, e.g to use xlprevious I needed to say SearchDirection:=xlSearchDirection.xlPrevious.

  5. #5
    Super Moderator koolsid's Avatar
    Join Date
    Feb 05
    Location
    Mumbai, India
    Posts
    11,449

    Re: [Excel,VB2010 Automation] Excel keywords not defined in VB2010

    Yes, you have to define them fully or you can also use their constant values for example

    Code:
    SearchDirection:=xlSearchDirection.xlPrevious
    can also be written as

    Code:
    SearchDirection:=2
    Whenever in doubt, simply type it in the Immediate window in Excel and you will get the values

    ?xlNext
    1
    ?xlPrevious
    2
    If you are into automating Excel from vb.net then you might want to have a look at this link.

    TOPIC: VB.Net and Excel

    Link: http://siddharthrout.wordpress.com/vb-net-and-excel/

    HTH
    A good exercise for the Heart is to bend down and help another up...
    Please Mark your Thread "Resolved", if the query is solved

    Microsoft MVP: 2011 - Till Date IMP Links : Acceptable Use Policy, FAQ

    MyGear:
    Sony VGN-FZ27G with a triple boot between (XP+Office 2003+VB6), (VISTA+Office 2007+VS2008) and (Win7+Office 2010+VS2010) || Sony VPCCB-45FN with a Win7+Office 2010+VS2010. VM: (XP+Office 2003+VB6), (VISTA+Office 2007+VS2008), (Win8+Office 2010+VS2012) || Mac Book Pro (10.6.8) with Office 2011

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •