ExcelVBA 將Range內容拷貝成圖表,轉成圖片

  • 5132
  • 0
  • 2016-03-03

如果遇到要將Excel的特定Range內文字做截圖方式轉成圖片,可以使用以下程式碼範例

    ''' Set Range you want to export to file
        Dim rgExp As Range: Set rgExp = Range("A1:F54")
        ''' Copy range as picture onto Clipboard
        rgExp.CopyPicture Appearance:=xlScreen, Format:=xlPrinter
        ''' Create an empty chart with exact size of range copied
        With ActiveSheet.ChartObjects.Add(Left:=rgExp.Left, Top:=rgExp.Top, _
        Width:=rgExp.Width, Height:=rgExp.Height)
        .Name = "myChart"
        .Activate
        End With
        ''' Paste into chart area, export to file, delete chart.
        ActiveChart.Paste
        ActiveSheet.ChartObjects("myChart").Chart.Export ThisWorkbook.Path & "\testmeExportChart.jpg"
        ActiveSheet.ChartObjects("myChart").Delete

資料出處:
http://stackoverflow.com/questions/10722851/exporting-excel-range-as-image-vb-net
http://software-solutions-online.com/word-vba-save-table-as-jpeg/

如果想將Word表格,另存圖片。可以將資料先放到Excel再依照前面介紹的方式來作業即可。
如果要將Word的圖片匯出,可以考慮另存HTML,直接取匯出的圖片即可。

另外,複製Range內容時,如發現為空白,導致匯出資料為空圖片時,請檢查是否有以下程式碼
Application.ScreenUpdating = False
註解該行程式碼,即可避免前述問題。

資料出處:
http://stackoverflow.com/questions/26512652/copy-paste-image-to-chart-improperly-pastes-a-blank-image