Results 1 to 8 of 8

Thread: Automating an excel spreadsheet (OLE)

  1. #1

    Thread Starter
    Hyperactive Member Krass's Avatar
    Join Date
    Aug 2000
    Location
    Montreal
    Posts
    489

    Automating an excel spreadsheet (OLE)

    Hi fellow coders..

    I have followed si_the_geek's excellent tutorial on how to automate excel from vb. Works like a charm... BUT now (on a completely different project of mine) I need to append data to an excel spreadsheet that is INSIDE an ole on my form.

    One thread I've read was achieving that with the following code:
    Code:
    Dim objXL as Object
    
    Ole1.DoVerb (-1)
    Ole1.Action = 7
    
    Set objXL = oleTable.object.ActiveSheet
     
    For i = 0 to 3
     objXL.Cells(i, 1) = "Field " & i & ":"
    Next i
     
    Ole1.Action = 9
    
    Set objXL = Nothing
    I assumed that "Ole1" was the name of his OLE control and gave my control the same name. When executing, Ole1.DoVerb(-1) (which I have no idea what it is), triggers a "Object doesn't support this property or method" error message.

    I simply skipped over it and got a "Object required" message when executing the "Set objXL" line of code.

    I double-checked and "Microsoft Excel 10.0 Object Library" is in my project's references.

    Any idea what is missing here?

    Note1:

    Note2: This is an ole excel grid on a form that will always start empty, will never be printed or saved. The user will use my application to fill this spreadsheet and when he's all done, he'll manually copy its content and work with it on his own. This is mainly why I didn't want a new excel app/spreadsheet to be opened (like si_the_geek's methods) but rather use a OLE excel spreadsheet)

    Thank you.
    Chris

  2. #2
    Software Carpenter dee-u's Avatar
    Join Date
    Feb 2005
    Location
    Pinas
    Posts
    11,127

    Re: Automating an excel spreadsheet (OLE)

    This worked for me.
    Code:
    Private Sub Command1_Click()
        Dim i As Integer
    
        OLE1.DoVerb -1
        OLE1.Action = 7
    
        Set objWB = OLE1.object
        For i = 1 To 3
            OLE1.object.ActiveSheet.Cells(i, 1) = "Field " & i & ":"
        Next
    
        OLE1.Action = 9
    
        Set objXL = Nothing
    End Sub
    Regards,


    As a gesture of gratitude please consider rating helpful posts. c",)

    Some stuffs: Mouse Hotkey | Compress file using SQL Server! | WPF - Rounded Combobox | WPF - Notify Icon and Balloon | NetVerser - a WPF chatting system

  3. #3
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: Automating an excel spreadsheet (OLE)

    I haven't used the OLE control much, or for many years... but the line "Set objWB = OLE1.object" seems vaguely familiar, and I suspect that is the important part.

  4. #4

    Thread Starter
    Hyperactive Member Krass's Avatar
    Join Date
    Aug 2000
    Location
    Montreal
    Posts
    489

    Re: Automating an excel spreadsheet (OLE)

    Thanks guys. Post #2 solved it.

    I couldn't find on the net what the ".DoVerb" and ".Action" methods are used for. I guess I could just wipe em off the code. If you know what it's used for, I would appreciate.

    Also, you'll notice that when you use an excel spreadsheet in a OLE object, you will see the columns "A, B, C" appearing only when you enter (double-click) the OLE object. Would there be a way to make them visible all the time?

    Thank you

    Christian
    Chris

  5. #5
    Head Hunted anhn's Avatar
    Join Date
    Aug 2007
    Location
    Australia
    Posts
    3,669

    Re: Automating an excel spreadsheet (OLE)

    With code in post#2, I don't see any use of objWB and objXL.
    If that code works then you don't need 2 lines below
    Code:
        Set objWB = OLE1.object
    
        Set objXL = Nothing
    • Don't forget to use [CODE]your code here[/CODE] when posting code
    • If your question was answered please use Thread Tools to mark your thread [RESOLVED]
    • Don't forget to RATE helpful posts

    • Baby Steps a guided tour
    • IsDigits() and IsNumber() functions • Wichmann-Hill Random() function • >> and << functions for VB • CopyFileByChunk

  6. #6

    Thread Starter
    Hyperactive Member Krass's Avatar
    Join Date
    Aug 2000
    Location
    Montreal
    Posts
    489

    Re: Automating an excel spreadsheet (OLE)

    anhn: You were right. Those lines were of no use.

    Would anyone know what .DoVerb and .Action are used for?

    Also, I used si_the_geek code to clear the excel spreadsheet, here's the code:
    Code:
        LastRow = Ole1.Object.ActiveSheet.UsedRange.Rows.Count
        LastCol = Ole1.Object.ActiveSheet.UsedRange.Columns.Count
        
        For x = 1 To LastRow
            For y = 1 To LastCol
                Ole1.Object.ActiveSheet.Cells(x, y) = ""
            Next y
        Next x
    ...this is working fine. Just wondered if I'm missing a ".clear" method or something more effective. Meanwhile I'll stick with this.

    Recap:
    1- user uses my app to filter/modify/play with data.
    2- user outputs that data to OLE spreadsheet in my app
    3- user wants to take that data and bring it in his own excel spreadsheet

    To do #3, the user enter the OLE excel spreadsheet (by double-cliking it) and manually copy the data by clicking the upperleft cell. Could this be automated?

    Something like:
    Code:
    clipboard.settext = Ole1.Object.ActiveSheet.Range("A1:J25").Value
    (I would then work the A1:J25 part more professionally as this would be dynamic - I could use .UsedRange method)

    Edit:
    This is interesting. I just tried this line of code. What it does is OPEN an instance of excel with my OLE spreadsheet data in it. That's almost it. I'd like that data to go to the clipboard, not to a new instance of excel.
    Code:
    myclip.SetText Ole1.Object.ActiveSheet.Copy
    Thanks for any feedback - you guys rocks.
    Last edited by Krass; May 4th, 2010 at 11:04 AM.
    Chris

  7. #7
    PowerPoster jcis's Avatar
    Join Date
    Jan 2003
    Location
    Argentina
    Posts
    4,430

    Re: Automating an excel spreadsheet (OLE)

    OLE1.DoVerb Opens the Object for a given operation, in this case this operation is OLEShow (-1).
    OLE1.Action Sets a value that determines the Action to perform, in this case it's OLEActivate (7).
    So, your Code, using the proper OLE contants (native in VB) would be:
    Code:
      OLE1.DoVerb vbOLEShow
      OLE1.Action = acOLEActivate
    Now in VB, right click vbOLEShow and select Definition, you'll see the contant in the Object Browser with a description and all the other Contants you can use, some will be applicable and others won't, remember the OLE Control can contain many different things like Excel, Docs, MoviePLayers, etc..

    Do the same with acOLEActivate and you'll see a complete constant list too in the Object Browser.
    Last edited by jcis; May 4th, 2010 at 11:53 AM.

  8. #8
    PowerPoster jcis's Avatar
    Join Date
    Jan 2003
    Location
    Argentina
    Posts
    4,430

    Re: Automating an excel spreadsheet (OLE)

    This should be enough to copy to Clipboard:
    Code:
    OLE1.Object.ActiveSheet.UsedRange.Copy

Posting Permissions

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



Click Here to Expand Forum to Full Width