运行时错误“7":内存不足 [英] Run-time error '7': Out of memory

查看:23
本文介绍了运行时错误“7":内存不足的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试编辑 Word 文档中的嵌入图表.我的源代码如下.它已经工作了很长时间,但不是最近两天.我收到此错误:

I'm trying to edit embedded charts in Word documents. My source code is below. It has worked a long time but not for the last two days. I get this error:

运行时错误7":内存不足

Run-time error '7': Out of memory

我搜索了很多,但我不明白问题所在.当我关闭计算机并打开它后,它可以正常工作,但再次出现错误.

I have searched a lot , but I don't understand the problem. When I shutdown computer and after open it, then it works correctly, but after I get error again.

它在这部分给出了错误:

It gives error in this part:

       '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

推荐答案

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

The frequent access to the worksheet can create problems with resource usage. The way to go about this is to fetch data in a single access point, like

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

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

Usually speeds up the code by several hundred times depending on the size of the range and also uses far less resources.

这篇关于运行时错误“7":内存不足的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆