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

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

问题描述

我有一个VBA代码,我使用它来复制范围作为图片,并将它们粘贴到图表。它这样做,所以我可以保存到一个图片。这个代码有70%的成功率,当它不工作,它给出错误范围类的CopyPicture方法失败。我不明白为什么它有时可以工作,有时不指定它采取相同的投入。



任何人都可以帮助吗?

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

设置tempWorkBook = Workbooks。 Open(workbookPath)

Dim selectRange As range
设置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工作表
设置tempSheet = Sheets.Add
tempSheet.range(A1)。PasteSpecial xlPasteAll

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

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

Dim oCht作为Excel。图
设置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


解决方案

我一直在努力处理同样的问题,而不是你,我认为与我们的VBA代码无关,缺乏编程能力。错误太随机了。



此外,如果在收到错误消息后,我点击DEBUG并按下 F8 ,继续逐步执行代码,然后我能够跳过错误。在有问题的行后按 F5 在正常执行模式下继续。



当然,上述不是解决方案,我的编码。



好吧,我这样做,它为我工作:



p>

  rgToPic.CopyPicture外观:= xlScreen,格式:= xlBitmap 

我添加了这个:

  rgToPic.Copy' b $ b  

我从来没有在 CopyPicture 方法。






在其他地方寻找这个问题,我发现一些用户能够跳过错误, CopyPicture 方法前的句子:

  application.CutCopyMode = false 


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

解决方案

I was struggling with the very same issue than you and I think is nothing to do with our VBA code or lack of programming skills. The error it's too random.

Moreover, if after getting the error message I clicked DEBUG and pressed F8 to continue executing the code step by step, then I was able to skip the error. After the problematic line I pressed F5 to continue in normal execute mode.

Of course, the above is not a solution but reveals nothing wrong with my coding.

Well, I did this and it worked for me:

before this sentence,

rgToPic.CopyPicture Appearance:=xlScreen, Format:=xlBitmap

I added this one:

rgToPic.Copy  'just for nothing

and I never have had the error in CopyPicture method again.


Looking for this issue in other places I found out some users were able to skip the error by introducing this sentence before the CopyPicture method:

    application.CutCopyMode=false

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

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