Пример #1
0
        public static void SaveWordDoc(string originFile, IEnumerable<KeyValuePair<string, string>> replaceList = null, string targetFile = null, WdSaveFormat format = WdSaveFormat.wdFormatDocumentDefault, MsoEncoding encoding = MsoEncoding.msoEncodingAutoDetect)
        {
            Word.Application ap = null;
            Word.Document doc = null;
            object missing = Type.Missing;
            bool success = true;
            replaceList = replaceList ?? new Dictionary<string, string>();
            targetFile = targetFile ?? originFile;
            if(targetFile.LastIndexOf('.')>targetFile.LastIndexOf('/')||targetFile.LastIndexOf('.')>targetFile.LastIndexOf('\\'))
                targetFile=targetFile.Remove(targetFile.LastIndexOf('.'));

            try
            {
                ap = new Word.Application();
                ap.DisplayAlerts = WdAlertLevel.wdAlertsNone;
                doc = ap.Documents.Open(originFile, ReadOnly: false, Visible: false);
                doc.Activate();

                Selection sel = ap.Selection;

                if (sel == null)
                    throw new Exception("Unable to acquire Selection...no writing to document done..");

                switch (sel.Type)
                {
                    case WdSelectionType.wdSelectionIP:
                        replaceList.ToList().ForEach(p => sel.Find.Execute(FindText: p.Key, ReplaceWith: p.Value, Replace: WdReplace.wdReplaceAll));
                        break;
                    default:
                        throw new Exception("Selection type not handled; no writing done");
                }

                sel.Paragraphs.LineUnitAfter = 0;
                sel.Paragraphs.LineUnitBefore = 0;
                sel.Paragraphs.LineSpacingRule = WdLineSpacing.wdLineSpaceSingle;

                doc.SaveSubsetFonts = false;
                doc.SaveAs(targetFile, format, Encoding: encoding);

            }
            catch (Exception)
            {
                success = false;
            }
            finally
            {
                if (doc != null)
                {
                    doc.Close(ref missing, ref missing, ref missing);
                    Marshal.ReleaseComObject(doc);
                }
                if (ap != null)
                {
                    ap.Quit(ref missing, ref missing, ref missing);
                    Marshal.ReleaseComObject(ap);
                }
                if(!success)
                    throw new Exception(); // Could be that the document is already open (/) or Word is in Memory(?)
            }
        }
Пример #2
0
 public void ReloadAs(MsoEncoding Encoding)
 {
     throw new System.NotImplementedException();
 }
 public void ReloadAs(MsoEncoding encoding)
 {
     throw new NotImplementedException();
 }
Пример #4
0
 public void ReloadAs(MsoEncoding Encoding)
 {
     throw new System.NotImplementedException();
 }
Пример #5
0
 public void ReloadAs(MsoEncoding encoding)
 {
     throw new NotImplementedException();
 }
        /// <summary>使用指定的文档编码方式,重新加载基于 HTML 文档的工作簿。
        /// </summary>
        /// <param name="Encoding">要应用于工作簿的编码。</param>
        public void ReloadAs(MsoEncoding Encoding)
        {
            _objaParameters = new object[1] { Encoding };

            _objWorkbook.GetType().InvokeMember("ReloadAs", BindingFlags.InvokeMethod, null, _objWorkbook, _objaParameters);
        }