2013-08-16 228 views
2

我試圖在Word文檔中編輯嵌入的圖表。我的源代碼如下。它工作了很長時間,但最近兩天沒有。我得到這個錯誤:運行時錯誤'7':內存不足

Run-time error '7': Out of memory

我搜查了很多,但我不明白這個問題。當我關機後打開它,然後它正常工作,但我得到錯誤後。

它給錯誤在這一部分:

 '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

您是否找到了解決這個問題的答案? –

+0

不幸的是沒有 – Goshawk

+0

@GrahamAnderson你有同樣的問題嗎? – Goshawk

回答

1

這種情況以前也發生在我身上。我有同樣的解決方案,退出excel,釋放一些內存,然後再試一次 - 並且工作。使用此功能時,您可能不得不關閉其他程序。它的字面意思就是它說的是缺乏可用內存。請記住,如果您已經運行了其他將信息複製到剪貼板的宏,那麼您將擁有更少的RAM以釋放運行宏。

另外,你使用32位或64位Excel - 64將允許你使用更多的RAM。

1

我注意到,清理你的子時沒有將oRange設置爲空,這是否可能是因爲這個對象使用了大量內存,而子內存不會被釋放?

0

我有一個類似的錯誤,並最終將其追溯到「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 

避免集合的引用解決了我的問題。

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 

通常加速通過取決於範圍的大小數百次的代碼並且還使用少得多的資源。