137 lines
3.6 KiB
C#
137 lines
3.6 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Text;
|
|
using System.Reflection;
|
|
using Microsoft.Office.Interop;
|
|
using Microsoft.Office.Interop.Word;
|
|
|
|
namespace Volian.MSWord
|
|
{
|
|
public class WordDoc
|
|
{
|
|
private static object optional = Missing.Value;
|
|
private static object oFalse = false;
|
|
private object oTrue = true;
|
|
private object oBlank = "";
|
|
private object oWdStory = WdUnits.wdStory;
|
|
private object oWdWord = WdUnits.wdWord;
|
|
private object oWdChar = WdUnits.wdCharacter;
|
|
private object oWdLine = WdUnits.wdLine;
|
|
private object oWdPara = WdUnits.wdParagraph;
|
|
private object oWdExtend = WdMovementType.wdExtend;
|
|
private object oWdMove = WdMovementType.wdMove;
|
|
private object o1 = 1;
|
|
private static Application m_App;
|
|
// Microsoft.Office.Interop.Word.Application App
|
|
// {
|
|
// get{return m_App;}
|
|
// }
|
|
private Document m_Doc;
|
|
// Microsoft.Office.Interop.Word.Document Doc
|
|
// {
|
|
// get{return m_Doc;}
|
|
// }
|
|
private string mDocName;
|
|
|
|
public string DocName
|
|
{
|
|
get { return mDocName; }
|
|
set { mDocName = value; }
|
|
}
|
|
public WordDoc(string sPath)
|
|
{
|
|
OpenApp();
|
|
//m_App.Visible = true;
|
|
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()
|
|
//{
|
|
// 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
|
|
{
|
|
m_Doc.SaveAs2000(
|
|
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";
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
return string.Format("Error during Doc save - {0}", ex.Message);
|
|
}
|
|
}
|
|
public string Save()
|
|
{
|
|
return SaveDoc();
|
|
}
|
|
public string Save(string sPath)
|
|
{
|
|
mDocName = sPath;
|
|
return SaveDoc();
|
|
}
|
|
public void Close()
|
|
{
|
|
m_Doc.Close(ref oFalse, ref optional, ref optional);
|
|
//CloseApp();
|
|
}
|
|
public void SetLineSpacing(float linesPerInch) // if need landscape set too: , bool landscape)
|
|
{
|
|
Selection sel = m_App.Selection;
|
|
sel.WholeStory();
|
|
// if need landscape set too: if (landscape) sel.PageSetup.Orientation = WdOrientation.wdOrientLandscape;
|
|
sel.ParagraphFormat.SpaceBefore=0;
|
|
sel.ParagraphFormat.SpaceAfter = 0;
|
|
sel.ParagraphFormat.LineSpacingRule = WdLineSpacing.wdLineSpaceExactly;
|
|
sel.ParagraphFormat.LineSpacing = 72/linesPerInch; // 12 ==> 6 Lines per inch
|
|
}
|
|
public void OpenApp()
|
|
{
|
|
if(m_App==null)
|
|
m_App = new ApplicationClass();
|
|
}
|
|
public static void CloseApp()
|
|
{
|
|
if (m_App != null)
|
|
{
|
|
m_App.Quit(ref oFalse, ref optional, ref optional);
|
|
m_App = null;
|
|
}
|
|
}
|
|
}
|
|
}
|