【问题标题】:Run-time error '7': Out of memory运行时错误“7”:内存不足
【发布时间】:2013-08-16 15:58:26
【问题描述】:

我正在尝试编辑 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

【问题讨论】:

  • 你找到这个问题的答案了吗?
  • @GrahamAnderson 你有同样的问题吗?
  • 由于 Excel 公式字符串太长(> 8,192 个字符),可能会发生此错误。

标签: excel vba charts ms-word runtime-error


【解决方案1】:

我以前也遇到过这种情况。我有相同的解决方案,退出 excel,释放一些内存并重试 - 它有效。使用此程序时,您可能必须关闭其他程序。它的字面意思是,缺乏可用内存。

请记住,如果您运行了将信息复制到剪贴板的其他宏,则释放的 RAM 将减少来运行宏。

此外,您使用的是 32 位还是 64 位 Excel - 64 位将允许您使用更多 RAM。

【讨论】:

    【解决方案2】:

    我注意到你在清理你的 sub 时没有将 oRange 设置为空,可能是这个对象正在使用大量内存而在 sub 结束时没有被释放?

    【讨论】:

      【解决方案3】:

      我遇到了类似的错误,最后将其追溯到“For Each”语句。我认为这与您的示例中的集合 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
      

      避免引用集合解决了我的问题。

      【讨论】:

        【解决方案4】:

        频繁访问工作表可能会产生资源使用问题。解决此问题的方法是在单个访问点中获取数据,例如

        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
        

        通常根据范围的大小将代码加速数百倍,并且使用的资源也少得多。

        【讨论】:

          猜你喜欢
          • 2016-07-10
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2020-08-07
          • 1970-01-01
          相关资源
          最近更新 更多