OLE: Inaccessible due to its protection level.

I am trying to plot a chart in excel sheet using VB.

So now I am following the instructions given here

1- I started a new VB project in VS2010, called Excelgraph.

2- By default I got Form1.vb[Design].

3- On this Form I created a button by dragging it from the toolbox.

4-I doubled clicked it and new Form1.vb opens.

5- I removed everything that was automatically generated in this file i,e Form1.vb file and pasted the following code:

Updated Code

This is another code and is a latest one, compatible with Visual Basic 6.0.

 Public Class Form1



  Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As 
  System.EventArgs) Handles Button1.Click

    Dim oXL As Object        ' Excel application
    Dim oBook As Object      ' Excel workbook
    Dim oSheet As Object     ' Excel Worksheet
    Dim oChart As Object     ' Excel Chart

    Dim iRow As Integer      ' Index variable for the current Row
    Dim iCol As Integer      ' Index variable for the current Row

    Const cNumCols = 10      ' Number of points in each Series
    Const cNumRows = 2       ' Number of Series


    ReDim aTemp(0 To cNumRows, 0 To cNumCols)

    'Start Excel and create a new workbook
    oXL = CreateObject("Excel.application")
    oBook = oXL.Workbooks.Add
    oSheet = oBook.Worksheets.Item(1)

    ' Insert Random data into Cells for the two Series:
    Randomize(Now().ToOADate())
    For iRow = 1 To cNumRows
        For iCol = 1 To cNumCols
            aTemp(iRow, iCol) = Int(Rnd() * 50) + 1
        Next iCol
    Next iRow
    oSheet.Range("A1").Resize(cNumRows, cNumCols).Value = aTemp

    'Add a chart object to the first worksheet
    oChart = oSheet.ChartObjects.Add(50, 40, 300, 200).Chart
    oChart.SetSourceData(Source:=oSheet.Range("A1").Resize(cNumRows, cNumCols))

    ' Make Excel Visible:
    oXL.Visible = True

    oXL.UserControl = True

    End Sub



End Class

Update

I updated the code as shown above.

Error

    'aTemp' is not declared. It may be inaccessible due to its protection level.    
     c:usersybf4 documentsvisual studio 2010ProjectsExcelgraph2
     Excelgraph2Form1.vb

There were two more errors which I managed to remove. How do I remove this error?

I am compiling the above code on visual studio 2010 and Office is Office 2007.


A simple, trivial program reveals the error to be as I suspected - you can't change the size of something that doesn't exist!

As Derek said, you need to change the ReDim to Dim - OR you need to declare it first.

FAILS:

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    Const cNumCols = 10      ' Number of points in each Series
    Const cNumRows = 2       ' Number of Series

    ReDim aTemp(0 To cNumRows, 0 To cNumCols)
End Sub

PASSES:

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    Const cNumCols = 10      ' Number of points in each Series
    Const cNumRows = 2       ' Number of Series
    Dim aTemp

    ReDim aTemp(0 To cNumRows, 0 To cNumCols)
End Sub

PASSES:

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    Const cNumCols = 10      ' Number of points in each Series
    Const cNumRows = 2       ' Number of Series

    Dim aTemp(0 To cNumRows, 0 To cNumCols)
End Sub

Hovering over aTemp should have told you this - it should also be underlined by a blue squiggly line to indicate a problem.


It's been a long time since I did this, but just looking at the code I suspect you need to change:

ReDim aTemp(0 To cNumRows, 0 To cNumCols) 

To:

Dim aTemp(0 To cNumRows, 0 To cNumCols)

ReDim is used to re-dimension an array after it has been dimensioned (using the Dim statement)


That's remarkably old code (it's for VB3, so 4 generations before the first VB.Net, and Excel 5)

However, I believe your code should run fine if you just comment out the two lines indicated.

The randomness of the entries added to the worksheet may be different, but since you'll be replacing that code anyway (where Rnd() is used), it shouldn't matter. (I'm assuming your purpose isn't to generate random graphs)

As to DoEvents , I'm not sure that was ever necessary.

链接地址: http://www.djcxy.com/p/65210.html

上一篇: 由于其保护级别,它可能无法访问

下一篇: OLE:由于其保护级别而无法访问。