Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Auto-updatable links

Is there a way to apply "auto-updatable" style for hyperlink?

I believe, this question is not trivial.

When you normally click on hyperlink, it will change it's color to violet. Next, if you save, close, and then reopen the document, the link will be updated back to blue. This is default behaviour of Word, and there is no need to use any macros for it.

I'm trying to replicate this behaviour with VBA. Here is the code:

Sub Test1()
    Selection.Range.Hyperlinks(1).Range.Fields(1).Result.Style = Word.WdBuiltinStyle.wdStyleHyperlinkFollowed
End Sub

To make it work, simply put caret into the link, run macro, and see the results:

enter image description here

This works fine, except such visited links will not be auto-updated after you save, close, and then reopen the document. See the difference in the picture below. The link "Google" was opened normally, using the mouse Ctrl-click; the link "StackOverflow" was opened using the macro:

enter image description here

As I already said, I want to make my VBA-opened links (StackOverflow) auto-updatable as well (as Google).

Yes, I understand, there is a workaround - simply create another macro, which will be started every time the document opened and change all violet hyperlinks back to blue. However, this is just workaround, and I don't like it. Using it, we use conversion from "permanent violet" to "permanent blue", instead of using "temporary violet" (that's mean, auto-updatable without any additional efforts).

Hope everything is clear. Thanks in advance.

Update (was added after several answers were already posted).

Yes, I understand, this will work:

Sub Test1()
    On Error Resume Next 'To avoid an error in case if the link isn't reachable
    Selection.Hyperlinks(1).Follow
End Sub

But I want just simulate following, without really opening the link in the browser. That's why, I can't use Selection.Hyperlinks(1).Follow.

like image 406
john c. j. Avatar asked Aug 29 '17 21:08

john c. j.


1 Answers

The "temporary" color of a followed hyperlink is an embedded (and not directly accessible) feature of the built-in Hyperlink character style. It is not exposed through the normal UI's Style tools, nor through the object model.

You can readily compare all formatting between two selections using the Reveal Formatting pane (Shift+F1) in the document window in Word.

If you compare a normally followed hyperlink with a hyperlink affected by your snippet, you'll see that the followed hyperlink still has the Hyperlink style, while your simulated follow has changed the style of the second hyperlink.

If you compare a never-followed hyperlink and a normally followed hyperink, Word identifies their formatting as exactly the same. Word does not acknowledge that any aspect of formatting (style, font color, etc.) has changed.

It seems likely that the Word.WdBuiltinStyle.wdStyleHyperlinkFollowed you are using exists explicitly to address this gap (which is somewhat disappointing).

I recommend using your existing approach, and then reverting the style in a procedure triggered by the Before Save and Before Close events of the document. Using those events will prevent the followed style from saving at all, and so avoid issues caused by someone opening the document without enabling macros.

like image 153
AjimOthy Avatar answered Sep 27 '22 16:09

AjimOthy