Author Posts

September 22, 2016 at 8:17 pm

I have a Script that changes the hyplink in the text file but it's not working

$msword = New-Object -ComObject Word.Application
$msword.Visible = $true
$doc = $msword.Documents.Open("C:\Outlook Signature Test 2016\Master\Master.docx")
$links = $doc.Hyperlinks 
$links

Results from running the script

Application : Microsoft.Office.Interop.Word.ApplicationClass
Creator : 1297307460
Parent : Microsoft.Office.Interop.Word.DocumentClass
Name : https://www.microsoft.com/
AddressOld : https://www.microsoft.com/
Type : 0
Range : System.__ComObject
Shape :
SubAddressOld :
ExtraInfoRequired : False
Address : https://www.microsoft.com/
SubAddress :
EmailSubject :
ScreenTip :
TextToDisplay :
Target :
So I do this:

$msword = New-Object -ComObject Word.Application
$msword.Visible = $true
$doc = $msword.Documents.Open("C:\Outlook Signature Test 2016\Master\Master.docx")
$links = $doc.Hyperlinks 
$links | gm

TypeName: System.__ComObject#{0002099d-0000-0000-c000-000000000046}

Name MemberType Definition
—- ———- ———-
AddToFavorites Method void AddToFavorites ()
CreateNewDocument Method void CreateNewDocument (string, bool, bool)
Delete Method void Delete ()
Follow Method void Follow (Variant, Variant, Variant, Variant, Variant)
Address Property string Address () {get} {set}
AddressOld Property string AddressOld () {get}
Application Property Application Application () {get}
Creator Property int Creator () {get}
EmailSubject Property string EmailSubject () {get} {set}
ExtraInfoRequired Property bool ExtraInfoRequired () {get}
Name Property string Name () {get}
Parent Property IDispatch Parent () {get}
Range Property Range Range () {get}
ScreenTip Property string ScreenTip () {get} {set}
Shape Property Shape Shape () {get}
SubAddress Property string SubAddress () {get} {set}
SubAddressOld Property string SubAddressOld () {get}
Target Property string Target () {get} {set}
TextToDisplay Property string TextToDisplay () {get} {set}
Type Property MsoHyperlinkType Type () {get}

As you can see Address is there, but I keep getting an error:

Exception setting "Address": "The property 'Address' cannot be found on this object. Verify that the property exists and can be set."
At C:\Outlook Signature Test 2016\HLink.ps1:46 char:5
+ $links.Address = "https://www.leggmason.com/"}
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], SetValueInvocationException
+ FullyQualifiedErrorId : ExceptionWhenSetting

Please help me.

September 22, 2016 at 8:19 pm

If there's more than one link, then $links will contain multiple objects, and what you're trying won't work. It's like...

$cars = Get-Cars
$cars.Color = "Blue"

You can't change all the cars at once.

Try:

$links | Measure

And see what that says.

September 23, 2016 at 4:00 pm

I did try $links | measure and I got this:

Count : 1
Average :
Sum :
Maximum :
Minimum :
Property :

I was able to correct the problem by using the following:



$msword = New-Object -ComObject Word.Application
$doc = $msword.Documents.Open("C:\Outlook Signature Test 2016\Master\Master.docx")
#$range = $doc.Sections
$links = $doc.Hyperlinks 
$links | measure
$co = 456 

Switch ($co)
{

    840 {$results = "https://www.mva.microsoft.com/"}
    826 {$results = "https://www.Microsoft.com/"}
    123 {$results = "https://www.IBM.com/"}
    156 {$results = "https://www.Adobe.com/"}
    456 {$results = "https://www.Pluralsight.com/"}
    default {"I have no idea where you are"}
    
}
$results

Foreach($link in $links){
    $link.Address = $results}

$doc.Save()

The address link changes depending on the results.

September 25, 2016 at 1:22 am

I believe when working with com objects in many instances if a property can be a collection it will always be returned as a collection, even if only one object is returned. Hence the need to iterate through the collection to access properties of each item in the collection, where those properties don't exist on the collection itself. I've made similar observations when working with wmi instances. Even if I filter my wmi query well enough to return only one instance, you still have to use foreach if you want to make a change to an instance or perhaps access a method on an instance.

I don't have any related info or documentation to prove this, it's just been a working observation through my own experience and I was satisfied with my observation to the point that I didn't bother to investigate it any further.