Hello,
I got this code from this forum to copy and paste a PDF into an Excel. Works perfectly on older versions of Windows so not sure why its not working on Windows 10. I am using Adobe Reader.
The code halts on this line:
Run time error 1004 – Microsoft Excel cannot paste the data
WKS.Paste
If I press F8 to continue, the data pastes correctly.
I have tried to change the focus using Appactivate for Excel and for the Adobe and neither worked. Also tried to Pastespecial and still receive the same results, halting on the paste line.
Here is where I got the original code:
Example Copy All Text in PDF
There are about 5 different modules with API calls, Clear Clipboard etc... I only included the part giving me the headache. Any help is appreciated
I got this code from this forum to copy and paste a PDF into an Excel. Works perfectly on older versions of Windows so not sure why its not working on Windows 10. I am using Adobe Reader.
The code halts on this line:
Run time error 1004 – Microsoft Excel cannot paste the data
WKS.Paste
If I press F8 to continue, the data pastes correctly.
I have tried to change the focus using Appactivate for Excel and for the Adobe and neither worked. Also tried to Pastespecial and still receive the same results, halting on the paste line.
Here is where I got the original code:
Example Copy All Text in PDF
There are about 5 different modules with API calls, Clear Clipboard etc... I only included the part giving me the headache. Any help is appreciated
VBA Code:
Public Sub CopyPDFText()
' Written: February 19, 2018
' Author: Leith Ross
Dim Cell As Range
Dim col As Long
Dim hwnd As Long
Dim retval As Long
Dim Wks As Worksheet
Const HWND_TOPMOST As Long = -1
Const HWND_TOP As Long = 0
Const SWP_NOSIZE As Long = &H1
Const SWP_NOMOVE As Long = &H2
Const SWP_SHOWWINDOW As Long = &H40
Const SW_HIDE = 0
Const SW_NORMAL = 1
Const SW_MAXIMIZE = 3
Const SW_MINIMIZE = 6
Const SW_RESTORE = 9
If CountClipboardFormats <> 0 Then Call ClearClipboard
Set Wks = ThisWorkbook.ActiveSheet
Set Cell = Wks.Cells.Find("*", Wks.Cells(1, Columns.Count), xlValues, xlWhole, xlByColumns, xlPrevious, False, False, False)
If Cell Is Nothing Then col = 1 Else col = Cell.Column
Set Cell = Wks.Cells(1, col + 1)
hwnd = FindWindow("AcrobatSDIWindow", vbNullString)
If hwnd = 0 Then
MsgBox "There are No Open PDF Files.", vbExclamation
Exit Sub
End If
'Set the window position to be on top of all other windows.
retval = SetWindowPos(hwnd, HWND_TOP, 0, 0, 0, 0, SWP_SHOWWINDOW Or SWP_NOMOVE Or SWP_NOSIZE)
'Display the PDF window.
retval = ShowWindow(hwnd, SW_RESTORE)
'Select All Text
Call CopyAll
'Wait unitl PDF has finished copying.
While CountClipboardFormats = 0: DoEvents: Wend
'Send the PDF window to the Task Bar
retval = ShowWindow(hwnd, SW_MINIMIZE)
Wks.Paste
Cell.Select
End Sub