//打开文件 public void OpenDocFile(string docName) { wordApp = new ApplicationClass(); wordApp.Visible = false; //所打开的WORD程序,是否是可见的。 object docObject = docName; //由于COM操作中,都是使用的 object ,所以,需要做一些转变 if (File.Exists(docName)) // 如果要打开的文件名存在,那就使用doc来打开 { wordDoc = wordApp.Documents.Add(ref docObject, ref missing, ref missing, ref missing); wordDoc.Activate(); //将当前文件设定为活动文档 } }
public void Print(string filepathandname) { WdSaveFormat format = WdSaveFormat.wdFormatPDF; // Create an instance of Word.exe _Application oWord = new Application { // Make this instance of word invisible (Can still see it in the taskmgr). Visible = false }; // Interop requires objects. object oMissing = System.Reflection.Missing.Value; object isVisible = true; object readOnly = true; // Does not cause any word dialog to show up //object readOnly = false; // Causes a word object dialog to show at the end of the conversion object oInput = filepathandname; object oOutput = filepathandname.Substring(0, filepathandname.Length - 4) + "pdf"; object oFormat = format; object oPrintToFile = true; // Load a document into our instance of word.exe _Document oDoc = oWord.Documents.Open( ref oInput, ref oMissing, ref readOnly, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref isVisible, ref oMissing, ref oMissing, ref oMissing, ref oMissing ); // Make this document the active document. oDoc.Activate(); // Save this document using Word //oDoc.SaveAs(ref oOutput, ref oFormat, ref oMissing, ref oMissing, // ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, // ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing // ); oWord.Visible = false; PrintDialog pDialog = new PrintDialog(); oWord.ActivePrinter = GlobalVariables.PDF_PRINTER; oDoc.PrintOut(ref oMissing, ref oMissing, ref oMissing, ref oOutput, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oPrintToFile, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing); oDoc.Close(SaveChanges: false); oDoc = null; }
/// <summary> /// 打开word文档 /// </summary> /// <param name="fileName"></param> public void OpenFile(string fileName) { _wordApp = new ApplicationClass(); _pathFile = fileName; object readOnly = false; object isVisible = true; _wordDoc = _wordApp.Documents.Open(ref _pathFile, ref nothing, ref readOnly, ref nothing, ref nothing, ref nothing, ref nothing, ref nothing, ref nothing, ref nothing, ref nothing, ref isVisible, ref nothing, ref nothing, ref nothing, ref nothing); _wordDoc.Activate(); }
private static void CloseDocument(_Document oDoc) { oDoc.Close(); if (oDoc != null) { try { oDoc.Activate(); while (System.Runtime.InteropServices.Marshal.ReleaseComObject(oDoc) > 0) { ; } } catch { } finally { oDoc = null; } } }
// Convert method public static void Convert(string input, string output, WdSaveFormat format) { // Create an instance of Word.exe _Application oWord = new Word.Application { // Make this instance of word invisible (Can still see it in the taskmgr). Visible = false }; // Interop requires objects. object oMissing = System.Reflection.Missing.Value; object isVisible = true; object readOnly = true; // Does not cause any word dialog to show up //object readOnly = false; // Causes a word object dialog to show at the end of the conversion object oInput = input; object oOutput = output; object oFormat = format; // Load a document into our instance of word.exe _Document oDoc = oWord.Documents.Open( ref oInput, ref oMissing, ref readOnly, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref isVisible, ref oMissing, ref oMissing, ref oMissing, ref oMissing ); // Make this document the active document. oDoc.Activate(); // Save this document using Word oDoc.SaveAs(ref oOutput, ref oFormat, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing ); // Always close Word.exe. oWord.Quit(ref oMissing, ref oMissing, ref oMissing); }
private object _pathFile; //文件路径 public bool SaveToWorld(string title, string imageLocation, string content) { try { string strTemplateFile = @"D:\bany\夏士安\震情信息.doc"; string strFilePath = @"D:\bany\夏士安\File"; string strFileName = Path.Combine(strFilePath, "震情信息" + DateTime.Now.ToString("yyyyMMddHHmmss") + ".doc"); object oFileName = strFileName; try { if (Directory.Exists(strFilePath) == false) { Directory.CreateDirectory(strFilePath); } if (File.Exists(strFileName)) { File.Delete(strFileName); } File.Copy(strTemplateFile, strFileName, true); } catch (Exception) { return(false); } _wordApp = new ApplicationClass(); object readOnly = false; object isVisible = false; _wordDoc = _wordApp.Documents.Open(ref oFileName, ref nothing, ref readOnly, ref nothing, ref nothing, ref nothing, ref nothing, ref nothing, ref nothing, ref nothing, ref nothing, ref isVisible, ref nothing, ref nothing, ref nothing, ref nothing); _wordDoc.Activate(); object _index = "title"; Bookmark bmTitle = _wordDoc.Bookmarks.get_Item(ref _index); bmTitle.Select(); bmTitle.Range.Font.Name = "宋体"; bmTitle.Range.Font.Size = 16; bmTitle.Range.Text = title; bmTitle.Delete(); object oimage = "image"; Object LinkToFile = false; Object savewithDocument = true; object rangs = _wordApp.ActiveDocument.Bookmarks.get_Item(ref oimage).Range; _wordDoc.InlineShapes.AddPicture(imageLocation, ref LinkToFile, ref savewithDocument, ref rangs); _wordDoc.Application.ActiveDocument.InlineShapes[1].Width = 420; _wordDoc.Application.ActiveDocument.InlineShapes[1].Height = 200; _index = "content"; Bookmark bmContent = _wordDoc.Bookmarks.get_Item(ref _index); bmContent.Select(); bmContent.Range.Font.Name = "楷体"; bmContent.Range.Text = content; bmContent.Delete(); #region 保存word文档 _saveChanges = true; _wordDoc.Close(ref _saveChanges, ref nothing, ref nothing); _wordApp.Quit(ref nothing, ref nothing, ref nothing); _wordDoc = null; _wordApp = null; #endregion } catch (Exception ex) { return(false); } finally { } return(true); }