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.