我正在尝试编辑Word文档中的嵌入式图表。我的源代码如下。它工作了很长时间,但最近两天都没有。我收到这个错误:
运行时错误'7':内存不足
我搜索了很多,但我不明白这个问题。当我关闭计算机并打开它后,它可以正常工作,但在我再次收到错误后。
这部分给出错误:
'create range with Cell
Set oChart = oInShapes.Chart
oChart.ChartData.Activate ' ***Note: It gives error here***
'Set oWorkbook = oChart.ChartData.Workbook
Set oWorksheet = oChart.ChartData.Workbook.Worksheets("Tabelle1")
Set oRange = oWorksheet.Range(Cell)
Public Sub updatechart(Doc As word.Application, ChartName As String, ChartTitle As String, Cell As String, data As String)`
Dim oInShapes As word.InlineShape
Dim oChart As word.Chart
Dim oWorksheet As Excel.Worksheet
'Dim oWorkbook As Excel.Workbook
Dim columnArray() As String
Dim rowArray() As String
Dim oRange As Range
Dim i As Integer
Dim j As Integer
For Each oInShapes In Doc.ActiveDocument.InlineShapes
' Check Shape type and Chart Title
If oInShapes.HasChart Then
'create range with Cell
Set oChart = oInShapes.Chart
oChart.ChartData.Activate ' ***Note: It gives error here***
'Set oWorkbook = oChart.ChartData.Workbook
Set oWorksheet = oChart.ChartData.Workbook.Worksheets("Tabelle1")
Set oRange = oWorksheet.Range(Cell)
' Commet for debug
'oWorksheet.Range("B33") = (ChartTitle & 33)
' Split text
columnArray = Split(data, SeperateChar)
For i = LBound(columnArray) To UBound(columnArray)
rowArray = Split(Trim(columnArray(i)), " ")
' Set Title. For example; ChartTitle = "XY" ----- Table Titles ----> | XY1 | XY2 | XY2 | ....
' After Set Value | 0,33| 0,1 | 0,46| ....
oRange.Cells(1, i + 1) = ChartTitle & (i + 1)
For j = LBound(rowArray) To UBound(rowArray)
' Set Values
oRange.Cells(j + 2, i + 1) = CDbl(rowArray(j))
Next j
Next i
'oWorkbook.Close
oChart.Refresh
End If
Next
Set oInShapes = Nothing
Set oChart = Nothing
Set oWorksheet = Nothing
'Set oWorkbook = Nothing
Erase rowArray, columnArray
End Sub
答案 0 :(得分:1)
之前发生在我身上。我有相同的解决方案,退出excel,释放一些内存并再试一次 - 它有效。您可能必须在使用此程序时关闭其他程序。它的字面意思是缺乏可用的记忆。
请记住,如果您运行其他将信息复制到剪贴板的宏,则可以释放更少的RAM来运行宏。
另外,您使用的是32位还是64位Excel - 64将允许您使用更多RAM。
答案 1 :(得分:1)
我注意到在清理你的sub时你没有将oRange设置为空,可能是这个对象使用了很多内存,当子结束时没有被释放?
答案 2 :(得分:1)
我有一个类似的错误,最后追溯到“For Each”声明。我认为它与您的示例中的Collection,Doc.ActiveDocument.InlineShapes的内存分配有关。
我的错误代码(PowerPoint到Excel):
For Each sh In InputBook.Sheets("Exec Sum").Shapes
sh.Visible = False
Next
Set sh = Nothing
我的固定代码:
For i = 1 To InputBook.Sheets("Exec Sum").Shapes.Count
InputBook.Sheets("Exec Sum").Shapes(i).Visible = False
Next
避免对集合的引用解决了我的问题。
答案 3 :(得分:0)
频繁访问工作表可能会产生资源使用问题。解决此问题的方法是在单个访问点中获取数据,例如
Dim V as Variant
V = InputRange
' Now V becomes a m x n array of the cell values in InputRange
' you may manipulate and work with this data and fill all your results in
' OutputV(m,n) variant array
Dim OutputV() as Variant
ReDim OutputV(m,n)
oRange = OutputV
通常会根据范围的大小将代码加速几百次,并且使用的资源也会少得多。