OLE:由于其保护级别无法访问。

时间:2012-10-05 06:28:27

标签: vb.net visual-studio-2010 ole

我正在尝试使用VB在Excel工作表中绘制图表。

所以现在我按照here

给出的指示

1-我在VS2010中开始了一个名为Excelgraph的新VB项目。

2-默认我得到了Form1.vb [Design]。

3-在此表单上,我通过从工具箱中拖动它来创建一个按钮。

4-I doubled点击它并打开新的Form1.vb。

5-我删除了在此文件i,e Form1.vb文件中自动生成的所有内容,并粘贴了以下代码:

更新代码

这是另一个代码,是最新的代码,与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

更新

我更新了上面显示的代码。

错误

    'aTemp' is not declared. It may be inaccessible due to its protection level.    
     c:\users\ybf4 \documents\visual studio 2010\Projects\Excelgraph2
     \Excelgraph2\Form1.vb

我设法删除了另外两个错误。如何删除此错误?

我正在Visual Studio 2010上编译上面的代码,Office正在Office 2007中。

3 个答案:

答案 0 :(得分:3)

一个简单,琐碎的程序揭示了我怀疑的错误 - 你无法改变不存在的东西的大小!

正如Derek所说,你需要将ReDim更改为Dim - 或者你需要先声明它。

<强>失败:

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

<强>关:

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

<强>关:

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

将鼠标悬停在aTemp上应该告诉你 - 它也应该用蓝色波浪线加下划线来表示问题。

答案 1 :(得分:1)

自从我这么做以来已经很长时间了,但只是查看我怀疑你需要改变的代码:

ReDim aTemp(0 To cNumRows, 0 To cNumCols) 

要:

Dim aTemp(0 To cNumRows, 0 To cNumCols)

ReDim用于在对数组进行尺寸标注后重新标注数组(使用Dim语句)

答案 2 :(得分:0)

这是非常古老的代码(适用于VB3,因此在第一个VB.Net和Excel 5之前的4代)

但是,我相信如果您只注释掉指定的两行,您的代码应运行正常。

添加到工作表中的条目的随机性可能不同,但由于您无论如何都将替换该代码(使用Rnd()),因此无关紧要。 (我假设你的目的不是生成随机图)

关于DoEvents,我不确定是否有必要。

相关问题