有时候,范围类的CopyPicture方法失败 [英] CopyPicture method of range class failed - sometimes

查看:803
本文介绍了有时候,范围类的CopyPicture方法失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个VBA代码,我用来将范围复制为图片并将其粘贴到图表中。这样做,所以我可以把它保存在一张照片中。该代码的成功率为70%,当它不起作用时,会发出错误范围类的CopyPicture方法失败。我不明白为什么它有时可以工作,有时候不会给出相同的输入。



任何人都可以帮助?

  Public Sub ExportRange(workbookPath As String,sheetName As String,rangeString As String,savepath As String)

设置tempWorkBook =工作簿。打开(workbookPath)

Dim selectRange作为范围
设置selectRange = Worksheets(sheetName).range(rangeString)
Dim numRows As Long
numRows = selectRange.Rows。计数
Dim numCols As Long
numCols = selectRange.Columns.Count

'将选择转移到新工作表并自动填充列
selectRange.Copy
Dim tempSheet As Worksheet
设置tempSheet = Sheets.Add
tempSheet.range(A1)。PasteSpecial xlPasteAll

ActiveSheet.UsedRange.Columns.AutoFit
设置selectRange = ActiveSheet.UsedRange
selectRange.Select
selectRange.CopyPicture xlScre en,xlPicture

Dim tempSheet2 As Worksheet
设置tempSheet2 = Sheets.Add
Dim oChtobj作为Excel.ChartObject
设置oChtobj = tempSheet2.ChartObjects.Add(_
selectRange.Left,selectRange.Top,selectRange.Width,selectRange.Height)

Dim oCht As Excel.Chart
设置oCht = oChtobj.Chart
oCht.Paste
oCht.Export filename:= savepath
oChtobj.Delete

Application.DisplayAlerts = False
tempSheet.Delete
tempSheet2.Delete
tempWorkBook .Close
Application.DisplayAlerts = True

End Sub


解决方案

对于我来说,我有类似的问题,我可以通过在 xlScreen xlPrinter selectRange.CopyPicture



我希望这有助于


I have a VBA code which I am using to copy ranges as a picture and paste them into a chart. It does this so I can save it into a picture. This code has like a 70% success rate, and when it doesn't work, it gives out the error "CopyPicture method of range class failed". I don't understand why it can sometimes work and sometimes doesn't given that it is taking the same inputs.

Can anyone help?

Public Sub ExportRange(workbookPath As String, sheetName As String, rangeString As String, savepath As String)

    Set tempWorkBook = Workbooks.Open(workbookPath)

    Dim selectRange As range
    Set selectRange = Worksheets(sheetName).range(rangeString)
    Dim numRows As Long
    numRows = selectRange.Rows.Count
    Dim numCols As Long
    numCols = selectRange.Columns.Count

    ' Transfer selection to a new sheet and autofit the columns
    selectRange.Copy
    Dim tempSheet As Worksheet
    Set tempSheet = Sheets.Add
    tempSheet.range("A1").PasteSpecial xlPasteAll

    ActiveSheet.UsedRange.Columns.AutoFit
    Set selectRange = ActiveSheet.UsedRange
    selectRange.Select
    selectRange.CopyPicture xlScreen, xlPicture

    Dim tempSheet2 As Worksheet
    Set tempSheet2 = Sheets.Add
    Dim oChtobj As Excel.ChartObject
    Set oChtobj = tempSheet2.ChartObjects.Add( _
        selectRange.Left, selectRange.Top, selectRange.Width, selectRange.Height)

    Dim oCht As Excel.Chart
    Set oCht = oChtobj.Chart
    oCht.Paste
    oCht.Export filename:=savepath
    oChtobj.Delete

    Application.DisplayAlerts = False
    tempSheet.Delete
    tempSheet2.Delete
    tempWorkBook.Close
    Application.DisplayAlerts = True

End Sub

解决方案

For me I had similar problem and I could solve it by changing between xlScreen and xlPrinter in selectRange.CopyPicture

I hope this helps

这篇关于有时候,范围类的CopyPicture方法失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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