Changed to use Late Binding (LBWordLibrary)

This commit is contained in:
Rich 2010-02-02 21:01:07 +00:00
parent 673acb6ecd
commit 8dcc308f66

View File

@ -1,93 +1,44 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Text; using System.Text;
using System.Reflection; using LBWordLibrary;
using Microsoft.Office.Interop;
using Microsoft.Office.Interop.Word;
namespace Volian.MSWord namespace Volian.MSWord
{ {
public class WordDoc public class WordDoc
{ {
private static object optional = Missing.Value; private static LBApplicationClass _MyWordApp;
private static object oFalse = false; public static LBApplicationClass MyWordApp
private object oTrue = true; {
private object oBlank = ""; get
private object oWdStory = WdUnits.wdStory; {
private object oWdWord = WdUnits.wdWord; if (_MyWordApp == null)
private object oWdChar = WdUnits.wdCharacter; _MyWordApp = new LBApplicationClass();
private object oWdLine = WdUnits.wdLine; return WordDoc._MyWordApp;
private object oWdPara = WdUnits.wdParagraph; }
private object oWdExtend = WdMovementType.wdExtend; }
private object oWdMove = WdMovementType.wdMove; private LBDocumentClass _MyWordDoc;
private object o1 = 1; public LBDocumentClass MyWordDoc
private static Application m_App; {
// Microsoft.Office.Interop.Word.Application App get
// { { return _MyWordDoc; }
// get{return m_App;} }
// } private string _DocName;
private Document m_Doc;
// Microsoft.Office.Interop.Word.Document Doc
// {
// get{return m_Doc;}
// }
private string mDocName;
public string DocName public string DocName
{ {
get { return mDocName; } get { return _DocName; }
set { mDocName = value; } set { _DocName = value; }
} }
public WordDoc(string sPath) public WordDoc(string docName)
{ {
OpenApp(); DocName = docName;
//m_App.Visible = true; _MyWordDoc = MyWordApp.Documents.Open(_DocName, false);
mDocName = sPath;
object oFile = sPath;
m_Doc = m_App.Documents.Open(
ref oFile // FileName
,ref optional // ConfirmConversions
,ref optional // ReadOnly
,ref optional // AddToRecentFiles
,ref optional // PasswordDocument
,ref optional // PasswordTemplate
,ref optional // Revert
,ref optional // WritePasswordDocument
,ref optional // WritePasswordTemplate
,ref optional // Format
,ref optional // Encoding
,ref optional // Visible
,ref optional // OpenAndRepair
,ref optional // DocumentDirection
,ref optional // NoEncodingDialog
,ref optional // XMLTransform
);
} }
//~WordDoc() public string Save()
//{
// m_App.Quit(ref oFalse, ref optional, ref optional);
//}
private string SaveDoc()
{ {
object oFileName = mDocName;
object oFileFormat = Microsoft.Office.Interop.Word.WdSaveFormat.wdFormatDocument;
//object oFileFormat = Microsoft.Office.Interop.Word.WdSaveFormat.wdFormatFilteredHTML;
object oPassWord = "";
try try
{ {
m_Doc.SaveAs2000( MyWordDoc.SaveAs(DocName, LBWdSaveFormat.wdFormatDocument);
ref oFileName // FileName
,ref oFileFormat // FileFormat
,ref oFalse // LockComments
,ref oPassWord // Password
,ref oFalse // AddToRecentFiles
,ref oPassWord // WritePassword
,ref oFalse // ReadOnlyRecommended
,ref oFalse // EmbedTrueTypeFonts
,ref oFalse // SaveNativePictureFormat
,ref oFalse // SaveFormsData
,ref oFalse // SaveAsAOCELetter
);
return "Successful Save"; return "Successful Save";
} }
catch (Exception ex) catch (Exception ex)
@ -95,41 +46,34 @@ namespace Volian.MSWord
return string.Format("Error during Doc save - {0}", ex.Message); return string.Format("Error during Doc save - {0}", ex.Message);
} }
} }
public string Save() public string Save(string docName)
{ {
return SaveDoc(); DocName = docName;
} return Save();
public string Save(string sPath)
{
mDocName = sPath;
return SaveDoc();
} }
public void Close() public void Close()
{ {
m_Doc.Close(ref oFalse, ref optional, ref optional); MyWordDoc.Close(false);
//CloseApp();
} }
public void SetLineSpacing(float linesPerInch) // if need landscape set too: , bool landscape) public void SetLineSpacing(float linesPerInch) // if need landscape set too: , bool landscape)
{ {
Selection sel = m_App.Selection; LBSelection selection = MyWordApp.Selection;
sel.WholeStory(); selection.WholeStory();
// if need landscape set too: if (landscape) sel.PageSetup.Orientation = WdOrientation.wdOrientLandscape; selection.ParagraphFormat.SpaceBefore = 0;
sel.ParagraphFormat.SpaceBefore=0; selection.ParagraphFormat.SpaceAfter = 0;
sel.ParagraphFormat.SpaceAfter = 0; selection.ParagraphFormat.LineSpacingRule = LBWdLineSpacing.wdLineSpaceExactly;
sel.ParagraphFormat.LineSpacingRule = WdLineSpacing.wdLineSpaceExactly; selection.ParagraphFormat.LineSpacing = 72 / linesPerInch;
sel.ParagraphFormat.LineSpacing = 72/linesPerInch; // 12 ==> 6 Lines per inch
} }
public void OpenApp() public float Length
{ {
if(m_App==null) get { return MyWordDoc.Length; }
m_App = new ApplicationClass();
} }
public static void CloseApp() public static void CloseApp()
{ {
if (m_App != null) if(_MyWordApp != null)
{ {
m_App.Quit(ref oFalse, ref optional, ref optional); _MyWordApp.Quit(false);
m_App = null; _MyWordApp=null;
} }
} }
} }