【问题标题】:Runtime error 1004 : paste method of worksheet class failed after Screenshot运行时错误 1004:截图后工作表类的粘贴方法失败
【发布时间】:2022-08-14 23:25:17
【问题描述】:

我正在尝试截取某些应用程序并使用应用激活发送密钥 1068,但是有时,我会收到 1004 错误Activesheet.Paste,

\"Worksheet 类的粘贴方法失败\"。

有时宏有效。我无法确定为什么会发生这种情况。

任何人都可以识别代码的问题吗?清除剪贴板Application.CutCopyMode = False有时有效,但并非总是如此。

Sub testscreenshotplusemail()

Dim r As Range
Dim outMail As Outlook.MailItem
Dim wordDoc As Word.Document
Dim count As Integer

AppActivate (\"TESTING\")

Application.CutCopyMode = False

Application.SendKeys \"(%{1068})\"

DoEvents

Application.Wait (Now + TimeValue(\"00:00:02\"))

Range(\"A5\").Select

ActiveSheet.Paste

For Each pic In ActiveSheet.Pictures
count = count + 1
Next pic

If count = 0 Then
Exit Sub
End If

Set r = Range(\"A12:F37\")
r.Copy

Dim outlookApp As Outlook.Application
Set outlookApp = CreateObject(\"Outlook.Application\")
Set outMail = outlookApp.CreateItem(olMailItem)

SelectionBehalf = Worksheets(\"Sheet1\").Range(\"F3\").Value
SelectionTO = Worksheets(\"Sheet1\").Range(\"G3\").Value
SelectionCC = Worksheets(\"Sheet1\").Range(\"H3\").Value
SelectionSubj = Worksheets(\"Sheet1\").Range(\"I3\").Value
SelectionBody = Worksheets(\"Sheet2\").Range(\"F2\").Value

With outMail
.SentOnBehalfOfName = SelectionBehalf
.Display
.To = SelectionTO
.CC = SelectionCC
.Subject = SelectionSubj
.Body = SelectionBody

Set wordDoc = outMail.GetInspector.WordEditor

wordDoc.Range(Start:=wordDoc.Range.End - 1).PasteAndFormat wdChartPicture


End With

For Each pic In ActiveSheet.Pictures
pic.Delete
Next pic
Application.SendKeys \"{NUMLOCK}\"
Application.CutCopyMode = False

End Sub
  • Sendkeys 不可靠,会导致很多歧义问题。因此,IMO,大多数情况下,您会在使用 Sendkeys 时遇到一些不稳定的行为。如果您确实需要将击键发送到应用程序,您最好使用keybd_event (reading)。但这更难编码。

标签: excel vba outlook screenshot office-automation


【解决方案1】:

使用SendKeys 是一种可靠的编程方式。使用 Windows API 函数而不是依赖键盘命令,因为其他软件或应用程序可能会阻止成功运行代码。请参阅How to do a screen capture using VBA 了解更多信息。

【讨论】:

    猜你喜欢
    • 2016-06-16
    • 1970-01-01
    • 2014-09-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-12-07
    • 1970-01-01
    相关资源
    最近更新 更多