}
·
|
自动化客户 |
· | 自动化服务器 |
· | ClassWizard自动化支持 |
m_bAutoRelease
|
在执行ReleaseDispatch或析构对象时,指定是否要释放Idispatch |
m_lpDispatch | 表示指向附着在这个COleDispatchDriver的IDispatch接口的指针 |
COleDispatchDriver
|
构造一个COleDispatchDriver对象 |
CreateDispatch
|
创建一个IDispatch连接并将它附着于COleDispatchDriver对象 |
AttachDispatch | 将一个IDispatch连接附着于COleDispatchDriver对象 |
DetachDispatch | 分开一个IDispatch连接,但并不释放它 |
ReleaseDispatch | 释放一个IDispatch连接 |
InvokeHelper | 用于调用自动化方法的助手 |
SetProperty | 设置一个自动化特性 |
GetProperty | 获得一个自动化特性 |
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.IO;
using Microsoft.Office.Interop.Word;
namespace WebApplication2
{
public class Totif
{
string tifPath = null;
//设置生成的tif文件路径
//路径名的命名规则为无后缀名,否则调用 varDoc.PrintOut方法时会打开打印预览窗体
public Totif(string buildTifPath)
{
this.tifPath = buildTifPath;
}
/**/
/// < summary>
/// 生成无后缀名的tif文件
/// < /summary>
/// < param name="sourceFile">源WORD文件< /param>
/// < param name="destiFile">< /param>
public void WordToTiff(object sourceFile)
{
object varMissing = Type.Missing;
try
{
// Writetxt(sourceFile.ToString());
if (sourceFile.ToString().IndexOf("~$") != -1)
{
return;
}
//生成的tif路径
object OutputFileName = (object)(tifPath+sourceFile.ToString().Split('\\')[sourceFile.ToString().Split('\\').Length -1].Split('.')[0]+".tif");
//定义Word实例对象
Microsoft.Office.Interop.Word.Application varWord = new Microsoft.Office.Interop.Word.Application();
object varFalseValue = false;
//设置Word的虚拟打印机
varWord.ActivePrinter = "Fax";
// 引用到Word文件
Microsoft.Office.Interop.Word.Document varDoc = varWord.Documents.Open(ref sourceFile, ref varMissing, ref varFalseValue,
ref varMissing, ref varMissing, ref varMissing, ref varMissing, ref varMissing, ref varMissing, ref varMissing,
ref varMissing, ref varMissing, ref varMissing, ref varMissing, ref varMissing, ref varMissing);
varDoc.Activate(); //激活文档
object PrintToFile = true;
//打印成tif文件
varDoc.PrintOut(ref varMissing, ref varMissing, ref varMissing, ref OutputFileName, ref varMissing,
ref varMissing, ref varMissing, ref varMissing, ref varMissing, ref varMissing, ref PrintToFile,
ref varMissing, ref varMissing, ref varMissing, ref varMissing, ref varMissing, ref varMissing, ref varMissing);
object saveChange = WdSaveOptions.wdSaveChanges; //关闭word文档时不打开新窗体
varDoc.Close(ref saveChange, ref varMissing, ref varMissing); //关闭word文档
varWord.Quit(ref varMissing, ref varMissing, ref varMissing); //退出Word
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
}
///
/// 生成无后缀名的tif文件
///
/// 源Excel文件
public void ExcelToTif(object sourceFile)
{
try
{
//生成的tif路径
object OutputFileName = (object)(tifPath + sourceFile.ToString().Split('\\')[sourceFile.ToString().Split('\\').Length - 1].Split('.')[0] + ".tif");
object varMissing = Type.Missing;
//定义Word实例对象
Microsoft.Office.Interop.Excel.Application varWord = new Microsoft.Office.Interop.Excel.Application();
Microsoft.Office.Interop.Excel.Workbook xBook = varWord.Workbooks._Open(sourceFile.ToString(),
varMissing, varMissing, varMissing, varMissing
, varMissing, varMissing, varMissing, varMissing, varMissing,
varMissing, varMissing, varMissing);
xBook.Activate();
object PrintToFile = true;
//打印成tif文件
xBook.PrintOut(varMissing, varMissing, varMissing, varMissing,
"FAX", PrintToFile, varMissing, OutputFileName);
xBook.Close(false, false, varMissing);
varWord.Quit();
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
}
}
}
//#include "stdafx.h"
#include "stdio.h"
// TODO: reference any additional headers you need in STDAFX.H
// and not in this file
//#include "Detours.h"
#using "WebApplication2.dll"
using namespace WebApplication2;
__declspec(dllexport) int doc2tif(char*docpath)
{
Totif ^c = gcnew Totif("c:\\tiff");
System::String ^strval1 = gcnew System::String(docpath);
c->WordToTiff(strval1);
//c->Name = \"zzj\";
// printf(\"%s\\n\", c->Name);
return 0;
}