【问题标题】:How to remove symbols in a word document如何去除word文档中的符号
【发布时间】:2011-05-26 07:30:37
【问题描述】:

我正在使用 word api(c# 2.0 应用程序)以编程方式将 word 文档转换为纯文本文件。对于某些文档,由于 word 文档中可用的符号,该过程处于挂起状态。我想以编程方式删除 word 文档中的那些符号,或者如何将 word 文档另存为包含符号而不挂起的纯文本文件。

请帮我解决问题

这里是示例代码

private void TextFileConvertion(string strsource, string strtarget)
        {
            // Use for the parameter whose type are not known or  
            // say Missing
            object Unknown = Type.Missing;

            //Creating the instance of Word Application
            Word.Application newApp = new Word.Application();
            newApp.AutomationSecurity = Microsoft.Office.Core.MsoAutomationSecurity.msoAutomationSecurityForceDisable;
            newApp.DisplayAlerts = Word.WdAlertLevel.wdAlertsNone;

            Word.Document doc = null;
            try
            {
                lblProgress.Text = "Converting " + strsource + " into Text file is under process.";
                Application.DoEvents();
                // specifying the Source & Target file names
                object Source = strsource;
                object Target = strtarget;
                object objTrue = true;
                object objFalse = false;
                // Source document open here
                // Additional Parameters are not known so that are  
                // set as a missing type

                try
                {
                    newApp.Visible = false;

                    doc = newApp.Documents.Open(ref Source,
                         ref Unknown, ref Unknown, ref Unknown,
                         ref Unknown, ref Unknown, ref Unknown,
                         ref Unknown, ref Unknown, ref Unknown,
                         ref Unknown, ref Unknown, ref Unknown,
                         ref Unknown, ref Unknown, ref Unknown);
                }
                catch (Exception exp)
                {
                    ZoniacLogger.Error("Exception : " + exp.Message + " Stack Trace : " + exp.StackTrace);
                }

                if (doc.ReadOnlyRecommended == true)
                    return;
                // Specifying the format in which you want the output file 
                object format = Word.WdSaveFormat.wdFormatText;

                //Changing the format of the document
                newApp.ActiveDocument.SaveAs(ref Target, ref format,
                        ref Unknown, ref Unknown, ref Unknown,
                        ref Unknown, ref Unknown, ref Unknown,
                        ref Unknown, ref Unknown, ref Unknown,
                        ref Unknown, ref Unknown, ref Unknown,
                        ref Unknown, ref Unknown);

                //if (doc.ReadOnlyRecommended == true)
                //    SetuncheckReadonly(doc, strsource);
                //intTxtCounter = intTxtCounter + 1;
                strTxtCounter = "OK";
            }
            catch (Exception ex)
            {
                strTxtCounter = "FAILED";
                ZoniacLogger.Error("<TextFileConvertion> Exception : " + ex.Message + " Stack Trace : " + ex.StackTrace);
            }
            finally
            {
                if (newApp != null)
                {
                    // for closing the application
                    newApp.Quit(ref Unknown, ref Unknown, ref Unknown);
                    newApp = null;
                }
            }
        }

【问题讨论】:

  • “可用符号”:这是什么意思?你有任何示例文档吗?
  • @alexD 和@Kamyar:我已经在我的问题中给出了代码示例

标签: c# ms-word


【解决方案1】:

如果您只需要将单词保存为纯文本,那么最快和最简单的方法就是使用 DocumentClass 的 SaveAs 方法?

Document.SaveAs on MSDN

您只需将适当的输出格式设置为第二个参数。 Save formats

【讨论】:

    猜你喜欢
    • 2013-05-06
    • 2015-12-29
    • 2011-01-19
    • 1970-01-01
    • 2018-11-11
    • 2017-07-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多