Monday 12 March 2018

Find and replace hyperlinks in word document

using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Office.Interop.Word;

namespace WordHyperLink
{
    class Program
    {
        static void Main(string[] args)
        {
            string folderpath = "d:\\Editted_old";
            DirectoryInfo di = new DirectoryInfo(folderpath);
            FileInfo[] files = di.GetFiles("*.docx");
            foreach (FileInfo singleFile in files)
            {
                Application applicationObject = new Application();
                object missing = Type.Missing;
                //object fileName = @"d:\Editted_old\LinkWord.docx";
                object fileName = singleFile.FullName;
                object False = false;
                applicationObject.DisplayAlerts = WdAlertLevel.wdAlertsNone;
                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);
                Hyperlinks links = documentObject.Hyperlinks;

                for (int i = 1; i <= links.Count; i++)
                {
                    object index = (object)i;
                    Hyperlink link = links.get_Item(ref index);
                    string currLink = link.Address;

                    link.Address = "www.microsoft.com";
                    link.TextToDisplay = "Microsoft Home Page";
                }
                object fileName2 = singleFile.FullName.Replace("Editted_old","Editted");
                documentObject.SaveAs2(ref fileName2);
                applicationObject.Quit(ref missing, ref missing, ref missing);
            }
        }
    }
}

No comments:

Post a Comment

Featured post

Data connections in Infopath forms

https://www.qdoscc.com/blog/how-automatically-retrieve-current-username-infopath-sharepoint-list-form

Popular Posts