Results 1 to 4 of 4

Thread: [RESOLVED] Limiting User's Interaction with Excel

  1. #1

    Thread Starter
    Junior Member
    Join Date
    May 2013
    Location
    Ireland
    Posts
    25

    Resolved [RESOLVED] Limiting User's Interaction with Excel

    I am writing a simple application which reads data from a GPS unit and stores the coordinates in a file. I am a novice when it comes to Excel automation and VB and am learning bit by bit how to do this! I have succeeded so far in writing a list of values into a column and after much "exception throwing" when an attempt is made to edit a cell, I set the interactive property of the Excel object to false to prevent any changes being made. This works fine but unfortunately this disables all interaction, so is it possible to prevent interaction with the cells of a worksheet but allow a user to scroll down through data (when this extends beyond the screen) ? I have tried to use the protect method and lock the cells in code, but this just causes Excel to complain about the sheet being protected and VB throws an exception.

  2. #2
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: Limiting User's Interaction with Excel

    lock the cells and protect the sheet? unprotect to change data and reprotect
    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

  3. #3

    Thread Starter
    Junior Member
    Join Date
    May 2013
    Location
    Ireland
    Posts
    25

    Re: Limiting User's Interaction with Excel

    Quote Originally Posted by westconn1 View Post
    lock the cells and protect the sheet? unprotect to change data and reprotect

    Yes that worked fine. Thanks! However Excel complains if a user tries to edit a cell on the sheet, and VB throws a "call was rejected by callee" exception. So this exception needs to be caught, or alternatively a displayalerts = false method can be used on the application object.

    Here is the code snippet:


    Dim oXL As Object
    Dim oWB As Object
    Dim oSheet As Object


    ' Opens an Excel file
    Private Sub ExcelOpen()

    'Start Excel and get Application object.
    oXL = CreateObject("Excel.Application")

    oXL.Visible = True

    ' Add a new workbook....
    oWB = oXL.Workbooks.add
    oSheet = oWB.ActiveSheet

    oXL.UserControl = True

    ' Don't display warnings in Excel
    oXL.DisplayAlerts = False

    End Sub

    ' Write to excel file
    Private Sub ExcelWrite(ByVal value_to_write As String, ByVal row As Integer, ByVal column As Integer)
    Static previous_row As Integer = 1


    Try
    oSheet.unprotect()
    oSheet.Cells(row, column).Value = value_to_write
    oSheet.columns.autofit()
    oSheet.protect()
    Catch ex As Exception
    MsgBox(ex.Message)
    End Try


    End Sub
    Last edited by Eugbug; May 17th, 2013 at 06:40 AM.

  4. #4
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: [RESOLVED] Limiting User's Interaction with Excel

    avoid the use of activesheet, as it is a new workbook, specify as
    Code:
    osheet = owb.sheets(1)
    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

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