vb.net - OLE:由于其保护级别而无法访问。

标签 vb.net visual-studio-2010 ole

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

所以现在我按照 here 给出的说明进行操作

1- 我在 VS2010 中启动了一个新的 VB 项目,名为 Excelgraph。

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

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

4-我双击它,新的 Form1.vb 打开。

5-我删除了此文件中自动生成的所有内容,即 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。

最佳答案

一个简单、琐碎的程序揭示了错误,正如我所怀疑的那样 - 你无法更改不存在的东西的大小!

正如 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 上应该会告诉您这一点 - 它还应该用蓝色波浪线加下划线来指示问题。

关于vb.net - OLE:由于其保护级别而无法访问。,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12740703/

相关文章:

javascript - 无法通过 Javascript 更改表的innerHTML 来添加行

java - Java 中有 OLE 自动化吗?

c++ - 使用 OLE 删除 excel 文档中的冒号过滤器

Perl 的 Data::Dumper 显示对象而不是值

asp.net - 结合经典的 asp 和 asp.net

c# - 使用 C#/VB.Net 将数据集转换为 JSON

c# - 如何访问来自 ASP.net 中 GET 方法的 url 中的变量?

visual-studio-2010 - 仅在调试版本中阻止 NuGet 还原包

visual-studio-2010 - Visual Studio 2010安装项目:如何设置默认安装位置中使用的公司名称?

c++ - 如何使用 Visual Studio 将 OpenCV 图像查看窗口推送到 QT GUI?