word to txt

#include "stdafx.h"
#include "HandleWord.h"

#include "msword.h"

HandleWord::HandleWord()
{

}

HandleWord::HandleWord(const char* fileName)
{
    FileName=fileName;
}

HandleWord::~HandleWord()
{

}

void HandleWord::TransFormToTxt()
{
    
    _Application WordApp;
    CoInitialize(NULL);
    if(!WordApp.CreateDispatch("Word.Application",NULL))
    {
        //AfxMessageBox("创建Word服务失败!");
        exit(1);
        return;
    }
    
    WordApp.SetVisible(TRUE);
    
    
    COleVariant vTrue((short)TRUE),vFalse((short)FALSE),vOpt((long)DISP_E_PARAMNOTFOUND, VT_ERROR);

    Documents docs=WordApp.GetDocuments();
    docs.Open(COleVariant(FileName),
        vFalse,vFalse,vFalse,COleVariant(""),COleVariant(""),
        vFalse,COleVariant(""),COleVariant(""),
        COleVariant((short)0),COleVariant("UTF-8")
        ,vTrue,vFalse,COleVariant((short)0),vFalse,
        COleVariant(""));


    _Document active_doc;
    active_doc = WordApp.GetActiveDocument();

    active_doc.SaveAs(COleVariant("D:\\12.txt"),
        COleVariant((short)2),
        vFalse, COleVariant(""),vFalse, COleVariant(""),
        vFalse,vTrue,vFalse,vFalse,vFalse,COleVariant("936"),//COleVariant(L"UTF-8")
        vFalse,vFalse,COleVariant((short)0),vFalse);

    
    WordApp.Quit(vOpt, vOpt, vOpt);
    

}


COleDispatchDriver没有 基类。
COleDispatchDriver类实现OLE自动化中的客户方。OLE调度接口为访问一个对象的方法和属性提供了途径。COleDispatchDriver的成员函数连接,分离,创建和释放一个IDispatch类型的调度连接。其它的成员函数使用 变量 参数列表来简化调用IDispatch::Invoke。
要获得更多的信息,参见“Win32 SDK OLE程序员参考”中的IDispatch和IDispatch::Invoke。
这个类可以直接使用,但一般来说,它只是由用ClassWizard创建的类使用。
当你通过引用一个类型库来创建一个新的C++类时,ClassWizard从COleDispatchDriver派生出新类。
有关使用COleDispatchDriver的更多信息,参见下面列出的“Visual C++程序员指南”一书中的文章:
·
自动化客户
· 自动化服务器
· ClassWizard自动化支持
#include
请参阅: CCmdTarget
COleDispatchDriver成员函数

数据成员编辑

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;  
}  


你可能感兴趣的:(word to txt)