VBA Copy table from email to Excel - Cannot paste the data - 1004

Copper Contributor

Hi,

I am trying to copy a table from email and paste it into Excel with VBA for further manipulation.

I found code on the internet and all seems to work 100%, except the final part where I need to paste the table into the new worksheet.

 

I get the following error: "Runtime error 1004 - Microsoft Excel cannot paste the data".

If I switch to the open workbook I can manually paste using Ctrl + V and the it works perfectly.

 

Please help - I have spend so much time on this and is unable to find a solution.

 

 

9 Replies
@Yea_So
Thank you for the suggestion - I will definitely investigate further.
I am grabbing a table from the body of an email though and then sending it to Excel for further manipulation - not sure if Power Query is the best tool for this... disclaimer - I haven't worked with Power Query much before...

@Gertdj 

 

I only suggested it because your code mentioned pdf, as pdf can only be an attachment unless it is an embedded object.

 

Sub PDF_To_Excel()

 

Dim setting_sh As Worksheet
Set setting_sh = ThisWorkbook.Sheets("Setting")

Dim pdf_path As String
Dim excel_path As String

pdf_path = setting_sh.Range("E11").Value
excel_path = setting_sh.Range("E12").Value


Dim fso As New FileSystemObject
Dim fo As Folder
Dim f As File

Set fo = fso.GetFolder(pdf_path)

Dim wa As Object
Dim doc As Object
Dim wr As Object

Set wa = CreateObject("word.application")

wa.Visible = True

Dim nwb As Workbook
Dim nsh As Worksheet

For Each f In fo.Files
Set doc = wa.documents.Open(f.Path, False, Format:="PDF Files")
Set wr = doc.Paragraphs(1).Range
wr.WholeStory

Set nwb = Workbooks.Add
Set nsh = nwb.Sheets(1)
wr.Copy

nsh.Paste
nwb.SaveAs (excel_path & "\" & Replace(f.Name, ".pdf", ".xlsx"))

'Clipboard 123
doc.Close False
nwb.Close False

Next

wa.Quit

MsgBox "Done"

 

End Sub

@Yea_So
My humble apologies...
I managed to insert incorrect code from another project I am busy with.
Here is the correct Sub. It gives the error on the last line.

Sub GetTable()

Dim oLookInspector As Inspector
Dim oLookMailitem As MailItem

'Declare Word Variables.
Dim oLookWordDoc As Word.Document
Dim oLookWordTbl As Word.Table

'Declare Excel Variables.
Dim xlApp As Excel.Application
Dim xlBook As Excel.Workbook
Dim xlWrkSheet As Excel.Worksheet

'Get mail item.
Set oLookMailitem = Application.ActiveExplorer.CurrentFolder.Items("Test Table")

'Get Active Inspector.
Set oLookInspector = oLookMailitem.GetInspector

'Get Word Editor object
Set oLookWordDoc = oLookInspector.WordEditor

'Create new Excel App.
Set xlApp = New Excel.Application

'Make visible.
xlApp.Visible = True

'Add new workbook.
Set xlBook = xlApp.Workbooks.Add
'
''Add a new worksheet.
'Set xlWrkSheet = xlBook.Worksheets.Add
Set xlWrkSheet = xlBook.Sheets(1)

'Grab the Word Table.
Set oLookWordTbl = oLookWordDoc.Tables(1)

'Copy the table.
oLookWordTbl.Range.Copy

'Paste it to the sheet.
xlWrkSheet.Paste Destination:=xlWrkSheet.Range("A1")

End Sub

@Gertdj 

 

its a good thing I pointed it out, now the real vba affecionados can go at it. vba is not my forte

@Yea_So
Thank you for your input! Much appreciated.
Anyone with a solution or suggestion?
I have now even tried another external forum and still nothing...
Surely someone from Microsoft must be able to assist here?
Hi,
I am running this code on my corporate PC at work and is getting this error.
I have now moved it over to my personal PC and the code is working perfectly.
Could this be a corporate restriction or maybe some setting in Office?

@Gertdj 

 

I found this link, maybe it will be a stepping stone towards your solution go check it out:

import data from outlook email body into access (microsoft.com)

 

actual vba code:

https://stackoverflow.com/a/40958425/17186635