I am relatively new to VBA, and am at a loss as to why I cannot paste into Excel from Word. The following macro ends up pasting the value into Word, even though I think I'm activating the Excel document.
The macro is meant to be run from Word; values from various FormFields then need to be pasted into cells in an existing Excel file.
I searched for a similar issue, though what was returned seemed to be variations of what I am experiencing, and I could not modify those answers to this.
Any help would be appreciated.
Sub Transfer()
Dim WD As Object
Dim ED As Excel.Application
Dim EDS As Excel.Workbook
Set WD = ActiveDocument
Set ED = CreateObject("excel.application")
ED.Visible = True
Workbooks.Open FileName:= _
"C:\Users\Documents\AppealData.xlsx"
ActiveWorkbook.Activate
Set EDS = ActiveWorkbook
WD.FormFields("AppNum").Copy
EDS.Activate
EDS.Sheets("Sheet1").Range("A1").Select
Selection.Paste
End Sub
Selection.Paste
is saying to paste the clipboard to the selection in Word. I think you want something likeED.Selection.Paste
. (You should also be careful withWorkbooks.Open
, which should probably beED.Workbooks.Open
to ensure you are referring to the correct instance of Excel, and likewiseED.ActiveWorkbook
instead of justActiveWorkbook
.) A good rule of thumb is "when you have multiple Application objects, qualify everything!" – YowE3K