如何在后台运行程序模拟RETURN(ENTER)命中

时间:2014-12-13 04:16:46

标签: c#

我编写了一个C#程序,将1997年至2003年的.DOC Word文档转换为TEXT FILES。我从命令行运行程序。我必须转换超过300,000个文件。每个文档需要0.7到1.5秒才能处理。该程序工作正常。我正在使用图书馆:

  • 的Microsoft.Office.Interop.Word

这里的问题是很少Word文档被破坏,有些已启用宏,有些是锁定编辑等。我能够处理大部分错误,除了一个。

我可以在此链接上找到类似的问题(除了我转换为TXT的事实):Microsoft Interop saveAs command failed

这是我的问题;我每25-35分钟就会收到一次错误。在该库中有一些东西可以捕获错误并生成一个POP-UP说"文件遇到错误。命令失败",并且弹出窗口只有一个“确定”'按钮。

当弹出该警告消息窗口时,程序会暂停,直到有人手动点击“退回”,或点击“确定”状态。按钮。

如何编写在后台运行的程序并模拟某人按下RETURN按钮?这样,命令行中生成的弹出窗口将消失,我的程序将恢复。

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();
        }

2 个答案:

答案 0 :(得分:1)

您可以使用SendKeys类

SendKeys.Send("{ENTER}");

您可以查看msdn以获取更多信息

答案 1 :(得分:1)

我弄明白了这个问题。尝试使用word.Quit();退出Word实例后,进程WINWORD.EXE仍在后台运行。一些Word文档有VB宏,其他一些使用MS Word扩展名(.dot),导致WINWORD.EXE流程实例不被释放,有些情况下它生成弹出窗口(如上面描述的那些,以及其他一些喜欢&#34;你想修改.dot扩展名&#34;)。此外,当我实际打开Word时,我有文件&#34;从上一次会话中保存&#34;。基本上,在退出MS Word后使用行Marshal.ReleaseComObject(documents);时,我的所有问题都消失了。这一行发布了该迭代的WINWORD.EXE实例进程,我的程序运行得非常流畅。

finally
{
    if (word != null)
    {
        word.Quit();
    }
    word = null;
}

Marshal.ReleaseComObject(documents);