Paste/Insert Hyperlink in Word document?

I’m trying to figure out the correct syntax for Word 2008/2011 to insert a file:// hyperlink at the insertion point.

My two problems are:

  1. How to specify the insertion point (i.e. where the cursor is blinking when I execute the script).

  2. The correct syntax for the hyperlink.

For the first, I have been using code that “types” the clipboard into the document, which works well, but doesn’t generate a clickable link to another file (e.g. file://afp.example.com/Server/Info/Word_Training_Manual.doc). But, to use the hyperlink, I need to specify a range and haven’t figured out how to do that. The location can be arbitrary, so using something specifying end of document or such won’t work.

For the second point, I found the following code in a MacTech article by Ben Waldie from 2007(?)
http://www.mactech.com/articles/mactech/Vol.23/23.01/2301applescript/index.html:

tell application "Microsoft Word"
	tell active document
		set theRange to create range start 0 end 0
		make new hyperlink object at end with properties {text to display:"Automated Workflows, LLC", hyperlink address:"http://www.automatedworkflows.com", text object:theRange}
	end tell
end tell
--> hyperlink object "http://www.automatedworkflows.com" of active document of application "Microsoft Word"

The formatting of the code in the article is a bit strange, so I don’t know what should be the proper formatting of the line beginning with the “–>”.

A lot of examples, such as the one above, show how to insert text at the beginning of the document, but not at the current insertion point.

I’ve read through the Word 2004 Applescript guide, searched here & elsewhere, but am still not enlightened sufficiently to write code that works.

Does anyone have suggestions as to where I can look for sample code or have snippets that you can post?

Once I get something working, I’ll be glad to post it back here for reference purposes.

Thanks.

Cheers,
Jon

The MS Word AppleScript dictionary gives the definition for the insert command this way:

insert‚v : Insert the string at the specified location.
insert
text text : The string to be inserted
at location specifier : The place the new text should be inserted. It must specify a range within a document i.e. the fifth word of paragraph 1 of document 1.

So I would think you would say something like:

insert text_to_insert after (word five of paragraph 20 of document 1

Hope this helps

Hello, I searched for a considerable amount of time and found the following that works for me and sharing to those who may stumble across this:

tell application "Microsoft Word"
    		activate
     		set theRange to text object of selection
    		tell selection
    			make new hyperlink object at end with properties {text to display:"insert your text here", hyperlink address:"insert your URL here", text object:theRange}
    		end tell
    	end tell
1 Like

Hey Charlie,

Welcome to the forum!  :sunglasses:

Thanks for sharing – that works like a charm!

-Chris