gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 03:31:10 28 4
gpt4 key购买 nike

我正在尝试使用 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/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com