Step4Dlg.cpp
上传用户:tjyzx0508
上传日期:2013-02-16
资源大小:254k
文件大小:6k
- // Step4Dlg.cpp : implementation file
- //
- #include "stdafx.h"
- #include "Step4.h"
- #include "Step4Dlg.h"
- #ifdef _DEBUG
- #define new DEBUG_NEW
- #undef THIS_FILE
- static char THIS_FILE[] = __FILE__;
- #endif
- /////////////////////////////////////////////////////////////////////////////
- // CStep4Dlg dialog
- CStep4Dlg::CStep4Dlg(CWnd* pParent /*=NULL*/)
- : CDialog(CStep4Dlg::IDD, pParent)
- {
- //{{AFX_DATA_INIT(CStep4Dlg)
- // NOTE: the ClassWizard will add member initialization here
- //}}AFX_DATA_INIT
- // Note that LoadIcon does not require a subsequent DestroyIcon in Win32
- m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
- }
- void CStep4Dlg::DoDataExchange(CDataExchange* pDX)
- {
- CDialog::DoDataExchange(pDX);
- //{{AFX_DATA_MAP(CStep4Dlg)
- // NOTE: the ClassWizard will add DDX and DDV calls here
- //}}AFX_DATA_MAP
- }
- BEGIN_MESSAGE_MAP(CStep4Dlg, CDialog)
- //{{AFX_MSG_MAP(CStep4Dlg)
- ON_WM_PAINT()
- ON_WM_QUERYDRAGICON()
- //}}AFX_MSG_MAP
- END_MESSAGE_MAP()
- /////////////////////////////////////////////////////////////////////////////
- // CStep4Dlg message handlers
- BOOL CStep4Dlg::OnInitDialog()
- {
- CDialog::OnInitDialog();
- // Set the icon for this dialog. The framework does this automatically
- // when the application's main window is not a dialog
- SetIcon(m_hIcon, TRUE); // Set big icon
- SetIcon(m_hIcon, FALSE); // Set small icon
-
- // TODO: Add extra initialization here
-
- return TRUE; // return TRUE unless you set the focus to a control
- }
- // If you add a minimize button to your dialog, you will need the code below
- // to draw the icon. For MFC applications using the document/view model,
- // this is automatically done for you by the framework.
- void CStep4Dlg::OnPaint()
- {
- if (IsIconic())
- {
- CPaintDC dc(this); // device context for painting
- SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);
- // Center icon in client rectangle
- int cxIcon = GetSystemMetrics(SM_CXICON);
- int cyIcon = GetSystemMetrics(SM_CYICON);
- CRect rect;
- GetClientRect(&rect);
- int x = (rect.Width() - cxIcon + 1) / 2;
- int y = (rect.Height() - cyIcon + 1) / 2;
- // Draw the icon
- dc.DrawIcon(x, y, m_hIcon);
- }
- else
- {
- CDialog::OnPaint();
- }
- }
- // The system calls this to obtain the cursor to display while the user drags
- // the minimized window.
- HCURSOR CStep4Dlg::OnQueryDragIcon()
- {
- return (HCURSOR) m_hIcon;
- }
- #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();
- sel.TypeText(_T("HELLOrn大家好呀"));
- AfxMessageBox(_T("好了,我要保存到c:\hello.doc中了"));
- /**************** 这是在WORD中录制的新建文档直到另存的宏 *************
- Documents.Add Template:= _
- "C:Documents and SettingsAdministratorApplication DataMicrosoftTemplatesNormal.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
- *********************************************************************/
- /**************** 程序思路 ******************************************
- 另存为的函数是ActiveDocument.SaveAs,显然表示的是对当前活跃的文档进行保存,
- 在我们的类中没有ActiveDocument,其实它对应的是_Document,而这个可以由
- _Application 的GetActiveDocument()得到。你一定会提问:“你怎么知道的?”
- 呵呵,怎么说那,我怎么知道的那?答案是:猜。其实如果使用的多了,分析、猜
- 查找都是办法。如果想得到确切的方法,其实可以在VBA的书或微软的网站中搜索
- *********************************************************************/
- _Document doc=app.GetActiveDocument(); //得到ActiveDocument
- CComVariant FileName(_T("c:\Hello.doc")); //文件名
- CComVariant FileFormat(0); //重点,看下面的说明
- CComVariant LockComments(false),Password(_T(""));
- CComVariant AddToRecentFiles(true),WritePassword(_T(""));
- CComVariant ReadOnlyRecommended(false),EmbedTrueTypeFonts(false);
- CComVariant SaveNativePictureFormat(false),SaveFormsData(false);
- CComVariant SaveAsAOCELetter(false);
- /*************** FileFormat 文件格式说明 ****************************
- 参数FileFormat,在WORD的宏中,使用的是 wdFormatDocument,这是什么那?
- 其实这是WORD宏中所使用的常量,由匈牙利命名可以知道wd其实是DWORD的意思
- 知道了是一个正数,那么它到底是多少那?其实有一个办法可以知道,那就是在
- WORD宏程序中,加一条语句:MsgBox wdFormatDocument 这样你再运行宏程序,
- 就能看到这个常量是多少了。呵呵,这个常量是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("请检查c:\hello.doc是否正常产生了。下面该学习Setp5了"));
- }