Setting Links in Word Programmatically
Since I didn’t find anything on this topic via Google… Just a quick “how to” on setting links in Microsoft Word programmatically (in this case, C#):
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
<FONT face=Courier><FONT color=#0000ff>public void </FONT><FONT color=#000000>CheckRuntime() {</FONT> <FONT color=#0000ff>object </FONT><FONT color=#000000>reference = </FONT><FONT color=#0000ff>null</FONT><FONT color=#000000>;</FONT> <FONT color=#0000ff>try </FONT><FONT color=#000000>{</FONT> <FONT color=#000000>reference = </FONT><FONT color=#808000>Marshal</FONT><FONT color=#000000>.GetActiveObject(</FONT><FONT color=#ff00ff>"Word.Application"</FONT><FONT color=#000000>);</FONT> <FONT color=#000000>}</FONT> <FONT color=#0000ff>catch </FONT><FONT color=#000000>(</FONT><FONT color=#808000>COMException</FONT><FONT color=#000000>) {</FONT> <FONT color=#008080>System</FONT><FONT color=#000000>.</FONT><FONT color=#808000>Console</FONT><FONT color=#000000>.Out.WriteLine(</FONT><FONT color=#ff00ff>"No instances found..."</FONT><FONT color=#000000>);</FONT> <FONT color=#000000>}</FONT> <FONT color=#0000ff>if </FONT><FONT color=#000000>(reference != </FONT><FONT color=#0000ff>null</FONT><FONT color=#000000>) {</FONT> <FONT color=#0000ff>try </FONT><FONT color=#000000>{</FONT> <FONT color=#008080>System</FONT><FONT color=#000000>.</FONT><FONT color=#808000>Console</FONT><FONT color=#000000>.Out.WriteLine(</FONT><FONT color=#ff00ff>"Found running instance!"</FONT><FONT color=#000000>);</FONT> <FONT color=#000000>ApplicationClass wordRuntime = (ApplicationClass)reference;</FONT> <FONT color=#008080>System</FONT><FONT color=#000000>.</FONT><FONT color=#008080>Text</FONT><FONT color=#000000>.</FONT><FONT color=#808000>StringBuilder </FONT><FONT color=#000000>buffer = </FONT><FONT color=#0000ff>new </FONT><FONT color=#808000>StringBuilder</FONT><FONT color=#000000>();</FONT> <FONT color=#0000ff>foreach </FONT><FONT color=#000000>(Document document </FONT><FONT color=#0000ff>in </FONT><FONT color=#000000>wordRuntime.Documents) {</FONT> <FONT color=#000000>buffer.AppendFormat(</FONT><FONT color=#ff00ff>"{0}|"</FONT><FONT color=#000000>, document.Name);</FONT> <FONT color=#0000ff>if </FONT><FONT color=#000000>(wordRuntime.Selection != </FONT><FONT color=#0000ff>null</FONT><FONT color=#000000>) {</FONT> <FONT color=#008080>System</FONT><FONT color=#000000>.</FONT><FONT color=#808000>Console</FONT><FONT color=#000000>.Out.WriteLine(</FONT><FONT color=#ff00ff>"Selected text: \"{0}\""</FONT><FONT color=#000000>,</FONT> <FONT color=#000000>wordRuntime.Selection.</FONT><FONT color=#008080>Text</FONT><FONT color=#000000>);</FONT> <FONT color=#0000ff>object </FONT><FONT color=#000000>optional = </FONT><FONT color=#808000>Missing</FONT><FONT color=#000000>.Value;</FONT> <FONT color=#0000ff>object </FONT><FONT color=#000000>url = </FONT><FONT color=#ff00ff>"http://www.google.com/"</FONT><FONT color=#000000>;</FONT> <FONT color=#0000ff>if</FONT><FONT color=#000000>(wordRuntime.Selection.Hyperlinks.Count == </FONT><FONT color=#800080>0</FONT><FONT color=#000000>) {</FONT> <FONT color=#000000>wordRuntime.Selection.Hyperlinks._Add(</FONT> <FONT color=#000000>wordRuntime.Selection.Range,</FONT> <FONT color=#0000ff>ref </FONT><FONT color=#000000>url,</FONT> <FONT color=#0000ff>ref </FONT><FONT color=#000000>optional</FONT> <FONT color=#000000>);</FONT> <FONT color=#000000>}</FONT> <FONT color=#000000>}</FONT> <FONT color=#000000>}</FONT> <FONT color=#008080>System</FONT><FONT color=#000000>.</FONT><FONT color=#808000>Console</FONT><FONT color=#000000>.Out.WriteLine(</FONT> <FONT color=#0000ff>string</FONT><FONT color=#000000>.Format(</FONT><FONT color=#ff00ff>"Currently open documents are: {0}"</FONT><FONT color=#000000>,</FONT> <FONT color=#000000>buffer.ToString().Trim(</FONT><FONT color=#ff00ff>'|'</FONT><FONT color=#000000>))</FONT> <FONT color=#000000>);</FONT> <FONT color=#000000>}</FONT> <FONT color=#0000ff>finally </FONT><FONT color=#000000>{</FONT> <FONT color=#808000>Marshal</FONT><FONT color=#000000>.ReleaseComObject(reference);</FONT> <FONT color=#000000>}</FONT> <FONT color=#000000>}</FONT> <FONT color=#000000>}</FONT> </FONT> |
The code is wrapped in a function call I’m using for testing. The key...