using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using word = Microsoft.Office.Interop.Word;
using Excel = Microsoft.Office.Interop.Excel;
using PPT = Microsoft.Office.Interop.PowerPoint;
using System.Text.RegularExpressions;
namespace UpdateUrls
{
public partial class updatelinks : System.Web.UI.Page
{
string Insightdocuments = @"D:\Sireesha\POC\oldDocuments";
string Archivedocuments = @"D:\Sireesha\POC\newDocuments";
string srcText = "google";
string replaceText = "youtube";
object missing = Type.Missing;
protected void Page_Load(object sender, EventArgs e)
{
DirectoryInfo di = new DirectoryInfo(Insightdocuments);
FileInfo[] files = di.GetFiles().Where(f => !f.Attributes.HasFlag(FileAttributes.Hidden)).ToArray();
for (int i = 0; i < files.Length; i++)
{
if (files[i].ToString().Contains(".docx") || files[i].ToString().Contains(".doc"))
{
UpdateLinksInWord(files[i]);
}
else if (files[i].ToString().Contains(".xlsx") || files[i].ToString().Contains(".xls"))
{
UpdateLinksInExcel(files[i]);
}
else
{
UpdateLinksInPPT(files[i])
}
}
Response.Write("Links has been updated from insight to archival system");
}
url update in word
private void UpdateLinksInWord(FileInfo file)
{
word.Application applicationObject = new word.Application();
try
{
object fileurl = file.FullName;
applicationObject.DisplayAlerts = word.WdAlertLevel.wdAlertsNone;
word.Document documentObject = applicationObject.Documents.Open(fileurl);
word.Hyperlinks links = documentObject.Hyperlinks;
string currLinkAddress = string.Empty;
string currLinkText = string.Empty;
foreach (word.Hyperlink link in links)
{
currLinkAddress = link.Address;
currLinkText = link.TextToDisplay;
if (Regex.IsMatch(currLinkAddress, srcText, RegexOptions.IgnoreCase))
{
link.Address = Regex.Replace(currLinkAddress, srcText, replaceText, RegexOptions.IgnoreCase);
}
if (Regex.IsMatch(currLinkText, srcText, RegexOptions.IgnoreCase))
{
link.TextToDisplay = Regex.Replace(currLinkText, srcText, replaceText, RegexOptions.IgnoreCase);
}
}
object archivaldoc = file.FullName.Replace(Insightdocuments, Archivedocuments);
documentObject.SaveAs2(ref archivaldoc);
documentObject.Close(true);
applicationObject.Quit();
}
catch (Exception ex)
{
Response.Write("Exception in UpdateLinksInWord" + ex.Message);
}
}
url update in excel
private void UpdateLinksInExcel(FileInfo file)
{
Excel.Application oExcel = new Excel.Application();
try
{
//specify the file name where its actually exist
string filepath = file.FullName;
//pass that to workbook object
Excel.Workbook WB = oExcel.Workbooks.Open(filepath);
// statement get the workbookname
string ExcelWorkbookname = WB.Name;
// statement get the worksheet count
int worksheetcount = WB.Worksheets.Count;
for (int ws = 1; ws <= worksheetcount; ws++)
{
Excel.Worksheet wks = (Excel.Worksheet)WB.Worksheets[ws];
// statement get the firstworksheetname
Excel.Hyperlinks excelinks = wks.UsedRange.Cells.Hyperlinks;
string currLink = string.Empty;
string TTD = string.Empty;
foreach (Excel.Hyperlink excellink in excelinks)
{
currLink = excellink.Address;
TTD = excellink.TextToDisplay;
if (Regex.IsMatch(currLink, srcText, RegexOptions.IgnoreCase))
{
excellink.Address = Regex.Replace(currLink, srcText, replaceText, RegexOptions.IgnoreCase);
}
if (Regex.IsMatch(TTD, srcText, RegexOptions.IgnoreCase))
{
excellink.TextToDisplay = Regex.Replace(TTD, srcText, replaceText, RegexOptions.IgnoreCase);
}
}
}
// Saves the modified file into Archive folder
object UpdatedFile = file.FullName.Replace(Insightdocuments, Archivedocuments);
WB.SaveAs(UpdatedFile, missing,
missing, missing, missing, missing, Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlNoChange);
WB.Close(true);
oExcel.Quit();
}
catch (Exception ex)
{
Response.Write("Exception in UpdateLinksInExcel" + ex.Message);
}
}
url update in ppt
private void UpdateLinksInPPT(FileInfo file)
{
powerpoint.Application pptapp = new powerpoint.Application();
powerpoint.Presentation presntn = pptapp.Presentations.Open(olddocuments ,MsoTriState.msoFalse, MsoTriState.msoFalse, MsoTriState.msoFalse);
foreach (powerpoint.Slide mSlide in presntn.Slides)
{
powerpoint.Hyperlinks links = mSlide.Hyperlinks;
for (int i = 1; i <= links.Count; i++)
{
powerpoint.Hyperlink mLink = links[i];
string currLinkAddress = mLink.Address;
string currLinkText = mLink.TextToDisplay;
if (Regex.IsMatch(currLinkAddress, srcText, RegexOptions.IgnoreCase))
{
mLink.Address = Regex.Replace(currLinkAddress, srcText, replaceText, RegexOptions.IgnoreCase);
}
if (Regex.IsMatch(currLinkText, srcText, RegexOptions.IgnoreCase))
{
mLink.TextToDisplay = Regex.Replace(currLinkText, srcText, replaceText, RegexOptions.IgnoreCase);
}
}
}
presntn.SaveAs(newdocuments , powerpoint.PpSaveAsFileType.ppSaveAsDefault,MsoTriState.msoTrue);
presntn.Close();
pptapp.Quit();
}
}
}