0

I have tried lots of options to paste information copied from other Excel workbook into my new workbook but not success do that (the range is huge - more them 3000 lines).

Please see sample of my script:

$objExcel = New-Object -ComObject Excel.Application
$objExcel.Visible = $false
$objExcel.displayAlerts = $false
$Src = [Environment]::GetFolderPath('Desktop')+'\New.xlsx'
$Files = [Environment]::GetFolderPath('Desktop')+'\Org.xlsx'
$wb1 = $objExcel.workbooks.open($Files)
$Worksheetwb1 = $wb1.WorkSheets.item('Org')
$Worksheetwb1.activate()
$Range = $Worksheetwb1.Range('A1:I1').EntireColumn
$Range.Copy() | Out-Null
$wb3 = $objExcel.workbooks.open($Src)
$Worksheetwb3 = $wb3.WorkSheets.item('Dest')
$Worksheetwb3.activate()
$Worksheetwb3.Columns.item('A:I').clear()
$Range3 = $Worksheetwb3.Range('A1:I1').EntireColumn
$Worksheetwb3.Paste($Range.Value2)
$wb3.close($true)
$wb1.close($true)
$objExcel.Quit()
2
  • Can you be more specific about exactly what is not working? Do you know how far it gets successfully? If you have a value in A1 instead of a multi-column range, is the script successful? I deal with PowerShell and Excel frequently but am not able to assist without more detailed information. Commented May 25, 2017 at 11:15
  • The Paste is not working for me "$Worksheetwb3.Paste($Range.Value2)" Commented May 25, 2017 at 11:43

3 Answers 3

1

You're pasting into a wrong range. Worksheet.Paste() has parameters of destination and link, your code uses destination only, which should be a Range belonging to that worksheet. Therefore, the proper line should be this:

$Worksheetwb3.Paste($Range3)
Sign up to request clarification or add additional context in comments.

Comments

0

Alternatively to Vesper's solution:

$Worksheetwb3.Range("A1").Paste() | Out-Null

# Paste special (as values)
$Worksheetwb3.Range("A1").PasteSpecial(-4163) | Out-Null

Comments

0

I have found the answer by changing the order of commands the Copy and then immediate after it the paste solved it for me.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.