locked
How to create shape, cut it, and paste as image in word document using power shell RRS feed

  • Question

  • Hi! I have a lot of .doc files with images insert as shape. Now i have problems with creating and cutting

    I try to create new document and fill it with some information. and stuck :(

    $word_app = New-Object -ComObject word.Application
    $word_app.Visible = $False
    $maindir = (Get-Item -Path ".\").FullName
    $doc = $word_app.documents.add()
    $select_doc = $word_app.Selection
    $add_obj = $select_doc.TypeText("This is the beg312312inning of my text line")
    $add_obj = $select_doc.Shapes.AddShape([ref] 1, 50, 50, 100, 200)
    $obj_cut=$select_doc.cut()
    $Table = $select_doc.Tables.Add($Word.Selection.Range, 1, 1)
    $doc_filename = "$($maindir)\doc.docx"
    $doc.SaveAs([ref] $doc_filename, [ref] 16)
    $doc.close()

    $word_app.Quit()

    After thar i try fing all shades 

    $select_doc.inlineshapes | ForEach-Object { ...

    how to CUT # here
    how to PASTESPESIAL #stuck here

    Wednesday, February 5, 2020 12:33 PM

All replies

  • Unfortunately you English makes it impossible to understand what you are asking.  I recommend that you ask Word questions in the Word developers forum.


    \_(ツ)_/

    Wednesday, February 5, 2020 12:41 PM
  • Im sorry.  I need to cut the figure and paste it as a picture.
    Have problems with "PasteSpecial"

    $doc.Shapes | ForEach-Object {
        $_.Select()
        $select_doc.cut()
        $select_doc.PasteSpecial($False, $False, 1, $False, 5, $False, $False)

    }

    This method or property is not available because the Clipboard is empty or not valid.
    At \addshape.ps1:18 char:2

    Wednesday, February 5, 2020 2:15 PM
  • Hi,

    for automation in word documents, it is quite useful to record your action as a macro in word while you do it by hand one time. Then open VBA editor (ALT+F11) where you can read the generated VBA code. VBA and PowerShell are quite similar. You just have to edit the code a little bit.

    Best regards

    Christoph

    Wednesday, February 5, 2020 2:19 PM
  • Thank you, its true)
    Sub Macro1()
        Selection.Cut
        Selection.PasteSpecial Link:=False, DataType:=wdPasteEnhancedMetafile, _
            Placement:=wdInLine, DisplayAsIcon:=False

    End Sub

     Code very similar. but i interested find solution in powershell. 
    This makes me very interested in this)

    what i do wrong?)

    Wednesday, February 5, 2020 2:39 PM
  • To begin with your code is not very close and does not really do what you think.  The following is how to write the code but it makes no sense and does not produce any results because you are not stating your pu8rpose clearly.

    $wd = New-Object -ComObject word.Application
    $wd.Visible = $true
    $doc = $wd.documents.add()
    
    $wd.Selection.TypeText('This is the beginning of my text line')
    $shape = $doc.Shapes.AddShape([ref] 1, 50, 50, 100, 200)
    $doc.Select()
    $wd.Selection.cut()  # remove all content from document
    $table = $doc.Tables.Add($wd.Selection.Range, 1, 1) # add a table to teh document
    $filename = "$pwd\doc.docx"
    $doc.SaveAs($filename)
    $doc.close()
    $wd.Quit()
    

    The Word object model is no really usable via VBA conversion.  You need to use C# examples and use the Interop API.  Post in the Word developer forum to get help with learning how to use teh Word object model with C#/PowerShell.


    \_(ツ)_/


    • Edited by jrv Wednesday, February 5, 2020 7:25 PM
    Wednesday, February 5, 2020 7:24 PM
  • This is how to insert a table and to insert from the clipboard into a table cell:

    $wd = New-Object -ComObject word.Application
    $wd.Visible = $true
    $doc = $wd.documents.add()
    
    # edit doc
    $wd.Selection.TypeText('This is the beg312312inning of my text line')
    $shape = $doc.Shapes.AddShape([ref] 1, 50, 50, 100, 200)
    $shape.Select()
    $wd.Selection.cut()  # remove all content from document
    $table = $doc.Tables.Add($wd.Selection.Range, 1, 1) # add a table to the document
    $table.Style = $doc.Styles['Grid Table 3 - Accent 1']
    
    #paste clipboard into cell
    $table.Rows[1].Cells[1].Select()
    $wd.Selection.Paste()
    
    # save file
    $filename = "$pwd\doc.docx"
    $doc.SaveAs($filename)
    $doc.close()
    $wd.Quit()
    


    \_(ツ)_/

    Wednesday, February 5, 2020 7:44 PM
  • Here is how to paste a shape as a picture.

    $wd = New-Object -ComObject word.Application
    $wd.Visible = $true
    $doc = $wd.documents.add()
    
    # edit doc
    $doc.Paragraphs[1].Range.Text = 'This is the beginning of my text line'
    
    # add shape
    $shape = $doc.Shapes.AddShape([ref] 1, 50, 50, 200, 100)
    $shape.TextFrame.TextRange.Text = 'hello world'
    $table = $doc.Tables.Add($wd.Selection.Range, 1, 1) # add a table to the document
    $table.Style = $doc.Styles['Grid Table 3 - Accent 1']
    
    #copy shape to clipboard and paste as picture into table cell
    $shape.Select()
    $wd.Selection.Copy()
    $wd.Selection.Cut() # remove original shape
    
    # paste copied shape as picture
    $table.Rows[1].Cells[1].Select()
    $wd.Selection.PasteAndFormat([Microsoft.Office.Interop.Word.WdRecoveryType]::wdChartPicture)
    
    # save file
    $filename = "$pwd\doc.docx"
    $doc.SaveAs($filename)
    $doc.close()
    $wd.Quit()

    As you can see working with the Office object model is not an easy thing and requires some considerable programming skill.


    \_(ツ)_/

    Wednesday, February 5, 2020 8:58 PM
  • Here is the image that was created in the docx file.  It is an embedded image that can be saved to an image file.


    \_(ツ)_/


    • Edited by jrv Wednesday, February 5, 2020 9:06 PM
    Wednesday, February 5, 2020 9:06 PM
  • There is also an alternate way to add a selection to the clipboard as a picture:

    $shape = $doc.Shapes.AddShape([ref] 1, 50, 50, 200, 100)
    $shape.TextFrame.TextRange.Text = 'hello world'
    $shape.Select()
    $doc.ActiveWindow.Selection.CopyAsPicture()


    \_(ツ)_/


    • Edited by jrv Wednesday, February 5, 2020 9:31 PM
    Wednesday, February 5, 2020 9:27 PM