I have a scenario where I provide more than 20 hyperlinks of a VM. So, if VM's IP gets changed, I need to change manually all over the document. Is there any way I can store variable like

vm_ip = "ec2-13-211-bla-bla.amazonaws.com"

So, instead of giving hyperlink ec2-13-211-bla-bla.amazonaws.com/swagger-ui.html/food-controller, I can give {vm_ip}/swagger-ui.html/food-controller

Something like this. I was looking into macro, but it was confusing and no idea how it works.

  • ec2-13-211-bla-bla.amazonaws.com is not an IP address. It's an URL. So you just need to use a static URL and change the IP it points to when the IP changes. And if the VMs are inside a domain then you don't even need to do anything, the name will automatically be resolved to the correct IP
    – phuclv
    Commented Dec 15, 2019 at 4:04
  • That's right, It's an URL. This DNS is in word, not related to which system the file is in. I am providing a hyperlink in my MS word. Anyone can have the doc outside of VM too. Commented Dec 15, 2019 at 4:06
  • 1
  • @DavidPostill "hyperlink base" is what I found, but ur answer also helped to find. Commented Dec 15, 2019 at 13:13

2 Answers 2

  1. This can be a lot more complicated than it might appear, which is why this Answer is rather long. There are a few key things to understand, and a lot of steps. IMO that isn't something you can rely on an end user to do correctly, at least not without a very good set of instructions. Personally, I think it's a "programming" task, and since it's not one that anyone is likely to be doing very often, I'd say it still needs a good set of instructions.

  2. If you just want these hyperlinks to work in Word there are two main possibilities depending on whether you want these hyperlinks to be just texts or active hyperlinks.

    If you want to insert them in Word in such a way that they work when you save the document as HTML or PDF, this answer doesn't cover that and you should modify your question to include that requirement. Also, the field coding described here can only be used in desktop versions of Word (it may also work in one of the web or "device" versions but the last time I looked thise versions don't really work well with field codes.

  3. So, if you want these links to be texts in Word that Word does not treat as active links (e.g., they might be in your document so that people can copy the link text somewhere else), then you can do this using SET and bookmark fields.

    For example, if you just have one "base" address such as "ec2-13-211-bla-bla.amazonaws.com" that you re-use in many places, perhaps with additional address info, you can use

    { SET base1 "ec2-13-211-bla-bla.amazonaws.com" }

    at the beginning of your document (actually, in the first page header can be a safer place to put it to avoid accidental deletion). Then following the example you gave, you need a REF field:

    { REF base1 }/swagger-ui.html/food-controller

    (There is actually an advantage to omitting the REF):

    { base1 }/swagger-ui.html/food-controller

    All the { } need to be the special field code brace pairs that you can insert using ctrl-F9 on the Windows desktop version of Word.

    As far as I know, the separation of the full URL into a REF field and some plain text prevents Word from trying to turn the resulting link text into a clickable HYPERLINK field.

    When you need to change "ec2-13-211-bla-bla.amazonaws.com" to something else, e.g. perhaps it's just to "ec2-13-211-new-bla-bla.amazonaws.com", you modify that text in the SET field, update the SET field (select and press F9) then update all the other fields (e.g. ctrl-A, F9).

    If you want something a bit harder to delete accidentally than a SET field, you can create and use a Custom Document Property. e.g., in Word 2016/2019/365,

    a. go to File->Info

    b. click the Properties dropdown (at the top of the right-hand column)

    c. click Advanced Properties

    d. click the Custom Tab

    e. in the Text box, type "base1"

    f. in the Type box, ensure "Text" is selected

    g. in the Value box, type ec2-13-211-bla-bla.amazonaws.com

    h. ensure the "Link to content" box is unchecked

    i. click OK

    Then the field coding you need for the link is

    { DOCPROPERTY base1 }/swagger-ui.html/food-controller

    To update the links, you would go back into that Custom Property dialog, change the value of the demo1 property, then select the document text and update all the fields in the document (ctrl-A, F9 again).

    If you have several different "base addresses" then you either need several SET fields or several Custom Document Properties, e.g. called base1, base2, base3 or whatever.

  4. If you want a clickable hyperlink field then it gets a little bit more complicated, because then you have to nest your REF field inside a HYPERLINK field like this:

    { HYPERLINK "{ base1 }/swagger-ui.html/food-controller" }

    Again, all the { } have to be those special ctrl-F9 field brace characters, not the ones you can type on the keyboard

    But there are two problems:

    a. The display text is fixed when Word first creates the HYPERLINK field (or when you first update it). Further, it is fixed to be the same as the initial "link text" that you provided.

    b. Word tends to resolve the REF field (i.e. replace the field by its result) when you save, close and re-open the document. So at that point you lose the ability to modify the base address and update the hyperlinks.

    You probably won't notice (b) if you experiment with one hyperlink field, because what appears to prevent Word from resolving the REF field is having a bookmark (which you can insert manually, or using a SET field) somewhere in the HYPERLINK field. Because Word inserts a hidden bookmark called _GoBack at the insertion point, if the last thing you were doing before you closed the document was editing a HYPERLINK field, that field's nested fields will probably be preserved when you re-open.

    So the trick to preventing HYPERLINK fields losing their nested coding is to ensure that each one has a bookmark in it. You can either do that by inserting a SET field with a different name in each HYPERLINK, like this:

    { HYPERLINK "{ base1 }/swagger-ui.html/food-controller" { SET save1 " " } } { HYPERLINK "{ base1 }/swagger-ui.html/something-else" { SET save2 " " } }

    or you can try to generate a new bookmark name automatically for each hyperlink field, using coding like this:

    { HYPERLINK "{ base1 }/swagger-ui.html/food-controller" { SET "save{ SEQ save }" " " } } { HYPERLINK "{ base1 }/swagger-ui.html/something-else" { SET "save{ SEQ save }" " " } }

    (NB, I believe this idea originated here . The fact that it relies on a "trick" means that Microsoft could easily undermine it by making a software change, but it seems to work at the moment.)

    You should be able to use either SET/REF fields or the Custom Document Property approach for the { base1 } part, as described above.

    Finally, what about problem (a), i.e. the "display text" of the link?

    This is actually a bit harder to solve.

    A. Suppose you want a "display text" that is independent of the "link text" - for example, if you want the display text to be

    my display text

    then first, I would create the hyperlink that you field that you are eventually going to use, e.g.

    { HYPERLINK "{ base1 }/swagger-ui.html/food-controller" { SET save1 " " } }

    Then, where you actually want the hyperlink, insert a hyperlink that shows the display text that you want. One way is to insert a HYPERLINK field manually, e.g.

    • use ctrl-F9 to insert a pair of field code braces { }

    • inside, type HYPERLINK "my display text"

    • select the field and use F9 to update its result. The result should show the text you want

    • select and copy the entire text of that other field HYPERLINK "{ base1 }/swagger-ui.html/food-controller" { SET save1 " " }

    • select HYPERLINK "my display text" and paste the other code in to replace it

    • update the field value.

    There is one problem with this which may become apparent if you read through "B". I'll explain at the end.

    B. If instead, you want the display text to be the same as the link text, you have to rely on another trick.

    Let's say you used this coding

    { SET base1 "ec2-13-211-bla-bla.amazonaws.com" }
    { HYPERLINK "{ base1 }/swagger-ui.html/food-controller" { SET save1 " " } }

    Then what you have to do is delete the "base1" bookmark, then update the HYPERLINK fields. At that point you should see that the HYPERLINK field result shows an error like this (in the English language version of Word):

    Error! Hyperlink reference not valid.

    This seems to reset the display text. So now, you can recreate the base1 bookmark with the address that you want, and update all the HYPERLINK fields again.

    When you are using SET fields and REF fields, you don't have to delete the SET field. You change the SET to SEQ:

    { SET base1 "ec2-13-211-bla-bla.amazonaws.com" }


    { SEQ base1 "ec2-13-211-bla-bla.amazonaws.com" }

    Then update the field, and that destroys the bookmark.

    After you've updated the hyperlinks, you change SEQ back to SET and modify the address, e.g. so you have

    { SET base1 "ec2-13-211-new-bla-bla.amazonaws.com" }

    then update the field, then all the HYPERLINK fields.

    Doing the equivalent thing with Custom Document Properties is probably a bit more awkward because you can rename the properties in a single step. The best you can probably do is select the property in the list, change the name, and click Add. That actually creates a new property. Delete the old one, update the HYPERLINK fields, then go back and recreate the property names and values that you need.

    As a final observation, if you actually need approach "A", where the display text does not alter, you should not do anything that deletes the value of the "base1" bookmark. If you do, that will reset the display text and you will probably have to re-insert a HYPERLINK field that will recreate the text you want. Not good.

  • Appreciate your effort, But, I found a shortcut "hyperlink base". But, from your answer I learned so many things like base, hyperlink, ref so many things Commented Dec 15, 2019 at 13:16
  • 1
    Yes, I thought about whether hyperlink base would be any use in the situation you described and decided that it probably wouldn’t, especially if you need more than one such base. But if it worked for you, maybe you could write that up as an Answer - otherwise I’ll put a note about it in my Answer.
    – user1116187
    Commented Dec 15, 2019 at 13:22

If just want to set value, then can use what @yokki said.

For hyperlink, word provides "hyperlink base". So, if you give "swagger-ui.html/food-controller" in hyperlink, it will take {$hyperlink_base}/swagger-ui.html/food-controller. If you want to use hyperlink of some other websites and don't want to use "hyperlink base", use full URL.

How to set "hyperlink base"?

  1. Goto "File" ribbon
  2. Right side top "Properties"
  3. click on it & select "Advanced Properties".
  4. On "summary" tab only, last field contains "Hyperlink base".

In my case, I set it to, "ec2-13-211-bla-bla.amazonaws.com".

So, all my link where I gave relative path, not whole URL,

It takes "ec2-13-211-bla-bla.amazonaws.com/swagger-ui.html/food-controller".

So, If I need to change IP, I will just update "hyperlink base".

  • 1
    BTW, you can mark your own reply as the Answer. I think you should.
    – user1116187
    Commented Dec 15, 2019 at 16:12

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .