2013-01-02 27 views
3

我使用下面的鏈接,可將Excel中的截圖並保存爲.gif文件保存GIF截圖:得到錯誤嘗試通過Excel的VBA宏

http://dmcritchie.mvps.org/excel/xl2gif.htm

當我嘗試運行宏,它給以下爲「containerbok.Activate」錯誤:

運行時錯誤「424」:所需的對象

可我知道爲什麼我收到這個錯誤?

我使用Excel 2010

謝謝!

+0

完全適合我(Excel 2003)。 –

+0

你可以試試Excel 2010嗎? – meetpd

+0

我想...但只有2003年可用:-( –

回答

5

事情實際上比您發佈的鏈接中的代碼簡單一些。只需選擇想要成像的單元格範圍,然後運行以下代碼。

Sub ExportSelection() 

    If TypeName(Selection) = "Range" Then 
     'Copy the area that you have selected as a picture. 
     Selection.CopyPicture Appearance:=xlScreen, Format:=xlBitmap 
     'Create a default bar(column) chart using the selected cells. 
     ActiveSheet.Shapes.AddChart.Select 
     ActiveChart.ChartType = xlColumnClustered 
     'Remove all the data from the chart, leaving a blank chart. 
     Dim i As Integer 
     For i = ActiveChart.SeriesCollection.Count To 1 Step -1 
      ActiveChart.SeriesCollection(i).Delete 
     Next 
     'Paste the image of the selected cells onto the chart. 
     ActiveChart.Paste 
     'Export the chart as a gif image. 
     ActiveChart.Export Environ("USERPROFILE") & "\Desktop\chart.gif" 
     'Delete the existing chart. 
     ActiveChart.Parent.Delete 
    End If 

End Sub 

的關鍵部分是ActiveChart.Export

這在Excel 2010中進行了測試和完美的作品。

+0

感謝您的回答。但作爲一個新手,我沒有'我不知道你寫的東西是如何影響那個鏈接中的代碼的,我必須刪除「containerbok.Activate」,我的意思是我應該編寫什麼代碼來避免這個錯誤?你可以發佈修改過的代碼嗎?我真的很感謝你的幫助 – meetpd

+0

請分享完整的代碼,工作..謝謝! – meetpd

+3

所有你需要的是Sub和End Sub來完成代碼Stewbob發佈的代碼和其他鏈接中沒有的東西我添加了Sub和End Sub,一個If語句,以確保選擇是一個範圍,並將導出移動到桌面(因爲我有一個權限錯誤,試圖保存到Win 7的根目錄)。 Stewb OB –