Skip to content

COM objects representing collections, such as provided by the Word.Application COM Automation Server, are inconsistently enumerated #24611

@mklement0

Description

@mklement0

Prerequisites

Steps to reproduce

Run the following on Windows, with Microsoft Word installed:

  • Creates a temp. sample document with 2 hyperlinks, which are then enumerated using various methods, only some of which work:
& {

  $word = New-Object -ComObject Word.Application

  # Create a sample document with 2 hyperlinks.
  $thisDoc = $word.Documents.Add() # Open($doc.FullName)
  $para = $thisDoc.Paragraphs.Add()
  $null = $thisDoc.HyperLinks.Add($para.Range, 'https://example.org') 
  $para.Range.InsertAfter("`n")
  $para = $thisDoc.Paragraphs.Add()
  $null = $thisDoc.HyperLinks.Add($para.Range, 'https://wikipedia.org') 

  # Try to enumerate the hyperlinks using various methods.
  [pscustomobject] @{
    # OK
    HyperLinks1 = $thisDoc.HyperLinks | ForEach-Object Address
    # OK
    HyperLinks2 = foreach ($lnk in $thisDoc.HyperLinks) { $lnk.Address }
    # OK(!) - explicit use of enumerator
    HyperLinks3 = $thisDoc.HyperLinks.GetEnumerator().Address
    # !! Broken - .ForEach() method (ditto for .ForEach('Address'))
    # !!  No enumeration -> single-element collection containing $null
    # !! Note: Fails fundamentally on WinPS.
    HyperLinks4 = $thisDoc.HyperLinks.ForEach({ $_.Address }) 
    # !! Broken - member-access enumeration:
    # !!  No enumeration -> $null
    HyperLinks5 = $thisDoc.HyperLinks.Address
  }

  $thisDoc.Close($false) # $false seemingly suppresses a save prompt. 

  $word.Quit()
  
}

Expected behavior

HyperLinks1 : {https://example.org/, https://wikipedia.org/}
HyperLinks2 : {https://example.org/, https://wikipedia.org/}
HyperLinks3 : {https://example.org/, https://wikipedia.org/}
HyperLinks4 : {https://example.org/, https://wikipedia.org/}
HyperLinks5 : {https://example.org/, https://wikipedia.org/}

That is, all enumeration methods should work.

Actual behavior

HyperLinks1 : {https://example.org/, https://wikipedia.org/}
HyperLinks2 : {https://example.org/, https://wikipedia.org/}
HyperLinks3 : {https://example.org/, https://wikipedia.org/}
HyperLinks4 : {$null}
HyperLinks5 :

That is, enumeration via the intrinsic .ForEach() method and via member-access enumeration did not work (except via an explicit .GetEnumerator() call); problem dates back to Windows PowerShell.

Error details

No response

Environment data

PowerShell 7.5.0-rc.1

Visuals

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    Needs-TriageThe issue is new and needs to be triaged by a work group.WG-Enginecore PowerShell engine, interpreter, and runtime

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions