Results 1 to 3 of 3

Thread: Need Help writinig to a OLE inserted Excel Spreadsheet

  1. #1

    Thread Starter
    Member
    Join Date
    Oct 2007
    Posts
    43

    Need Help writinig to a OLE inserted Excel Spreadsheet

    I inserted an pre-formated spreadsheet into my VB 6 form, but I want to be able to write data to the spreadsheet for display and saving. How can I write data to my OLE inserted spreadsheet

    I tried ActiveWorkbook.Sheets.Cell(A2).Value but that doesn't work. Can someone help me.

    how do I call the sheet that I want to write to
    Sheets1.sheets.cell(a2)?

    I am not sure if I need to call Excel objects in to play. Need help.

  2. #2
    Discovering Life Siddharth Rout's Avatar
    Join Date
    Feb 2005
    Location
    Mumbai, India
    Posts
    12,001

    Re: Need Help writinig to a OLE inserted Excel Spreadsheet

    'Cells(2,1) represents A2 i.e Cells(Row,Column)
    Workbooks(1).Worksheets(1).Cells(2,1).Value

    See the Exmaple below... In this example, the Object property returns an object reference to the Worksheet object that represents the embedded Microsoft Excel worksheet. Once you have a reference to a Worksheet object, you can set and retrieve its properties and apply its methods through Automation.

    If you've embedded or linked a document created in a component that also supports Automation, such as Microsoft Excel, you can work with objects associated with that document much as you would if you were performing Automation. You can use the Object property to set and retrieve the properties of an object and apply its methods. The Object property returns an object reference to an Automation object that is related to the linked or embedded document.

    To illustrate this concept, create a Microsoft Excel spreadsheet with some data in the first worksheet and embed the .xls document in an unbound object frame named OLEUnbound0 on a form. Create a button named EditSpreadsheet on the form, set its OnClick event property to [Event Procedure], and enter the following code into the form module. When the user clicks on the EditSpreadsheet button, the Microsoft Excel worksheet is activated and the data in cells in the first row is formatted in bold.

    Code:
    Private Sub EditSpreadsheet_Click()
       Dim ctl As Control, wks As Excel.Worksheet
       Dim intI As Integer
       
       ' Return reference to control containing OLE object.
       Set ctl = Me!OLEUnbound0
       ' Activate OLE object.
       ctl.Action = acOLEActivate
       ' Return reference to embedded Worksheet object.
       Set wks = ctl.Object.Application.Workbooks(1).Worksheets(1)
       ' Initialize integer variable to 1.
       intI = 1
       ' Check whether there is data in cells in first row.
       Do Until IsEmpty(wks.Cells(1, intI).Value)
          ' Set font to bold.
          wks.Cells(1, intI).Font.Bold = True
          ' Increment integer variable.
          intI = intI + 1
       Loop
    End Sub
    A good exercise for the Heart is to bend down and help another up...
    Please Mark your Thread "Resolved", if the query is solved


    MyGear:
    ★ CPU ★ Ryzen 5 5800X
    ★ GPU ★ NVIDIA GeForce RTX 3080 TI Founder Edition
    ★ RAM ★ G. Skill Trident Z RGB 32GB 3600MHz
    ★ MB ★ ASUS TUF GAMING X570 (WI-FI) ATX Gaming
    ★ Storage ★ SSD SB-ROCKET-1TB + SEAGATE 2TB Barracuda IHD
    ★ Cooling ★ NOCTUA NH-D15 CHROMAX BLACK 140mm + 10 of Noctua NF-F12 PWM
    ★ PSU ★ ANTEC HCG-1000-EXTREME 1000 Watt 80 Plus Gold Fully Modular PSU
    ★ Case ★ LIAN LI PC-O11 DYNAMIC XL ROG (BLACK) (G99.O11DXL-X)
    ★ Monitor ★ LG Ultragear 27" 240Hz Gaming Monitor
    ★ Keyboard ★ TVS Electronics Gold Keyboard
    ★ Mouse ★ Logitech G502 Hero

  3. #3

    Thread Starter
    Member
    Join Date
    Oct 2007
    Posts
    43

    Re: Need Help writinig to a OLE inserted Excel Spreadsheet

    I tried to use the code above. I have a pre-created spreadsheet that contains only a column header in A1 and nothing else. When I try to insert data in A2 - K5 nothing is displayed in the Form embedded spreadsheet, but after I stop the run and I double click on spreadsheet an go in edit mode - i see the data that I wrote but it doesn't show on the embed spreadsheet when I am in run mode. Can you help me. All i see is blank rows after the column header. I did a test and tried to update to column header row and it worked but no other data will be displayed on the form embed spreadsheet at runtime. Can you help me with this? Also Set ctl = Me!OLEUnbound0 doesn't work I had to use ctrl = Me.OLE1 in VB6. Please help me to insert data on the spreadsheet inside the form. I tried to insert rows but it keeps giving me an error saying that I can't insert a new row with blank rows showing. I don't know how to get rid of the blank rows so that my data will appear inside the form spreadsheet at runtime.

    vb Code:
    1. Dim ctl As Control, wks As Excel.Worksheet
    2.    Dim intI As Integer
    3.    
    4.    ' Return reference to control containing OLE object.
    5.    Set ctl = Me.OLE1
    6.    ' Activate OLE object.
    7.    ctl.Action = acOLEActivate
    8.    ' Return reference to embedded Worksheet object.
    9.    Set wks = ctl.Object.Application.Workbooks(1).Worksheets(1)
    10.    ' Initialize integer variable to 1.
    11.    intI = 2
    12.    ' Check whether there is data in cells in first row.
    13.    'Do Until intI <= 9 'IsEmpty(wks.Cells(1, intI).Value)
    14.       ' Set font to bold.
    15.       'wks.Cells(1, intI).Font.Bold = True
    16.       'wks.Cells(1, intI).Font.Color = vbBlack
    17.       wks.Cells(2, 1).Value = 234
    18.       wks.Cells(3, 3).Value = "Somewhere"
    19.       wks.Cells(4, 5).Value = "Nowhere"
    20.       'wks.Range("A2:K2").Font.Color = vbRed
    21.       ' Increment integer variable.
    22.       intI = intI + 1
    23.    'Loop
    24. End Sub
    Last edited by jazlady; Nov 20th, 2008 at 04:07 PM.

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