运行不带Sendkeys的VBA代码

2024-06-26 13:29:21 发布

您现在位置:Python中文网/ 问答频道 /正文

想用vbi把PDF读入Excel。我已经可以使用sendkeys来实现这一点,但这不是一个可靠的解决方案,因为sendkeys通常都是易受攻击的。是否有其他方法或直接方法(在VBA或Python中)?在

使用发送密钥的代码:

Sub Get_Pdf()
    Dim XLName As String, PDFPath As String, READERPath As String
    Dim OpenPDF, sh As Worksheet

    XLName = ThisWorkbook.Name
    Set sh = ThisWorkbook.Sheets(1)
    PDFPath = Application.GetOpenFilename(filefilter:="PDF file (*.pdf), *.pdf")

    If UCase(PDFPath) = "FALSE" Then Exit Sub
    '~~> Below path differs depending Adobe version and installation path
    READERPath = "C:\Program Files\Adobe\Reader 9.0\Reader\AcroRd32.exe"
    Shell READERPath & " " & PDFPath, vbNormalFocus: DoEvents

    Application.Wait Now + TimeValue("00:00:2")

    SendKeys "^a", True
    Application.Wait Now + TimeValue("00:00:2")

    SendKeys "^c"
    Application.Wait Now + TimeValue("00:00:2")

    Windows(XLName).Activate
    sh.Paste sh.Range("A1")
    SendKeys "%{F4}", True
End Sub

我研究了一种使用“复制到剪贴板”的复制和粘贴方法。但是,没有代码或任何可用的东西。如果有人能在这方面提供帮助,我们将不胜感激。在

另外,我发现了一个不使用sendkeys的方法。它实际上定义了一个使用虚拟键盘的函数。但显然,该方法也不起作用,因为代码根本没有运行/给出输出。在

代码:

^{pr2}$

我们可以直接定义相同的其他函数并直接调用这些函数而不是senkeys。但没有产出。在

如果有人能帮上上述任何一种方法,那将是一个很大的帮助。在


Tags: 方法代码stringapplicationasshnowwait