locked
Word Range Copy and Paste not working on powershell 4.0 and below RRS feed

  • Question

  • Hi In Below code: $range2.Paste($copy) line is not working on PS4 and below versions. Any Suggestion.

    Error: Exception Type: System.Management.Automation.MethodInvocationException Exception Message: Exception calling "Paste" with "1" argument(s): "Object reference not set to an instance of an object."

    Minimal Code:

    $word = New-Object -ComObject word.application
      $word.visible = $false
      $word2 = New-Object -ComObject word.application
      $word2.visible = $false
      $doc = $word.documents.open($source)
      $doc2 = $word2.documents.add($template)
      $range = $doc.Range()
      $copy=""
      $copy = $range.Copy()
      $range2 = $doc2.Range()
      $range2.Paste($copy)
      $doc2.saveas([ref]$destination,[ref]$saveFormat::wdFormatDocument)
    Friday, May 5, 2017 6:38 AM

Answers

  • this will point you in the right direction:

    $start = $doc1.GoTo([Microsoft.Office.Interop.Word.WdGoToDirection]::wdGoToAbsolute, [type]::Missing, '1').Start
    $end = $doc1.GoTo([Microsoft.Office.Interop.Word.WdGoToDirection]::wdGoToAbsolute, [type]::Missing, '3').Start
    $doc1.Range().Copy()
    $doc2.Range().Paste()


    \_(ツ)_/

    • Marked as answer by pareekp Thursday, May 18, 2017 6:31 AM
    Monday, May 8, 2017 1:40 PM

All replies

  • Hi pareekp,

    from the description of the thread , it looks like your issue is related with Powershell Forum.

    but you had posted the issue on word for developer forum.

    on this forum we only handles the issues related with VBA Word Object model.

    so for better response and better solution for your issue , I move this thread to correct and best suitable powershell forum.

    The reason why we recommend posting appropriately is you will get the most qualified pool of respondents, and other partners who read the forums regularly can either share their knowledge or learn from your interaction with us.

    Thank you for your understanding.

    Regards

    Deepak


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, May 8, 2017 1:56 AM
  • I recommend using CopyTo to copy between documents.

    Do not create two word objects.  Open both documents with on objecy:

    $word = New-Object -ComObject word.application
    $word.visible = $true
    $doc = $word.documents.open($source)
    $doc2 = $word.documents.add($template)
    $range = $doc.Range()
    $copy = $range.Copy()
    $range2 = $doc2.Range()
    $range2.Paste($copy)
    $doc2.saveas([ref]$destination, [ref]$saveFormat::wdFormatDocument)
    


    \_(ツ)_/

    Monday, May 8, 2017 7:17 AM
  • Here is the best method for copying a complete document.

    $source = 'd:\test\file.docx'
    $template = 'd:\test\mytemplate.dotx'
    $word = New-Object -ComObject word.application
    $word.visible = $true
    $doc1 = $word.documents.open($source)
    $doc2 = $word.documents.add($template)
    
    $doc1.Range().Copy()
    $doc2.Range().Paste()
    
    $doc2.saveas([ref]$destination, [ref]$saveFormat::wdFormatDocument)
    


    \_(ツ)_/

    Monday, May 8, 2017 7:37 AM
  • That's Fine, I can use one word object but my question is still not answered. 

    Adding one more question:

    Q1 $doc2.Range().Paste() is not working on powershell 4.0 and below?

    Q2 Is it possible to apply another different template to existing document. e.g. doc1 is having 10 pages with template one and I want to add page 11 with different template.

    I am able to add page 11 using:
    $word.Selection.InsertNewPage()
    $word.Selection.InsertFile("new template file") but it's showing old template only.

    Any help please.

    Thanks,

    Pankaj

    Monday, May 8, 2017 10:13 AM
  • What versions of Office are you using?


    \_(ツ)_/

    Monday, May 8, 2017 10:54 AM
  • I just tested on WMF 3 and 4 and it works as expected.


    \_(ツ)_/

    Monday, May 8, 2017 10:59 AM
  • office 2013 and powershell 5.0
    Monday, May 8, 2017 11:02 AM
  • What about the ps 4 office system?

    I just tested on Office 2010 and later.  It works the same on all versions I have tested.


    \_(ツ)_/

    Monday, May 8, 2017 11:07 AM
  • Ok, let me check again, but someone answer my second query - 

    Q2 Is it possible to apply another different template to existing document. e.g. doc1 is having 10 pages with template one and I want to add page 11 with different template.

    I am able to add page 11 using:
    $word.Selection.InsertNewPage()
    $word.Selection.InsertFile("new template file") but it's showing old template only.

    Monday, May 8, 2017 11:46 AM
  • A template is just a container for styles and settings.  Templates are merged when loaded.  It is possible to create a page under a different template in a new document and copy it to an existing document but the merging of templates may change things.

    To learn how to use word and word templates post in the Word users forum.  Once you learn how to use templates you can then easily convert what you are doing to PowerShell.


    \_(ツ)_/

    Monday, May 8, 2017 11:51 AM
  • Ok, got it jrv.

    Check my below code:

    $doc.Range($word.ActiveDocument.ActiveWindow.Document.Content.Start,$word.ActiveDocument.Content.End).select() 

    It will select content from page 1 to Last page, but I want to select range of content e.g. page 3 to last page. Any Help?

    Monday, May 8, 2017 1:13 PM
  • To learn Word I recommend posting in the Word users forum or in the Word developers forum.

    We cannot teach you how to use products and this is not a good forum for getting consulting help for Word.

    Here are numerous documents to help you understand how to work with Word.

    https://www.google.com/?gws_rd=ssl#newwindow=1&q=learn+word+automation&spf=398


    \_(ツ)_/

    Monday, May 8, 2017 1:23 PM
  • this will point you in the right direction:

    $start = $doc1.GoTo([Microsoft.Office.Interop.Word.WdGoToDirection]::wdGoToAbsolute, [type]::Missing, '1').Start
    $end = $doc1.GoTo([Microsoft.Office.Interop.Word.WdGoToDirection]::wdGoToAbsolute, [type]::Missing, '3').Start
    $doc1.Range().Copy()
    $doc2.Range().Paste()


    \_(ツ)_/

    • Marked as answer by pareekp Thursday, May 18, 2017 6:31 AM
    Monday, May 8, 2017 1:40 PM