Share via

Paste data in a variable in an excel file

BetT 21 Reputation points
2020-11-14T16:15:59.95+00:00

I have some data in a variable in an excel file and I want to copy it to a excel file. However I am getting an error:
Exception setting "Paste": Cannot convert the "System.Object[]" value of type "Object[]" to type "Object".

$f2 = "C:\Users\This PC\Documents\testbook2.xlsx"
$xl = New-Object -ComObject Excel.Application
$xl.Visible = $true
$w2 = $xl.Workbooks.Open($f2)
$ws2 = $w2.Worksheets.Item(1)
$ws2.Paste($c)
$w2.Save()
$w2.Close()
$xl.Quit()

where $c has some data inside it.

Windows for business | Windows Server | User experience | PowerShell
0 comments No comments

2 answers

Sort by: Most helpful
  1. Anonymous
    2020-11-16T07:22:02.787+00:00

    Hi,

    Is $c a range of cells? If $c is "some data" you can just assign the values to the cells.

    Best Regards,
    Ian

    ============================================

    If the Answer is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    Was this answer helpful?

    0 comments No comments

  2. Rich Matheisen 48,116 Reputation points
    2020-11-14T19:05:51.713+00:00

    What's in $c?

    The Excel paste takes two values: a destination, and a link. The destination is usually a range, and a paste takes what's been copied, not "some data", and "pastes" it into the range.
    excel.worksheet.paste

    Was this answer helpful?

    0 comments No comments

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.