title: How to get hyperlinks placed inside of shapes in .docx file using office interop word
body: <p>I’m looking for a way to programmatically replace hyperlinks from inside of word documents. I already have code which handles hyperlinks replacement as long as those’re placed along document’s main text. Unfortunately this code doesn’t see hyperlinks if those’re placed inside of shapes. The documents I deal with quite often contains diagrams (flow-charts) composed of shapes (often grouped shapes) and I can’t find any way to retrieve and modify hyperlinks from inside of the shapes.</p>
  
  <p>The code that I’ve so far allows me to retrieve all hyperlinks from documents text:</p>
  
  <pre><code>Microsoft.Office.Interop.Word.Application applicationObject = new Microsoft.Office.Interop.Word.Application();
  Microsoft.Office.Interop.Word.Document aDDoc = applicationObject.Documents.Open(FileName: @"c:\Temp\a.docx");
  Microsoft.Office.Interop.Word.Hyperlinks links = aDDoc.Hyperlinks;
  
  foreach (var hyperlink in links)
          {
              string adress = ((Microsoft.Office.Interop.Word.Hyperlink) hyperlink).Address;
          }
  </code></pre>
  
  <p>Unfortunately all the attempts to retrieve hyperlinks from shape object cause an exception:</p>
  
  <pre><code>Microsoft.Office.Interop.Word.Shapes shapes = aDDoc.Shapes;
  
  
          foreach (var shape in shapes)
          {
  
                  Microsoft.Office.Interop.Word.Hyperlink hyperlink = ((Microsoft.Office.Interop.Word.Shape)shape).Hyperlink;
  
                  string adress = ((Microsoft.Office.Interop.Word.Hyperlink)hyperlink).Address;
          }
  </code></pre>
  
  <p>This throws an exception on line where address variable is assigned. I’m working in non-English version of visual studio so the exception I get is also not in English but it says something like: operation could not be performed</p>
  
  <p>Anyone have an idea how to retrieve hyperlinks from inside of shapes?</p>