Read Write Hyperlinks Microsoft.Office.Interop.Word

S

SurferJoe

Trying to read the hyperlink(s) from Word 2007 documents, make changes and
write them back. Being new to C#, I am not sure how to address the
Microsoft.Office.Interop.Word.Hyperlinks collection correctly.


Thanx in adavnce,

Greg
 
J

Jialiang Ge [MSFT]

Hello Greg,

From your post, my understanding on this issue is: you want to know how to
manipulate Microsoft.Office.Interop.Word.Hyperlinks to read and edit
hyperlinks with C# in a Word 2007 document. If I'm off base, please feel
free to let me know.

Suppose that you have a C# project, first you need to confirm that the
Office PIA has been installed
(http://www.microsoft.com/downloads/details.aspx?familyid=59daebaa-bed4-4282
-a28c-b864d8bfa513&displaylang=en ) and the reference to
Microsoft.Office.Interop.Word has been added. (To add reference to
Microsoft.Office.Interop.Word, you need to right click the project in
Solution Explorer, choose 'Add Reference' and select 'COM' in 'Add
Reference' dialog, then find the 'Microsoft Word 12.0 Object Library' and
click OK.)

Secondly, the following codes get an instance of
Microsoft.Office.Interop.Word.Hyperlinks collection:
Word.Application applicationObject = new Word.Application();
object missing = Type.Missing;
object fileName = @"d:\test.docx";
object False = false;
applicationObject.DisplayAlerts = Word.WdAlertLevel.wdAlertsNone;
Word.Document documentObject = applicationObject.Documents.Open(
ref fileName, ref missing, ref missing, ref missing,
ref missing, ref missing, ref missing, ref missing, ref missing,
ref missing, ref missing, ref False, ref missing, ref missing,
ref missing, ref missing);
Word.Hyperlinks links = documentObject.Hyperlinks;

To read the links one by one, you can use the get_Item method of Hyperlinks
collection
for (int i = 1; i <= links.Count; i++)
{
object index = (object)i;
Word.Hyperlink link = links.get_Item(ref index);
}

The page
http://msdn2.microsoft.com/fr-fr/library/microsoft.office.interop.word.hyper
link_members(VS.80).aspx gives a detailed description of Hyperlink object.
Suppose that you want to edit the text and URL of a link, you can refer to
the following sample code:
Word.Hyperlink link = links.get_Item(ref index);
link.Address = "http://www.microsoft.com";
link.TextToDisplay = "Microsoft Home Page";

Finally, you may save the document and quit the Word.
documentObject.Save();
applicationObject.Quit(ref missing, ref missing, ref missing);

Please let me know if you have any other concerns, or need anything else.

Sincerely,
Jialiang Ge ([email protected], remove 'online.')
Microsoft Online Community Support

==================================================
For MSDN subscribers whose posts are left unanswered, please check this
document: http://blogs.msdn.com/msdnts/pages/postingAlias.aspx

Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications. If you are using Outlook Express/Windows Mail, please make sure
you clear the check box "Tools/Options/Read: Get 300 headers at a time" to
see your reply promptly.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
 
S

SurferJoe

Oh boy, I really appreciate your help with this.
Following code listing is what I worked out from your example.

We have recently purchased (50) Office 2007 upgrades, what I really need is
to expose the actual link address string like;
@"file://C:\Temp\Source Folder\Test Document #1.doc"

Evaluate the link(s) then write them back like this;

@"file://C:\Temp\Source Folder\Test Document #1.docx" // Please Note .DOCX

This excerpt from the code listing and many other variations I have tried
only returns only "Test Document" not @"file://C:\Temp\Source Folder\Test
Document #1.doc"

for (int i = 1; i <= links.Count; i++)
{
object index = (object)i;
Word.Hyperlink link = links.get_Item(ref index);
MessageBox.Show(link.Address);
Console.WriteLine(link.AddressOld);
}


//Code Listing
namespace WordReadHyperLinks
{
public partial class frmWordReadHyperlinks : Form
{
public frmWordReadHyperlinks()
{
InitializeComponent();
}

private void btnReadHyperlink_Click(object sender, EventArgs e)
{
Microsoft.Office.Interop.Word.Hyperlinks collection;
Word.Application applicationObject = new Word.Application();
object missing = Type.Missing;
object fileName = @"C:\Temp\Source Folder\Test Document #1.doc";
object False = false;
applicationObject.DisplayAlerts =
Word.WdAlertLevel.wdAlertsNone;
Word.Document documentObject = applicationObject.Documents.Open(
ref fileName, ref missing, ref missing, ref missing,
ref missing, ref missing, ref missing, ref missing, ref
missing,
ref missing, ref missing, ref False, ref missing, ref
missing,
ref missing, ref missing);
Word.Hyperlinks links = documentObject.Hyperlinks;

try
{

//To read the links one by one, you can use the get_Item
method of Hyperlinks
// collection
for (int i = 1; i <= links.Count; i++)
{
object index = (object)i;
Word.Hyperlink link = links.get_Item(ref index);
MessageBox.Show(link.Address);
Console.WriteLine(link.Address);
}
}
catch (Exception ex)
{
// Respond to the error
}
finally
{
// Close and release the Document object.
if (documentObject != null)
{
documentObject.Close(ref missing, ref missing,
ref missing);
documentObject = null;
}

// Quit Word and release the ApplicationClass object.
if (applicationObject != null)
{
applicationObject.Quit(ref missing, ref missing,
ref missing);
applicationObject = null;
}

GC.Collect();
GC.WaitForPendingFinalizers();
GC.Collect();
GC.WaitForPendingFinalizers();
}
}

private void btnExit_Click(object sender, EventArgs e)
{
this.Close();

}
}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top