日韩久久久精品,亚洲精品久久久久久久久久久,亚洲欧美一区二区三区国产精品 ,一区二区福利

VC操縱Word

系統(tǒng) 1884 0

我們操縱Word需要通過類型庫(kù)中的MFC類。而這些類,應(yīng)該都是基于一個(gè)叫COleDispatchDriver的類。至少我所了解到的都是這樣。

COleDispatchDriver沒有基類。COleDispatchDriver類實(shí)現(xiàn)OLE自動(dòng)化中的客戶方。OLE調(diào)度接口為訪問一個(gè)對(duì) 象的方法和屬性提供了途徑。COleDispatchDriver的成員函數(shù)連接,分離,創(chuàng)建和釋放一個(gè)IDispatch類型的調(diào)度連接。其它的成員函 數(shù)使用變量參數(shù)列表來簡(jiǎn)化調(diào)用IDispatch::Invoke。

學(xué)習(xí)如何自動(dòng)化控制 Word、Excel 和 Powerpoint 的對(duì)象模型的最佳方法是使用這些 Office 應(yīng)用程序中的宏錄制器:

  1. 工具 ?菜單上的 ?選項(xiàng)中選擇 錄制新宏 ?,然后執(zhí)行您感興趣的任務(wù)。
  2. 工具 ?菜單上的 ?選項(xiàng)中選擇 停止錄制 ?。
  3. 完成錄制后,從 工具 ?菜單上的 ?選項(xiàng)中選擇 ?,選擇您錄制的宏,然后單擊 編輯 ?。

您將看到生成的 VBA 代碼,該代碼可完成您所錄制的任務(wù)。記住,錄制的宏在大多數(shù)情況下并 ?是最佳代碼,但它可以提供快捷可用的示例。

Application ?:代表 Microsoft Word 應(yīng)用程序。Application 對(duì)象包含可返回最高級(jí)對(duì)象的屬性和方法。例如,ActiveDocument 屬性可返回當(dāng)前活動(dòng)的Document 對(duì)象。

Documents ?:由 Word 當(dāng)前打開的所有 Document(文檔) 對(duì)象所組成的集合。

Document ?:代表一篇文檔。Document 對(duì)象是 Documents 集合中的一個(gè)元素。Documents 集合包含 Word 當(dāng)前打開的所有 Document 對(duì)象。

Selection: ?該對(duì)象代表窗口或窗格中的當(dāng)前所選內(nèi)容。所選內(nèi)容代表文檔中被選定(或突出顯示的)的區(qū)域,若文檔中沒有所選內(nèi)容,則代表插入點(diǎn)。每個(gè)文檔窗格只能有一個(gè)活動(dòng)的 Selection 對(duì)象,并且整個(gè)應(yīng)用程序中只能有一個(gè)活動(dòng)的 Selection 對(duì)象。

例子1 ?:

?

      #include 
      
        "
      
      
        msword9.h
      
      
        "
      
      
        //
      
      
        為了使代碼集中,方便閱讀,所以把頭文件放到了這里
      
      
        void
      
      
         CStep1Dlg::OnOK() 

{

_Application app; 
      
      
        //
      
      
        定義一個(gè)WORD的應(yīng)用對(duì)象
      
      
        if
      
      (!app.CreateDispatch(_T(
      
        "
      
      
        Word.Application
      
      
        "
      
      ))) 
      
        //
      
      
        啟動(dòng)WORD
      
      
        {

   AfxMessageBox(_T(
      
      
        "
      
      
        居然你連OFFICE都沒有安裝嗎?
      
      
        "
      
      
        ));

   
      
      
        return
      
      
        ;

}

AfxMessageBox(_T(
      
      
        "
      
      
        WORD 已經(jīng)運(yùn)行啟動(dòng)啦,你可以用Ctrl+Alt+Del查看
      
      
        "
      
      
        ));

app.SetVisible(TRUE); 
      
      
        //
      
      
        設(shè)置WORD可見。

        
      
      
        //
      
      
        當(dāng)然,如果你想要悄悄地調(diào)用WORD的功能,則注釋掉這條語(yǔ)句
      
      

AfxMessageBox(_T(
      
        "
      
      
        現(xiàn)在你已經(jīng)看到WORD的程序界面了吧
      
      
        "
      
      
        ));

AfxMessageBox(_T(
      
      
        "
      
      
        WORD準(zhǔn)備要退出啦
      
      
        "
      
      
        ));

VARIANT SaveChanges,OriginalFormat,RouteDocument; 
      
      
        //
      
      
        定義調(diào)用QUIT時(shí)使用的參數(shù)
      
      

SaveChanges.vt=VT_BOOL;     
      
        //
      
      
        設(shè)置退出WORD時(shí)候的保存參數(shù)
      
      

SaveChanges.boolVal=VARIANT_FALSE; 
      
        //
      
      
        為不保存任何文檔,模板及設(shè)置
      
      
        

::VariantInit(
      
      &OriginalFormat);   
      
        //
      
      
        清空變量
      
      

RouteDocument.vt=VT_EMPTY;    
      
        //
      
      
        清空變量的另一種方法


      
      
        //
      
      
        調(diào)用Quit退出WORD應(yīng)用程序。當(dāng)然不調(diào)用也可以,那樣的話WORD還在運(yùn)行著那
      
      

app.Quit(&SaveChanges,&OriginalFormat,&
      
        RouteDocument);

app.ReleaseDispatch();   
      
      
        //
      
      
        釋放對(duì)象指針。切記,必須調(diào)用
      
      
        

AfxMessageBox(_T(
      
      
        "
      
      
        Step1執(zhí)行完成。接著請(qǐng)學(xué)習(xí)Setp2
      
      
        "
      
      
        ));

}
      
    

?

例子2:

?

      #include 
      
        "
      
      
        msword9.h
      
      
        "
      
      
        

#include 
      
      <AtlBase.h> 
      
        //
      
      
        新增加了一個(gè)頭文件,為使用CComVariant替代VARIANT做準(zhǔn)備
      
      
        void
      
      
         CStep2Dlg::OnOK() 

{


      
      
        //
      
      
        以下3行代碼,同Step1。就不解釋啦
      
      
        _Application app;


      
      
        //
      
      
        為了簡(jiǎn)單,沒有判斷返回值。如果沒有成功,記得檢查你有沒有AfxOleInit()呀?
      
      

app.CreateDispatch(_T(
      
        "
      
      
        Word.Application
      
      
        "
      
      
        ));

app.SetVisible(TRUE);      

AfxMessageBox(_T(
      
      
        "
      
      
        WORD已經(jīng)啟動(dòng),現(xiàn)在要退出啦
      
      
        "
      
      
        ));

AfxMessageBox(_T(
      
      
        "
      
      
        怎么和Step1沒有什么區(qū)別呀?
      
      
        "
      
      
        ));

AfxMessageBox(_T(
      
      
        "
      
      
        嘿嘿,是沒什么區(qū)別,但是使用方式簡(jiǎn)單了很多呀。看看源程序吧
      
      
        "
      
      
        ));


      
      
        //
      
      
        準(zhǔn)備調(diào)用_Application::Quit函數(shù)了,需要定義3個(gè)參數(shù)。


      
      
        //
      
      
        但是,這次我們使用CComVariant,這是一個(gè)模板類。


      
      
        //
      
      
        在定義的時(shí)候直接調(diào)用帶參數(shù)的構(gòu)造函數(shù),比VARIANT使用簡(jiǎn)單多了吧
      
      

CComVariant SaveChanges(
      
        false
      
      
        ),OriginalFormat,RouteDocument;


      
      
        //
      
      
        使用 CComVariant 的不帶參數(shù)的構(gòu)造函數(shù),默認(rèn)就是使用VT_EMPTY,設(shè)置為空類型


      
      
        //
      
      
        另外,除了CComVariant,你還可以使用COleVariant和_variant_t,但我個(gè)人最喜歡前者
      
      

app.Quit(&SaveChanges,&OriginalFormat,&
      
        RouteDocument);

app.ReleaseDispatch();

AfxMessageBox(_T(
      
      
        "
      
      
        下面該學(xué)習(xí)Setp3了
      
      
        "
      
      
        ));

}
      
    

?

例子3:

      #include 
      
        "
      
      
        msword9.h
      
      
        "
      
      
        

#include 
      
      <AtlBase.h>




      
        void
      
      
         CStep3Dlg::OnOK() 

{


      
      
        ////////////
      
      
        // 這次,我們要控制在WORD中輸入一些字符了 
      
      
        /////////////////////


      
      
        /*
      
      
        ************ WORD 錄制的宏,新建一個(gè)空文檔,然后輸入一些文字 ************

    Documents.Add Template:= _

        "C:/Documents and Settings/Administrator/Application Data/Microsoft/Templates/Normal.dot" _

        , NewTemplate:=False, DocumentType:=0

    Selection.TypeText Text:="HELLO"

    Selection.TypeParagraph

    Selection.TypeText Text:="大家好"

**************************************************************************
      
      
        */
      
      
        

_Application app;

app.CreateDispatch(_T(
      
      
        "
      
      
        Word.Application
      
      
        "
      
      
        ));

app.SetVisible(TRUE);

AfxMessageBox(_T(
      
      
        "
      
      
        看好了,就要新建一個(gè)空白文檔了
      
      
        "
      
      
        ));


      
      
        //
      
      
        通過WORD宏可以知道,由于要使用Documents,于是我們定義一個(gè)并從app中取得
      
      

Documents docs=
      
        app.GetDocuments();


      
      
        //
      
      
        準(zhǔn)備調(diào)用Documents::Add函數(shù)了,需要定義4個(gè)參數(shù)。


      
      
        //
      
      
        從WORD宏可以看出來3個(gè)參數(shù)的類型為:


      
      
        //
      
      
        Template字符,NewTemplate布爾,DocumentType數(shù)值


      
      
        //
      
      
        但Add函數(shù)還需要一個(gè)參數(shù)是Visible,傻子也能看出來這個(gè)值表示是否顯示出新文檔


      
      
        //
      
      
        并且可以給默認(rèn)值(VT_EMPTY)
      
      

CComVariant Template(_T(
      
        ""
      
      )); 
      
        //
      
      
        為了簡(jiǎn)單,沒有使用WORD的文檔模板
      
      

CComVariant NewTemplate(
      
        false
      
      ),DocumentType(
      
        0
      
      
        ),Visible;

docs.Add(
      
      &Template,&NewTemplate,&DocumentType,&
      
        Visible);

AfxMessageBox(_T(
      
      
        "
      
      
        下面,程序要向WORD發(fā)送字符啦
      
      
        "
      
      
        ));


      
      
        //
      
      
        通過WORD宏可以知道,由于要使用Selection,于是我們定義一個(gè)并從app中取得


      
      
        //
      
      
        Selection表示輸入點(diǎn),即光標(biāo)閃爍的那個(gè)地方
      
      

Selection sel=
      
        app.GetSelection();


      
      
        //
      
      
        調(diào)用函數(shù)Selection::TypeText 向WORD發(fā)送字符
      
      

sel.TypeText(_T(
      
        "
      
      
        HELLO/r/n大家好呀
      
      
        "
      
      
        ));

AfxMessageBox(_T(
      
      
        "
      
      
        看見了嗎?我要退出啦
      
      
        "
      
      
        ));

sel.ReleaseDispatch();   
      
      
        //
      
      
        Selection 不用了,一定要釋放
      
      

docs.ReleaseDispatch();   
      
        //
      
      
        Documents 也不用了
      
      
        

CComVariant SaveChanges(
      
      
        false
      
      
        ),OriginalFormat,RouteDocument;

app.Quit(
      
      &SaveChanges,&OriginalFormat,&
      
        RouteDocument);

app.ReleaseDispatch();

AfxMessageBox(_T(
      
      
        "
      
      
        下面該學(xué)習(xí)Setp4了
      
      
        "
      
      
        ));

}
      
    

?

?

例子4:

      #include 
      
        "
      
      
        msword9.h
      
      
        "
      
      
        

#include 
      
      <AtlBase.h>




      
        void
      
      
         CStep4Dlg::OnOK() 

{

_Application app;

app.CreateDispatch(_T(
      
      
        "
      
      
        Word.Application
      
      
        "
      
      
        ));

app.SetVisible(TRUE);

Documents docs
      
      =
      
        app.GetDocuments();

CComVariant Template(_T(
      
      
        ""
      
      
        ));

CComVariant NewTemplate(
      
      
        false
      
      ),DocumentType(
      
        0
      
      
        ),Visible;

docs.Add(
      
      &Template,&NewTemplate,&DocumentType,&
      
        Visible);

Selection sel
      
      =
      
        app.GetSelection();

COleVariant varstrRange(
      
      
        ""
      
      
        );

COleVariant varConfirmConversions(
      
      
        short
      
      (
      
        0
      
      
        ),VT_BOOL);

COleVariant varLink(
      
      
        short
      
      (
      
        0
      
      
        ),VT_BOOL);

COleVariant varAttachment(
      
      
        short
      
      (
      
        0
      
      
        ),VT_BOOL);

sel.InsertFile(
      
      
        "
      
      
        C://My Project//WordOperator//doc//fjjb.doc
      
      
        "
      
      
        ,varstrRange,varConfirmConversions,varLink,varAttachment);

sel.MoveUp(COleVariant((
      
      
        short
      
      )
      
        5
      
      ),COleVariant((
      
        short
      
      )
      
        2
      
      ),COleVariant((
      
        short
      
      )
      
        0
      
      
        ));

sel.TypeText(
      
      
        "
      
      
        123456789 
      
      
        "
      
      
        );

sel.MoveRight(COleVariant((
      
      
        short
      
      )
      
        12
      
      ),COleVariant((
      
        short
      
      )
      
        1
      
      ),COleVariant((
      
        short
      
      )
      
        0
      
      
        ));

sel.TypeText(_T(
      
      
        "
      
      
        HELLO
      
      
        "
      
      
        ));

sel.MoveRight(COleVariant((
      
      
        short
      
      )
      
        1
      
      ),COleVariant((
      
        short
      
      )
      
        1
      
      ),COleVariant((
      
        short
      
      )
      
        0
      
      
        ));

sel.TypeText(
      
      
        "
      
      
        123456789
      
      
        "
      
      
        );

AfxMessageBox(_T(
      
      
        "
      
      
        好了,我要保存到c://hello.doc中了
      
      
        "
      
      
        ));


      
      
        /*
      
      
        *************** 這是在WORD中錄制的新建文檔直到另存的宏 *************

    Documents.Add Template:= _

        "C:/Documents and Settings/Administrator/Application Data/Microsoft/Templates/Normal.dot" _

        , NewTemplate:=False, DocumentType:=0

    Selection.TypeText Text:="Hello"

    Selection.TypeParagraph

    Selection.TypeText Text:="大家好"

    ChangeFileOpenDirectory "C:/"

    ActiveDocument.SaveAs FileName:="Hello.doc", FileFormat:=wdFormatDocument _

        , LockComments:=False, Password:="", AddToRecentFiles:=True, _

        WritePassword:="", ReadOnlyRecommended:=False, EmbedTrueTypeFonts:=False, _

         SaveNativePictureFormat:=False, SaveFormsData:=False, SaveAsAOCELetter:= _

        False

********************************************************************
      
      
        */
      
      
        /*
      
      
        *************** 程序思路 ******************************************

另存為的函數(shù)是ActiveDocument.SaveAs,顯然表示的是對(duì)當(dāng)前活躍的文檔進(jìn)行保存,

在我們的類中沒有ActiveDocument,其實(shí)它對(duì)應(yīng)的是_Document,而這個(gè)可以由

_Application 的GetActiveDocument()得到。你一定會(huì)提問:“你怎么知道的?”

呵呵,怎么說那,我怎么知道的那?答案是:猜。其實(shí)如果使用的多了,分析、猜

查找都是辦法。如果想得到確切的方法,其實(shí)可以在VBA的書或微軟的網(wǎng)站中搜索

********************************************************************
      
      
        */
      
      
        



_Document doc
      
      =app.GetActiveDocument();   
      
        //
      
      
        得到ActiveDocument
      
      

CComVariant FileName(_T(
      
        "
      
      
        c://doc.wps
      
      
        "
      
      )); 
      
        //
      
      
        文件名
      
      

CComVariant FileFormat(
      
        101
      
      );      
      
        //
      
      
        重點(diǎn),看下面的說明
      
      

CComVariant LockComments(
      
        false
      
      ),Password(_T(
      
        ""
      
      
        ));

CComVariant AddToRecentFiles(
      
      
        true
      
      ),WritePassword(_T(
      
        ""
      
      
        ));

CComVariant ReadOnlyRecommended(
      
      
        false
      
      ),EmbedTrueTypeFonts(
      
        false
      
      
        );

CComVariant SaveNativePictureFormat(
      
      
        false
      
      ),SaveFormsData(
      
        false
      
      
        );

CComVariant SaveAsAOCELetter(
      
      
        false
      
      
        );


      
      
        /*
      
      
        ************** FileFormat 文件格式說明 ****************************

參數(shù)FileFormat,在WORD的宏中,使用的是 wdFormatDocument,這是什么那?

其實(shí)這是WORD宏中所使用的常量,由匈牙利命名可以知道wd其實(shí)是DWORD的意思

知道了是一個(gè)正數(shù),那么它到底是多少那?其實(shí)有一個(gè)辦法可以知道,那就是在

WORD宏程序中,加一條語(yǔ)句:MsgBox wdFormatDocument 這樣你再運(yùn)行宏程序,

就能看到這個(gè)常量是多少了。呵呵,這個(gè)常量是0,我夠聰明吧^_^

********************************************************************
      
      
        */
      
      
        

doc.SaveAs(
      
      &FileName,&FileFormat,&LockComments,&
      
        Password,

   
      
      &AddToRecentFiles,&WritePassword,&
      
        ReadOnlyRecommended,

   
      
      &EmbedTrueTypeFonts,&SaveNativePictureFormat,&
      
        SaveFormsData,

   
      
      &
      
        SaveAsAOCELetter);



sel.ReleaseDispatch();

doc.ReleaseDispatch();

docs.ReleaseDispatch();



CComVariant SaveChanges(
      
      
        false
      
      
        ),OriginalFormat,RouteDocument;

app.Quit(
      
      &SaveChanges,&OriginalFormat,&
      
        RouteDocument);

app.ReleaseDispatch();

AfxMessageBox(_T(
      
      
        "
      
      
        請(qǐng)檢查c://hello.doc是否正常產(chǎn)生了。下面該學(xué)習(xí)Setp5了
      
      
        "
      
      
        ));

}
      
    

?

?

例子5:

      
        /*
      
      
        **************** WORD 中錄制的宏,按筆畫排序(全部選擇,菜單表格/排序) ********

    MsgBox wdSortFieldStroke    '5      使用了常量,所以使用MsgBox得到具體的數(shù)值

    MsgBox wdSortOrderAscending '0

    MsgBox wdSortFieldSyllable '3

MsgBox wdSeparateByTabs     '1

    MsgBox wdSimplifiedChinese '2052

    

    Selection.WholeStory   '全選

    Selection.Sort ExcludeHeader:=False, FieldNumber:="段落數(shù)", SortFieldType:= _

        wdSortFieldStroke, SortOrder:=wdSortOrderAscending, FieldNumber2:="", _

        SortFieldType2:=wdSortFieldSyllable, SortOrder2:=wdSortOrderAscending, _

        FieldNumber3:="", SortFieldType3:=wdSortFieldSyllable, SortOrder3:= _

        wdSortOrderAscending, Separator:=wdSortSeparateByTabs, SortColumn:=False, _

         CaseSensitive:=False, LanguageID:=wdSimplifiedChinese

    Selection.Copy     '把排序后的結(jié)果,復(fù)制到剪貼板

********************************************************************************
      
      
        */
      
      
        



#include 
      
      
        "
      
      
        msword9.h
      
      
        "
      
      
        

#include 
      
      <AtlBase.h>




      
        void
      
      
         CStep5Dlg::OnOK() 

{

CString str;

GetDlgItemText(IDC_EDIT1,str);

str.TrimLeft(); str.TrimRight();


      
      
        if
      
      (str.IsEmpty()) 
      
        return
      
      
        ;



::CoInitialize(NULL);

_Application app;

app.CreateDispatch(_T(
      
      
        "
      
      
        Word.Application
      
      
        "
      
      
        ));


      
      
        //
      
      
        app.SetVisible(FALSE); 
      
      
        //
      
      
        這次不調(diào)用顯示,因?yàn)槲覀円低得霓D(zhuǎn)換:)
      
      

Documents docs=
      
        app.GetDocuments();

CComVariant Template(
      
      
        ""
      
      ),NewTemplate(
      
        false
      
      ),DocumentType(
      
        0
      
      
        ),Visible;

docs.Add(
      
      &Template,&NewTemplate,&DocumentType,&
      
        Visible);

Selection sel
      
      =
      
        app.GetSelection();




      
      
        for
      
      (
      
        int
      
       i=
      
        0
      
      ;i<str.GetLength()/
      
        2
      
      ;i++
      
        )

{ 
      
      
        //
      
      
        這里只考慮了輸入為純漢字的情況,你自己修改為可以支持中英文混合的情況
      
      

   sel.TypeText(str.Mid(i*
      
        2
      
      ,
      
        2
      
      )+
      
        "
      
      
        /r/n
      
      
        "
      
      ); 
      
        //
      
      
        2個(gè)字符表示一個(gè)漢字,用回車換行分隔
      
      
        }

sel.WholeStory();   
      
      
        //
      
      
        全部選擇
      
      
        

CComVariant ExcludeHeader(
      
      
        false
      
      
        );

CComVariant FieldNumber(_T(
      
      
        "
      
      
        段落數(shù)
      
      
        "
      
      )),SortFieldType(
      
        5
      
      ),SortOrder(
      
        0
      
      
        );

CComVariant FieldNumber2(_T(
      
      
        ""
      
      )),SortFieldType2(
      
        3
      
      ),SortOrder2(
      
        0
      
      
        );

CComVariant FieldNumber3(_T(
      
      
        ""
      
      )),SortFieldtype3(
      
        3
      
      ),SortOrder3(
      
        0
      
      
        );

CComVariant SortColumn(
      
      
        false
      
      ),Separator(
      
        1
      
      ),LanguageID(
      
        2052
      
      
        );

CComVariant CaseSensitive(
      
      
        false
      
      
        ),BidiSort,IgnoreThe;

CComVariant IgnoreKashida,IgnoreDiacritics,IgnoreHe;


      
      
        //
      
      
        排序
      
      

sel.Sort(&ExcludeHeader,&FieldNumber,&SortFieldType,&
      
        SortOrder,

   
      
      &FieldNumber2,&SortFieldType2,&SortOrder2,&
      
        FieldNumber3,

   
      
      &SortFieldtype3,&SortOrder3,&SortColumn,&
      
        Separator,

   
      
      &CaseSensitive,&BidiSort,&IgnoreThe,&
      
        IgnoreKashida,

   
      
      &IgnoreDiacritics,&IgnoreHe,&
      
        LanguageID);




      
      
        //
      
      
        其實(shí),這里可以直接調(diào)用sel.GetText()取得文本。


      
      
        //
      
      
        但現(xiàn)在選擇復(fù)制到剪貼板的方式。
      
      
        

sel.Copy();   
      
      
        //
      
      
        復(fù)制到剪貼板
      
      
        if
      
      
        (OpenClipboard())

{ 
      
      
        //
      
      
        從剪貼板取出排序后的文字
      
      

   HGLOBAL hMem=
      
        ::GetClipboardData(CF_TEXT);

   LPCTSTR lp
      
      =
      
        (LPCTSTR)::GlobalLock(hMem);

   str
      
      =
      
        lp;

   ::GlobalUnlock(hMem);



   CloseClipboard();



   str.Replace(
      
      
        "
      
      
        /r/n
      
      
        "
      
      ,
      
        ""
      
      );   
      
        //
      
      
        刪除回車換行
      
      
           SetDlgItemText(IDC_EDIT2,str);

}

sel.ReleaseDispatch();

docs.ReleaseDispatch();



CComVariant SaveChanges(
      
      
        false
      
      
        ),OriginalFormat,RouteDocument;

app.Quit(
      
      &SaveChanges,&OriginalFormat,&
      
        RouteDocument);

app.ReleaseDispatch();



::CoUninitialize();

}
      
    

?

例子6:

?

      #include 
      
        "
      
      
        msword9.h
      
      
        "
      
      
        void
      
      
         CStep6Dlg::OnOK() 

{

CLSID clsid;

HRESULT hr;

hr
      
      =::CLSIDFromProgID(L
      
        "
      
      
        Word.Application
      
      
        "
      
      ,&clsid); 
      
        //
      
      
        通過ProgID取得CLSID
      
      
        if
      
      
        (FAILED(hr))

{

   AfxMessageBox(_T(
      
      
        "
      
      
        不會(huì)吧,竟然沒有安裝OFFICE
      
      
        "
      
      
        ));

   
      
      
        return
      
      
        ;

}

IUnknown 
      
      *pUnknown=
      
        NULL;

IDispatch 
      
      *pDispatch=
      
        NULL;

_Application app
      
      =
      
        NULL;

Selection sel
      
      =
      
        NULL;

hr
      
      =::GetActiveObject(clsid,NULL,&pUnknown); 
      
        //
      
      
        查找是否有WORD程序在運(yùn)行
      
      
        if
      
      
        (FAILED(hr))

{

   AfxMessageBox(_T(
      
      
        "
      
      
        沒有正在運(yùn)行中的WORD應(yīng)用程序
      
      
        "
      
      
        ));

   
      
      
        return
      
      
        ;

}


      
      
        try
      
      
        

{

   hr
      
      =pUnknown->QueryInterface(IID_IDispatch,(LPVOID *)&
      
        app);

   
      
      
        if
      
      (FAILED(hr)) 
      
        throw
      
      (_T(
      
        "
      
      
        沒有取得IDispatchPtr
      
      
        "
      
      
        ));

   pUnknown
      
      ->Release(); pUnknown=
      
        NULL;

   sel
      
      =
      
        app.GetSelection();

   
      
      
        if
      
      (!sel) 
      
        throw
      
      (_T(
      
        "
      
      
        沒有正在編輯的文檔
      
      
        "
      
      
        ));

   sel.WholeStory();     
      
      
        //
      
      
        全部選擇
      
      

   CString str=sel.GetText();   
      
        //
      
      
        取得文本
      
      

   SetDlgItemText(IDC_EDIT1,str); 
      
        //
      
      
        顯示到編輯窗中
      
      
        }


      
      
        catch
      
      
        (LPCTSTR lpErr)

{

   AfxMessageBox(lpErr);

}


      
      
        if
      
      (pUnknown) pUnknown->
      
        Release();


      
      
        if
      
      
        (sel) sel.ReleaseDispatch();


      
      
        if
      
      
        (app) sel.ReleaseDispatch();

}
      
    

?

?

?

7、圖片查找的demo(無選中功能)

vc操縱word問題:查找到圖片后如何得到該圖片的長(zhǎng)寬信息
一個(gè)Word文檔中已經(jīng)有一張BMP圖片。VC打開該文檔,并Find查找到該圖片。

      COleVariant   covTrue((
      
        short
      
      )TRUE),covFalse((
      
        short
      
      )FALSE),covOptional((
      
        long
      
      
        )DISP_E_PARAMNOTFOUND,VT_ERROR); 



_Application   app;    
      
      
        //
      
      
        定義一個(gè)WORD的應(yīng)用對(duì)象 
      
      

app.CreateDispatch(_T(
      
        "
      
      
        Word.Application
      
      
        "
      
      )))    
      
        //
      
      
        啟動(dòng)WORD 
      
      

app.SetVisible(
      
        true
      
      );                           
      
        //
      
      
        設(shè)置用戶可見 
      
      

Documents   docs=
      
        app.GetDocuments(); 

docs.Open(                                         
      
      
        //
      
      
        打開文檔 
      
      
                        COleVariant((LPCSTR)m_csPath,VT_BSTR), 

covFalse,     
      
      
        //
      
      
               確定轉(zhuǎn)換       
      
      

covTrue,     
      
        //
      
      
               只讀打開       
      
      

covFalse,     
      
        //
      
      
               添加到最近使用文件列表       
      
      

covOptional,     
      
        //
      
      
               PasswordDocument.       
      
      

covOptional,     
      
        //
      
      
               PasswordTemplate.       
      
      

covFalse,     
      
        //
      
      
               Revert.       
      
      

covOptional,     
      
        //
      
      
               WritePasswordDocument.       
      
      

covOptional,     
      
        //
      
      
               WritePasswordTemplate.       
      
      

COleVariant(
      
        long
      
      (
      
        1
      
      )),       
      
        //
      
      
               Format.       如果是Word97,這便是最好一個(gè)參數(shù)       
      
      

covOptional,     
      
        //
      
      
               編碼             下面是Word       2000/2002專用的參數(shù)       
      
      

covTrue)     
      
        //
      
      
               可見       
      
      
                            

Selection   sel
      
      =
      
        app.GetSelection(); 

Find   find
      
      =
      
        sel.GetFind(); 

find.ClearFormatting(); 

find.Execute(                                       
      
      
        //
      
      
        開始查找圖片 
      
      

COleVariant(_T(
      
        "
      
      
        ^g
      
      
        "
      
      
        ),VT_BSTR), 

covFalse, 

covFalse, 

covFalse, 

covFalse, 

covFalse, 

covTrue, 

COleVariant(
      
      
        long
      
      (
      
        1
      
      
        )), 

covFalse, 

COleVariant(
      
      
        ""
      
      
        ,VT_BSTR), 

covFalse, 

covFalse, 

covFalse, 

covFalse, 

covFalse);
      
    

?

?8、文字查找(有選中功能)

          COleVariant   covTrue((
      
        short
      
      )TRUE),covFalse((
      
        short
      
      )FALSE),covOptional((
      
        long
      
      
        )DISP_E_PARAMNOTFOUND,VT_ERROR); 

    Find t_find
      
      =
      
        sel.GetFind();

    t_find.ClearFormatting();

    t_find.Execute(COleVariant(_T(
      
      
        "
      
      
      
        "
      
      
        ),VT_BSTR),

        covFalse,

        covFalse,

        covFalse,

        covFalse,

        covFalse,

        covTrue,

        COleVariant(
      
      
        long
      
      (
      
        1
      
      
        )),

        covFalse,

        COleVariant(
      
      
        ""
      
      
        ,VT_BSTR),

        covFalse,

        covFalse,

        covFalse,

        covFalse,

        covFalse);
      
    

?

9、帶選中功能的

      
        void
      
      
         DoSelctionPicture()

{   
        
sel.MoveRight(COleVariant(
short ( 1 )),COleVariant( short ( 1 )),COleVariant( short ( 0 ))); // 撤銷當(dāng)前的選中狀態(tài)。 sel.MoveLeft(COleVariant( short ( 1 )),COleVariant( short ( 1 )),COleVariant( short ( 0 ))); // 撤銷當(dāng)前的選中狀態(tài)。 COleVariant covTrue(( short )TRUE),covFalse(( short )FALSE),covOptional(( long )DISP_E_PARAMNOTFOUND,VT_ERROR); Find t_find = sel.GetFind(); t_find.ClearFormatting(); t_find.Execute(COleVariant(_T( " ^g " ),VT_BSTR), covFalse, covFalse, covFalse, covFalse, covFalse, covTrue, COleVariant( long ( 1 )), covFalse, COleVariant( "" ,VT_BSTR),//要替換的字符串。空 covFalse, covFalse, covFalse, covFalse, covFalse); sel.MoveRight(COleVariant( short ( 1 )),COleVariant( short ( 1 )),COleVariant( short ( 1 ))); // 選中。bug:多選中了一個(gè)右邊的字符。 }

?

10、擴(kuò)展

?

      
        //
      
      
        sel.MoveRight(COleVariant((short)1),COleVariant((short)1),COleVariant((short)1));
      
      
        //
      
      
        左移,以字為單位。


      
      
        //
      
      
        m_Sel.MoveDown(COleVariant((short)4),COleVariant((short)1),COleVariant((short)0));
      
      
        //
      
      
        下移,以行為單位。


      
      
        //
      
      
        m_Sel.MoveLeft(COleVariant((short)2),COleVariant((short)1),COleVariant((short)0));
      
      
        //
      
      
        左移,以詞組為單位。


      
      
        //
      
      
        m_Sel.MoveLeft(COleVariant((short)1),COleVariant((short)1),COleVariant((short)0));
      
      
        //
      
      
        左移,以字為單位。


      
      
        //
      
      
        m_Sel.MoveStart(COleVariant((short)2),COleVariant((short)1));


      
      
        //
      
      
        m_Sel.MoveDown(COleVariant((short)4),COleVariant((short)1),COleVariant((short)1));
      
      
        //
      
      
        選中下移,以行為單位。


      
      
        //
      
      
        m_Sel.MoveLeft(COleVariant((short)2),COleVariant((short)1),COleVariant((short)1));
      
      
        //
      
      
        選中左移,以詞組為單位。


      
      
        //
      
      
        m_Sel.MoveLeft(COleVariant((short)1),COleVariant((short)1),COleVariant((short)1));
      
      
        //
      
      
        選中左移,以字為單位。


      
      
        //
      
      
        m_Sel.TypeBackspace();
      
      
        //
      
      
        刪除選中內(nèi)容。
      
    

?

?

11、插入圖片【此部分來源: http://blog.csdn.net/halin1983/article/details/4533112

VC++6.0向Word文件的指定位置上插入一張圖片,需要用到nlineShapes類型的AddPicture方法。

在MSDN中,該方法的聲明如下:

**************************************************************************

在文檔中添加一幅圖片。返回一個(gè) Shape 對(duì)象,該對(duì)象代表圖片,并將其添加至 InlineShapes 集合。

expression.AddPicture(FileName, LinkToFile, SaveWithDocument, Range)
expression????? 必需。該表達(dá)式返回一個(gè) InlineShapes 對(duì)象。

FileName???? String 類型,必需。圖片的路徑和文件名。

LinkToFile???? Variant 類型,可選。如果為 True,則將圖片鏈接到創(chuàng)建該對(duì)象的文件;如果為 False,則將圖片作為該文件的獨(dú)立副本。默認(rèn)值是 False。

SaveWithDocument???? Variant 類型,可選。如果為 True,則將鏈接的圖片與文檔一起保存。默認(rèn)值是 False。

Range???? Variant 類型,可選。圖片置于文本中的位置。如果該區(qū)域未折疊,那么圖片將覆蓋此區(qū)域,否則插入圖片。如果忽略此參數(shù),則自動(dòng)放置圖片

**************************************************************************

根據(jù)該說明的定義,第四個(gè)參數(shù)Range是用來設(shè)置圖片位于文本中的位置的。因此,我們可以利用該參數(shù)向某一指定位置插入圖片。具體方法如下:

?

      Selection sel=m_app.GetSelection();
      
        //
      
      
        獲取文檔的selection
      
      

InlineShapes inlineshapes =
      
         sel.GetInlineShapes();

inlineshapes.AddPicture(
      
      
        "
      
      
        D:\\abc.bmp
      
      
        "
      
      ,COleVariant((
      
        short
      
      )FALSE),COleVariant((
      
        short
      
      )TRUE),&_variant_t(sel.GetRange()));
      
        //
      
      
        添加圖片,并制定其位置為當(dāng)前光標(biāo)位置
      
      
        inlineshapes.ReleaseDispatch();

sel.ReleaseDispatch();






      
      
        //
      
      
        注意上面第四個(gè)參數(shù)的轉(zhuǎn)換方法,用到了_variant_t變量,使用此種方法可以將LPDISPATCH轉(zhuǎn)換成VARIANT*類型
      
    

?

?

如此就可將圖片直接插入到光標(biāo)當(dāng)前所在位置

?

?

Document對(duì)象的保存功能:

      LPCWSTR pathstr=L"D:\\abc.doc";

VARIANT path;

path.vt=VT_BSTR;

path.bstrVal=SysAllocString(pathstr);

VARIANT overw;

overw.vt=VT_BOOL;

overw.boolVal=VARIANT_TRUE;

VARIANT name;

name.vt=VT_BSTR;

name.bstrVal=NULL;

m_FramerControl.Save( path,overw,path,path);


    

?

VC操縱Word


更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號(hào)聯(lián)系: 360901061

您的支持是博主寫作最大的動(dòng)力,如果您喜歡我的文章,感覺我的文章對(duì)您有幫助,請(qǐng)用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點(diǎn)擊下面給點(diǎn)支持吧,站長(zhǎng)非常感激您!手機(jī)微信長(zhǎng)按不能支付解決辦法:請(qǐng)將微信支付二維碼保存到相冊(cè),切換到微信,然后點(diǎn)擊微信右上角掃一掃功能,選擇支付二維碼完成支付。

【本文對(duì)您有幫助就好】

您的支持是博主寫作最大的動(dòng)力,如果您喜歡我的文章,感覺我的文章對(duì)您有幫助,請(qǐng)用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長(zhǎng)會(huì)非常 感謝您的哦!!!

發(fā)表我的評(píng)論
最新評(píng)論 總共0條評(píng)論
主站蜘蛛池模板: 衢州市| 绥江县| 若羌县| 平安县| 黄陵县| 宝坻区| 武城县| 林周县| 马公市| 呼和浩特市| 玛纳斯县| 楚雄市| 岚皋县| 金湖县| 崇明县| 忻州市| 高碑店市| 阿克| 从江县| 金华市| 隆尧县| 云南省| 诸城市| 井陉县| 宿迁市| 乡城县| 普陀区| 冷水江市| 昌江| 张家口市| 江北区| 永安市| 剑河县| 南漳县| 明光市| 塔河县| 海阳市| 舒兰市| 翁牛特旗| 辽源市| 兴文县|