Solved

Change a word bookmark by an hyperlink

Posted on 2012-04-03
2
1,130 Views
Last Modified: 2012-04-03
Hi, I am pretty new in powershell scripting so I am asking for your help.

I created a simple word file in path c:\tmp\test.rtf
Inside this file, I created a bookmark name link.

With this code I change the bookmark by my email address:

$MSWord = New-Object -com word.application
$objDoc = $MSWord.Documents.Open('c:\tmp\test.rtf')
$MSWord.Visible = $true
$email= "my@email.com"
($MSWord.ActiveDocument.Bookmarks.Item("link")).Select()
$MSWord.Selection.text = $email

With that code I can add an hyperlink to my test.rtf file:

$MSWord = New-Object -com Word.Application
$objDoc = $MSWord.Documents.Open('c:\tmp\test.rtf')
$MSWord.Visible = $true
$objRange = $objDoc.Range()
$email= "my@email.com"
$objLink = $objDoc.HyperLinks.Add($objRange, $email,$null,$null,$email)

Here what I am looking for:
I want to replace my bookmark by an hyperlink and I did not succeed in mixing those two script.

Your help would really be appreciated.
0
Comment
Question by:Digico
2 Comments
 
LVL 76

Accepted Solution

by:
GrahamSkan earned 500 total points
ID: 37803130
I don't know Powershell, but this is an attempt to translate from the way that it could be done in VBA
$MSWord = New-Object -com Word.Application
$objDoc = $MSWord.Documents.Open('c:\tmp\test.rtf')
$MSWord.Visible = $true
$objRange = $objDoc.Bookmarks.Item("link")).Range
$email= "my@email.com"
$objLink = $objDoc.HyperLinks.Add($objRange, $email,$null,$null,$email)

Open in new window

0
 
LVL 3

Author Closing Comment

by:Digico
ID: 37803217
It worked!

Only thing i needed to add was in line 6:
$objLink = $objDoc.HyperLinks.Add($objRange, “mailto:” + $email,$null,$null,$email)
That way the email link to my email address and not to a local file.

Thank you very much for your help!
0

Featured Post

Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Utilizing an array to gracefully append to a list of EmailAddresses
I thought I'd write this up for anyone who has a request to create an anonymous whistle-blower-type submission form created using SharePoint 2010 (this would probably work the same for 2013). It's not 100% fool-proof but it's as close as you can get…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

773 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question