user3021640
user3021640

Reputation: 11

Applescript for inserting hyperlink into MSWord comment

I'm trying to add a hyperlink object inside a Word comment. To create a new comment in the active document I'm using this piece of script:

tell application "Microsoft Word" set tempString to "lorem ipsum" make new Word comment at selection with properties {comment text:tempString} end tell

but now I'm not able to get a reference to the new created comment for use it with the command "make new hyperlink object".

Thanks for any suggestions.

Riccardo

Upvotes: 0

Views: 649

Answers (2)

user3021640
user3021640

Reputation: 11

Finally I got a solution here:

https://discussions.apple.com/message/24628799#24628799

that allowed me to insert the hyperlink in reference with part of the comment text, with the following lines, if somebody in the future will search for the same:

tell application "Microsoft Word"

      set wc to make new Word comment at end of document 1 with properties {comment text:"some text"}
      set ct to comment text of wc
      set lastChar to last character of ct
      make new hyperlink object at end of document 1 with properties {hyperlink address:"http://www.example.com", text object:lastChar}

end tell

Upvotes: 0

user1379931
user1379931

Reputation:

I don't think you can work with the object returned by make new Word comment (at least not in this case), and you have to insert a unique, findable string then iterate through the comments:

tell application "Microsoft Word"
    -- insert a unique string
    set tempString to (ASCII character 127) 
    set theComments to the Word comments of the active document
    repeat with theComment in theComments
        if the content of the comment text of theComment = tempString then
            set theRange to the comment text of theComment
            -- you do not have to "set theHyperlink". "make new" is enough
            set theHyperlink to make new hyperlink object at theRange with properties {text range:theRange, hyperlink address:"http://www.google.com", text to display:"HERE", screen tip:"click to search Google"}
            insert text "You can search the web " at theRange
            exit repeat
        end if
    end repeat
end tell

(edited to insert some text before the Hyperlink. If you want to insert text after the hyperlink, you can also use 'insert text "the text" at end of theRange.).

So for adding text, it was enough to use "the obvious" after all.

[ For anyone else finding this Answer. The basic problem with working with Word ranges in Applescript is that every attempt to redefine a range in the Comments story results in a range that is in the main document story. OK, I may not have tried every possible method, but e.g., collapsing the range, moving the start of range and so on cause that problem. In the past, I have noticed that with other story ranges as well, but have not investigated as far as this.

Also, I suspect that the reason why you cannot set a range to the Word comment that you just created is because the properties of the Comment specify a range object of some kind that I think is a temporary object that may be destroyed immediately after creation. SO trying to reference the object that you just created just doesn't work.

This part of the Answer is modified...

Finally, the only other way I found to populate a Comment with "rich content" was to insert the content in a document at a known place, then copy its formatted text to the comment, e.g. if the "known place is the selection, you can set the content of theComment via

set the formatted text of the comment text of theComment to the formatted text of the text object of the selection

If you are using a version of Word that supports VBA as well as Applescript, I don't really see any technical reason why you shouldn't invoke VBA to do some of these trickier things, even if you need the main code to be Applescript. ]

Upvotes: 0

Related Questions