We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Beginner Questions

I have never programmed in C# before or used Apose.words before. I was asked to write a program for Apose.words to update a large amount of refences in doc files, and convert docx to doc files. I was also asked to write one to read the references from the word documents into an excel spreadsheet. Create a list of references with the associated file name from the document they were originally in. This way the references could be updated in excel. Then create a program to update all off the references in the documents based off the updates in the excel file. I have written what I believe will work for the first problem. I was hoping somone could review it and tell me if I am making in obvious mistakes and if there was a way to improve its speed as it will be looking at many documents. Also I was wondering if there was any advice or programs already written that might solve the second two problems dealing with excel spreadsheets. Thank you for any and all help.

using

System;

using

System.Collections.Generic;

using

System.Text;

using

System.IO;

using

Aspose.Words;

namespace

TextReplaceDemo

{

class Program

{

static void Main(string[] args)

{

// Set license info

Aspose.Words.License license =

new Aspose.Words.License();

license.SetLicense(

"Aspose.Total.lic");

string path = Directory.GetCurrentDirectory();

// Get all docx files in the search location, we will need to convert them to doc files first

DirectoryInfo directoryInfo = new DirectoryInfo(path + @"\In\");

FileInfo[] docxfiles = directoryInfo.GetFiles("*.docx", SearchOption.AllDirectories);

//Create a directory for backups

if (Directory.Exists(path + @"\Backup\"))

Directory.Delete(path + @"\Backup\",true);

Directory.CreateDirectory(path + @"\Backup\");

//Create a directory to save files converted from Docx to Doc


if (Directory.Exists(path + @"\In\DocxtoDoc"))


Directory.Delete(path + @"\In\DocxtoDoc", true);


Directory.CreateDirectory(path + @"\In\DocxtoDoc");

//Create a directory for completed files


if (Directory.Exists(path + @"\Out"))


Directory.Delete(path + @"\Out", true);


Directory.CreateDirectory(path + @"\Out");

foreach (FileInfo file in docxfiles) //Go through all the docx files…


{

try

{

file.CopyTo(path +

@"\Backup\" + file.Name); //Create a backup of docx file

Document doc =

new Document(file.FullName); //Open docx file

doc.Save(path +

@"\In\DocxtoDoc\" + file.Name, SaveFormat.Doc); //Save file as doc

}

catch (Exception ex) // On Error displace exception

{

Console.WriteLine(ex.Message);

Console.ReadLine();

}

}

// Get all doc files in the search location

FileInfo[] docfiles = directoryInfo.GetFiles("*.doc", SearchOption.AllDirectories);

// Loop over the files

foreach (FileInfo file in docfiles) //Go through all the doc files....

{

try

{

bool isModified = false;

// Make a back up of the file in case we mess something up

file.CopyTo(path +

@"\Backup\" + file.Name);

Document doc =

new Document(file.FullName);

// Grab all the paragraphs in the document and loop over them

NodeCollection nc = doc.GetChildNodes(NodeType.Paragraph,

true);

foreach (Node n in nc)

{

Paragraph thisPara = (Paragraph)n;

// Check each paragraph to see if is a reference by examining the assigned style.

// If it is and it is any variation of a reference to a Standard Spec, update it

// to a 2014 reference.

if (thisPara.ParagraphFormat.StyleName.Contains("Reference Style"))

{

if (n.ToTxt().Contains("SFLC") && n.ToTxt().Contains("SFLC Std Spec") && n.ToTxt().Contains("0000"))

{

n.Range.Replace(n.Range.Text.Replace(

"\r", ""), "Surface Forces Logistics Center Standard Specification 0000 (SFLC Std Spec 0000), 2014, General Requirements", false, false);

isModified =

true;

}

if (n.ToTxt().Contains("SFLC") && n.ToTxt().Contains("SFLC Std Spec") && n.ToTxt().Contains("0850"))

{

n.Range.Replace(n.Range.Text.Replace(

"\r", ""), "Surface Forces Logistics Center Standard Specification 0085 (SFLC Std Spec 0085), 2014, General Requirements for Drawing Preparation", false, false);

isModified =

true;

}

if (n.ToTxt().Contains("SFLC") && n.ToTxt().Contains("SFLC Std Spec") && n.ToTxt().Contains("0085"))

{

n.Range.Replace(n.Range.Text.Replace(

"\r", ""), "Surface Forces Logistics Center Standard Specification 0085 (SFLC Std Spec 0085), 2014, General Requirements for Drawing Preparation", false, false);

isModified =

true;

}

if (n.ToTxt().Contains("SFLC") && n.ToTxt().Contains("SFLC Std Spec") && n.ToTxt().Contains("0450"))

{

n.Range.Replace(n.Range.Text.Replace(

"\r", ""), "Surface Forces Logistics Center Standard Specification 0450 (SFLC Std Spec 0450), 2014, Electrical Power for Contractor's Tools & Equipment", false, false);

isModified =

true;

}

if (n.ToTxt().Contains("SFLC") && n.ToTxt().Contains("SFLC Std Spec") && n.ToTxt().Contains("0740"))

{

n.Range.Replace(n.Range.Text.Replace(

"\r", ""), "Surface Forces Logistics Center Standard Specification 0740 (SFLC Std Spec 0740), 2014, Welding and Allied Processes", false, false);

isModified =

true;

}

if (n.ToTxt().Contains("SFLC") && n.ToTxt().Contains("SFLC Std Spec") && n.ToTxt().Contains("2350"))

{

n.Range.Replace(n.Range.Text.Replace(

"\r", ""), "Surface Forces Logistics Center Standard Specification 2350 (SFLC Std Spec 2350), 2014, Inspect, Test and Recondition DC Propulsion Generators & Motors In-Place", false, false);

isModified =

true;

}

if (n.ToTxt().Contains("SFLC") && n.ToTxt().Contains("SFLC Std Spec") && n.ToTxt().Contains("2351"))

{

n.Range.Replace(n.Range.Text.Replace(

"\r", ""), "Surface Forces Logistics Center Standard Specification 2351 (SFLC Std Spec 2351), 2014, Overhaul DC Generators & Motors", false, false);

isModified =

true;

}

if (n.ToTxt().Contains("SFLC") && n.ToTxt().Contains("SFLC Std Spec") && n.ToTxt().Contains("3020"))

{

n.Range.Replace(n.Range.Text.Replace(

"\r", ""), "Surface Forces Logistics Center Standard Specification 3020 (SFLC Std Spec 3020), 2014, Overhaul AC Electrical Motors", false, false);

isModified =

true;

}

if (n.ToTxt().Contains("SFLC") && n.ToTxt().Contains("SFLC Std Spec") && n.ToTxt().Contains("3041"))

{

n.Range.Replace(n.Range.Text.Replace(

"\r", ""), "Surface Forces Logistics Center Standard Specification 3041 (SFLC Std Spec 3041), 2014, Shipboard Electrical Cable Test", false, false);

isModified =

true;

}

if (n.ToTxt().Contains("SFLC") && n.ToTxt().Contains("SFLC Std Spec") && n.ToTxt().Contains("3042"))

{

n.Range.Replace(n.Range.Text.Replace(

"\r", ""), "Surface Forces Logistics Center Standard Specification 3042 (SFLC Std Spec 3042), 2014, Shipboard Electrical Cable Removal, Relocation, Splice, Repair, and Installation", false, false);

isModified =

true;

}

if (n.ToTxt().Contains("SFLC") && n.ToTxt().Contains("SFLC Std Spec") && n.ToTxt().Contains("3100"))

{

n.Range.Replace(n.Range.Text.Replace(

"\r", ""), "Surface Forces Logistics Center Standard Specification 3100 (SFLC Std Spec 3100), 2014, Inspect, Test and Recondition AC Sychronous Machines In-Place", false, false);

isModified =

true;

}

if (n.ToTxt().Contains("SFLC") && n.ToTxt().Contains("SFLC Std Spec") && n.ToTxt().Contains("3101"))

{

n.Range.Replace(n.Range.Text.Replace(

"\r", ""), "Surface Forces Logistics Center Standard Specification 3101 (SFLC Std Spec 3101), 2014, Overhaul AC Synchronous Machines", false, false);

isModified =

true;

}

if (n.ToTxt().Contains("SFLC") && n.ToTxt().Contains("SFLC Std Spec") && n.ToTxt().Contains("5000"))

{

n.Range.Replace(n.Range.Text.Replace(

"\r", ""), "Surface Forces Logistics Center Standard Specification 5000 (SFLC Std Spec 5000), 2014, Auxiliary Machine Systems", false, false);

isModified =

true;

}

if (n.ToTxt().Contains("SFLC") && n.ToTxt().Contains("SFLC Std Spec") && n.ToTxt().Contains("5100"))

{

n.Range.Replace(n.Range.Text.Replace(

"\r", ""), "Surface Forces Logistics Center Standard Specification 5100 (SFLC Std Spec 5100), 2014, Clean Shipboard Ventilation Systems", false, false);

isModified =

true;

}

if (n.ToTxt().Contains("SFLC") && n.ToTxt().Contains("SFLC Std Spec") && n.ToTxt().Contains("6310"))

{

n.Range.Replace(n.Range.Text.Replace(

"\r", ""), "Surface Forces Logistics Center Standard Specification 6310 (SFLC Std Spec 6310), 2014, Requirements for Preservation of Ship Structures", false, false);

isModified =

true;

}

if (n.ToTxt().Contains("SFLC") && n.ToTxt().Contains("SFLC Std Spec") && n.ToTxt().Contains("6341"))

{

n.Range.Replace(n.Range.Text.Replace(

"\r", ""), "Surface Forces Logistics Center Standard Specification 6341 (SFLC Std Spec 6341), 2014, Install Interior Deck Covering Systems", false, false);

isModified =

true;

}

if (n.ToTxt().Contains("SFLC") && n.ToTxt().Contains("SFLC Std Spec") && n.ToTxt().Contains("8634"))

{

n.Range.Replace(n.Range.Text.Replace(

"\r", ""), "Surface Forces Logistics Center Standard Specification 8634 (SFLC Std Spec 8634), 2014, Drydocking", false, false);

isModified =

true;

}

if (n.ToTxt().Contains("SFLC") && n.ToTxt().Contains("SFLC Std Spec") && n.ToTxt().Contains("8635"))

{

n.Range.Replace(n.Range.Text.Replace(

"\r", ""), "Surface Forces Logistics Center Standard Specification 8635 (SFLC Std Spec 8635), 2014, Temporary Services", false, false);

isModified =

true;

}

if (n.ToTxt().Contains("SFLC") && n.ToTxt().Contains("SFLC Std Spec") && n.ToTxt().Contains("8636"))

{

n.Range.Replace(n.Range.Text.Replace(

"\r", ""), "Surface Forces Logistics Center Standard Specification 8636 (SFLC Std Spec 8636), 2014, Temporary Hull Accesses", false, false);

isModified =

true;

}

}

// Another random replacement sample.

//if (n.ToTxt().Contains("in accordance with COMDTINST M10360.3, Appendix A (Cutter and Boat Exterior Painting Systems"))

//{

// n.Range.Replace("Appendix A", "Appendix D", false, false);

// isModified = true;

//}

}

// Save new version if we made any replacements.

if (isModified)

{

doc.Save(path +

@"\Out\" + file.Name);

}

}

catch (Exception ex) // On Error displace exception

{

Console.WriteLine(ex.Message);

Console.ReadLine();

}

}

}

}

}

Hi Ross,

We are analyzing your code. In the meantime, can you please further elaborate your second requirement which is about Excel files. Please also share a sample Excel file and your required output file.

Best Regards,

I would like to create an excel file with three columns as in the attached file.

I would like the program to itterate through all files and subfolders in the directory it is run in. Each time it opens a file it looks for anything with the reference style applied to it. Each time it finds one, add a row in the excel file with the name of the document and the reference.

Once done, close and save the excel file.

This excel file will give some users a birds eye view of the refrences and perhaps make them easier to update or see errors.

Once a user has updated all the references in the third column, I would like a program to do the following:

Open the excel file, read the file name, and create a list of all the old and new references for that file. Open the file, replace the references. Close the word document, and look for the next document listed in the excel file. I would like it to repeat this through the entire excel document.

I know that is a lot. I have to write the code and send it to someone else, or I would tinker with it more myself. I was also not able to find an example of any other source code that could complete this task for me.

As for the program I have already posted above. The only things I could think of to perhaps speed it up would be to nest the if statements. But I think that might also make little to no difference in the long run.

Thanks ahead for any and all help. I do appreciate it.

Here is a copy of some code I thought might work using tables in word instead of using excel. Not sure about reading it and updating the files yet.

using System;

using System.Collections.Generic;

using System.Text;

using System.IO;

using Aspose.Words;

namespace TextReplaceDemo

{

class Program

{

static void Main(string[] args)

{

// Set license info

Aspose.Words.License license = new Aspose.Words.License();

license.SetLicense("Aspose.Total.lic");

string path = Directory.GetCurrentDirectory();

//Create a directory for completed files


if (Directory.Exists(path + @"\Out"))


Directory.Delete(path + @"\Out", true);


Directory.CreateDirectory(path + @"\Out");

// Get all doc files in the search location


FileInfo[] docfiles = directoryInfo.GetFiles("*.doc", SearchOption.AllDirectories);

//Create File to save table in


Document doctwo = new Document();


DocumentBuilder builder = new DocumentBuilder(doctwo);


Table table = builder.StartTable();

// Loop over the files


foreach (FileInfo file in docfiles) //Go through all the doc files…


{


try


{


Document doc = new Document(file.FullName);

// Grab all the paragraphs in the document and loop over them


NodeCollection nc = doc.GetChildNodes(NodeType.Paragraph, true);


foreach (Node n in nc)


{


Paragraph thisPara = (Paragraph)n;


// Check each paragraph to see if is a reference by examining the assigned style.

if (thisPara.ParagraphFormat.StyleName.Contains(“Reference Style”))


{


// Insert a cell


builder.InsertCell();


// Use fixed column widths.


table.AutoFit(AutoFitBehavior.FixedColumnWidths);

builder.CellFormat.VerticalAlignment = CellVerticalAlignment.Center;


builder.Write(file.FullName);

// Insert a cell


builder.InsertCell();


builder.Write(n.ToTxt());

builder.EndRow();


}


}


}


catch (Exception ex) // On Error displace exception


{


Console.WriteLine(ex.Message);


Console.ReadLine();


}


}


builder.EndTable();


doctwo.Save(path + @"\Out\ReferencesView.doc");


}


}


}

Hi Ross,

We are working on this and will share the optimized code with you shortly.

Best Regards,