Word Automation Error when setting SetPlaceholderText

Welcome Forums General PowerShell Q&A Word Automation Error when setting SetPlaceholderText

This topic contains 1 reply, has 2 voices, and was last updated by

4 years, 8 months ago.

  • Author
  • #20845

    Topics: 1
    Replies: 0
    Points: 0
    Rank: Member

    I'm trying to convert the VBA code from the following articles into PowerShell. Everything seems to work fine apart from the setting of the placeholder text.



    Viewing the default placeholdertext is easy enough ;-

    Choose an item.

    This is my shortened code purely trying to set the placeholder text.

    $Word = New-Object -Com Word.Application
    $template = "template.docx"   #this can be any old word doc [saved in %TEMP% with a "Section4" bookmark defined]
    $Doc = $Word.Documents.Open($env:temp + "\" + $template)
    $objRange = $Doc.BookMarks.Item("Section4").Range
    $objCC = $objRange.ContentControls.Add(4) # 4 = Drop down list
    $text = "Select upgrade type..."

    Get Member reveals its an available method ;-

    $objCC |gm

    TypeName: System.__ComObject#{ee95afe3-3026-4172-b078-0e79dab5cc3d}

    Name MemberType Definition
    —- ———- ———-
    SetPlaceholderText Method void SetPlaceholderText (BuildingBlock, Ra...

    No matter how I try to set it fails with the following ;-

    Exception calling "SetPlaceholderText" with "3" argument(s): "Type mismatch.
    (Exception from HRESULT: 0x80020005 (DISP_E_TYPEMISMATCH))"
    At line:1 char:1
    + $objCC.SetPlaceholderText($null,$null,$text)
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : ComMethodTargetInvocation

    Can anyone throw any light on why this isnt working and how it can be done.


  • #20855

    Topics: 0
    Replies: 669
    Points: 0
    Rank: Member

    The method expects one out of 3 possible parameters each of which is a different type. It doesn't recognise $null so treats it as the first parameter and fails because it isn't of the correct type

    I couldn't get your code to actually add the control to work so modified it to this

    $Word = New-Object -Com Word.Application
    $word.visible = $true
    $template = "c:\test\template.docx"
    $Doc = $Word.Documents.Open($template)

    $cntrl = [Enum]::Parse([Microsoft.Office.Interop.Word.WdContentControlType], "wdContentControlDropdownList")

    $objCC = $doc.ContentControls.Add($cntrl)


    Still looking at the SetPlaceHoldertext method

The topic ‘Word Automation Error when setting SetPlaceholderText’ is closed to new replies.