I wrote a C# program that converts a 1997 - 2003 .DOC Word Documents into TEXT FILES. I am running the program from the Command Line. I have to convert over 300,000 documents. Every document takes from 0.7 to 1.5 seconds to process. The program works fine. I am using library:
- Microsoft.Office.Interop.Word
The problem here is that few Word Documents are corrupted, some have macros enabled, some are lock for editing etc. I am able to handle most of those errors, except for one.
A similar problem that I am having can be found on this link (except for the fact that I am converting into TXT): Microsoft Interop saveAs command failed
Here is my problem; I get an error once every 25 - 35 minutes. And something is in that library, that catches the error and generates a POP-UP saying "An error was encountered with the file. Command Failed", and the pop-up has only an 'OK' BUTTON.
When that warning message window pops up, the program pauses until someone manually hits 'RETURN', or click on the 'OK' button.
How can I write a program that runs in the background and simulates someone pressing the RETURN button? This way the pop-up generated within the command line will be gone, and my program resumes.
class Program
{
public static object refTrue = true;
public static object refFalse = false;
public static object refMissing = Type.Missing;
static void Main(string[] args)
{
Word._Application word = null;
Word.Documents documents;
string dirpath = "C:\\Blobs\\directory\\";
String msserver = "SERVER";
String msdatabase = "DB";
String msconnstring = String.Format("Data Source={0};Trusted_Connection=yes;Database={1};MultipleActiveResultSets=true", msserver, msdatabase);
using (System.Data.SqlClient.SqlConnection con = new System.Data.SqlClient.SqlConnection(msconnstring))
{
//open connection
con.Open();
System.Data.SqlClient.SqlCommand cmdtest = con.CreateCommand();
cmdtest.CommandText = "select f.filenamedoc from dbo.tb_worddtl_txt_ f left join dbo.tb_worddtl_txt p on f.filenamedoc = p.filenamedoc where p.filenamedoc is null ";
cmdtest.CommandType = CommandType.Text;
cmdtest.CommandTimeout = 60;
List<string> Filestoprocesstest = new List<string>();
SqlDataReader missingFiles = cmdtest.ExecuteReader();
while (missingFiles.Read())
{
Filestoprocesstest.Add(missingFiles[0].ToString().Trim());
}
//Will process missing files:
foreach (string value in Filestoprocesstest)
{
Console.WriteLine(value);
word = new Word.Application();
documents = word.Documents;
String file2p = dirpath.Trim() + value.Trim();
Object filename = file2p;
object newRefTargetFile = file2p.Replace(".doc", ".txt");
if (File.Exists(file2p))
{
/** converts .DOC into .TXT **/
try
{
Word.Document document = documents.OpenNoRepairDialog(
ref filename,
ref refTrue,
ref refFalse,
ref refFalse,
ref refMissing,
ref refMissing,
ref refMissing,
ref refMissing,
ref refMissing,
ref refMissing,
ref refMissing,
ref refTrue,
ref refFalse,
ref refMissing,
ref refMissing,
ref refMissing);
if (document == null)
throw new Exception("Could not read " + filename);
word.Visible = false;
word.DisplayAlerts = Word.WdAlertLevel.wdAlertsNone;
object wdFormatDOSText = Word.WdSaveFormat.wdFormatDOSText;
word.Options.SavePropertiesPrompt = false;
word.Options.SaveNormalPrompt = false;
word.ActiveDocument.SaveAs(ref newRefTargetFile, ref wdFormatDOSText,
ref refMissing, ref refMissing, ref refMissing,
ref refMissing, ref refMissing, ref refMissing,
ref refMissing, ref refMissing, ref refMissing,
ref refMissing, ref refMissing, ref refMissing,
ref refMissing, ref refMissing);
word.Quit();
Thread.Sleep(500);
word = null;
}
catch (System.Runtime.InteropServices.COMException exception)
{
Console.WriteLine("An error was encountered with file: " + exception.Message);
}
finally
{
if (word != null)
{
word.Quit();
}
word = null;
}
/** Reads TXT **/
String filecontents = "";
try
{
using (StreamReader sr = new StreamReader((String)newRefTargetFile))
{
filecontents += sr.ReadToEnd();
}
}
catch (Exception e)
{
Console.WriteLine("The file could not be read:");
Console.WriteLine(e.Message);
}
/** Inserts TXT back into Database **/
System.Data.SqlClient.SqlCommand cmd = con.CreateCommand();
cmd.CommandText = "Insert into dbo.tb_worddtl_txt values (@filename, @sourcefile, @filetext)";
cmd.CommandType = CommandType.Text;
cmd.CommandTimeout = 60;
System.Data.SqlClient.SqlParameter param = cmd.Parameters.Add("@filename", SqlDbType.Text);
param.Value = file2p.Substring(20).Replace("doc", "txt");
param.Direction = ParameterDirection.Input;
param = cmd.Parameters.Add("@sourcefile", SqlDbType.Text);
param.Direction = ParameterDirection.Input;
param.Value = value;
param = cmd.Parameters.Add("@filetext", SqlDbType.Text);
param.Direction = ParameterDirection.Input;
param.Value = filecontents;
Console.Write("Processed: " + filename + "\r\n");
cmd.ExecuteNonQuery();
}
}
con.Close();
//OdbcConn.Close();
}