⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 myworddlg.cpp

📁 有关于VC操作Word的程序
💻 CPP
字号:
// MyWordDlg.cpp : implementation file
//

#include "stdafx.h"
#include "MyWord.h"
#include "MyWordDlg.h"
#include "GrobalFunc.h"



#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About

class CAboutDlg : public CDialog
{
public:
	CAboutDlg();

// Dialog Data
	//{{AFX_DATA(CAboutDlg)
	enum { IDD = IDD_ABOUTBOX };
	//}}AFX_DATA

	// ClassWizard generated virtual function overrides
	//{{AFX_VIRTUAL(CAboutDlg)
	protected:
	virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
	//}}AFX_VIRTUAL

// Implementation
protected:
	//{{AFX_MSG(CAboutDlg)
	//}}AFX_MSG
	DECLARE_MESSAGE_MAP()
};

CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
	//{{AFX_DATA_INIT(CAboutDlg)
	//}}AFX_DATA_INIT
}

void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CAboutDlg)
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
	//{{AFX_MSG_MAP(CAboutDlg)
		// No message handlers
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CMyWordDlg dialog

CMyWordDlg::CMyWordDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CMyWordDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CMyWordDlg)
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CMyWordDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CMyWordDlg)
	DDX_Control(pDX, IDC_COMBO, m_wordpos);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CMyWordDlg, CDialog)
	//{{AFX_MSG_MAP(CMyWordDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_FORM_WORD, OnFormWord)
	ON_BN_CLICKED(IDC_EXIT, OnExit)
	ON_CBN_SELCHANGE(IDC_COMBO, OnSelchangeCombo)
	ON_WM_DESTROY()
	ON_WM_DRAWITEM()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CMyWordDlg message handlers

BOOL CMyWordDlg::OnInitDialog()
{
	CDialog::OnInitDialog();

	// Add "About..." menu item to system menu.

	// IDM_ABOUTBOX must be in the system command range.
	ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
	ASSERT(IDM_ABOUTBOX < 0xF000);

	CMenu* pSysMenu = GetSystemMenu(FALSE);
	if (pSysMenu != NULL)
	{
		CString strAboutMenu;
		strAboutMenu.LoadString(IDS_ABOUTBOX);
		if (!strAboutMenu.IsEmpty())
		{
			pSysMenu->AppendMenu(MF_SEPARATOR);
			pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
		}
	}

	// 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
	m_wordpos.AddString("E:\\\\2.doc");
	m_wordpos.AddString("E:\\\\3.doc");
	m_wordpos.SetCurSel(0);
    swordpos="E:\\1.doc";
    
	if(CoInitialize(NULL)!=0)
	{
		AfxMessageBox("初始化COM支持库失败!");
		exit(1);
	}

    if(!oApp.CreateDispatch("Word.Application",NULL))
	{
		AfxMessageBox("无法启动Word程序!");
		exit(1);
	}

	oApp.SetVisible(TRUE);
	oDocs=oApp.GetDocuments();

	return TRUE;  // return TRUE  unless you set the focus to a control
}

void CMyWordDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
	if ((nID & 0xFFF0) == IDM_ABOUTBOX)
	{
		CAboutDlg dlgAbout;
		dlgAbout.DoModal();
	}
	else
	{
		CDialog::OnSysCommand(nID, lParam);
	}
}

// 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 CMyWordDlg::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 CMyWordDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}


//执行表格/图形的删除和序号调整操作
int CMyWordDlg::Rechange_TalbePic( CString del_TablePic[], int count, CString strSql, int p1,int p2 )
{
	//定义数据库操作变量
	ado::_RecordsetPtr  m_pset;
	VARIANT  var;
	

	//定义序号调整所需变量
	CString Markrange,Markorder,strorder,result;
	int delRage,increment,i,j,k;
	int order,chapter,iRecordcount,chapdel[20],ordel[20];
//	int chaptemp,chapchg;
	char buffer[5];


	if(!del_TablePic[count].IsEmpty())//需删除表格/图形记录字符串数组中数量与记录的数量不一致
		return -1;

	//执行删除操作
	for(i=0;i<=count-1;i++)
	{
		Markrange=GetParaS(del_TablePic[i],p2,'.');
		Markorder=GetParaS(del_TablePic[i],p1,'.');
		Markrange.Remove(' ');//去除空格

		//取得要删除表格/图形的序号标签值
		bookmarks=oDoc.GetBookmarks();
		COleVariant temmark2(Markorder);
		bookmark=bookmarks.Item(&temmark2);
		range=bookmark.GetRange();
		strorder=range.GetText();

		//记录要删除表格/图形的序号,分别记录章数(chapdel)和序号(ordel)
		chapdel[i]=atoi(GetParaS(strorder,1,'.'));
		ordel[i]=atoi(GetParaS(strorder,2,'.'));
		chapdel[i+1]=chapdel[i]+1;

		//删除当前报告没有的表格/图形
		bookmarks=oDoc.GetBookmarks();
		COleVariant temmark1(Markrange);
		bookmark=bookmarks.Item(&temmark1);
		range=bookmark.GetRange();
		range.SetText("");
	}


	//初始化记录集
	m_pset=::DefineDatabaseRecordset();

	if(::GetOpenRecordset(m_pset,strSql)!=false)//执行SQL语句
	{
		iRecordcount=m_pset->GetRecordCount();//得到记录数

		j=-1;//已经删除的表格/图形的序号记录
		i=1;//记录更改的标签数
		
		if(!m_pset->adoEOF)
			m_pset->MoveFirst();//将记录集指针移到记录头		
		do
		{
			var = m_pset->GetCollect("Mark_Order");
			if(var.vt != VT_NULL)
				Markorder = (LPCSTR)_bstr_t(var);

			Markorder.Remove(' ');//去除空格

			AfxMessageBox(Markorder);

			delRage=0;
			for(k=0;k<=count;k++)//判断取出的标签的表格/图形是否已经被删除
			{
				del_TablePic[k].Remove(' ');
				if(Markorder==GetParaS(del_TablePic[k],p1,'.'))
					delRage=1;//已被删除就将rage标志置1
			}

			if(delRage==0)//该标签的表格/图形存在则进入重新排序的操作
			{
				bookmarks=oDoc.GetBookmarks();
				COleVariant temmark1(Markorder);
				bookmark=bookmarks.Item(&temmark1);
				range=bookmark.GetRange();
				strorder=range.GetText();
				
				//取得表格/图形序号
				chapter=atoi(GetParaS(strorder,1,'.'));
				order=atoi(GetParaS(strorder,2,'.'));

				increment=0;//序号调整增量
				for(j=0;j<=count-1;j++)
				{
					if(chapter==chapdel[j])
						if(order>ordel[j])
							increment++;
				}
				order=order-increment;//序号调整

				result=_itoa(chapter,buffer,10);
				result=result+"."+_itoa(order,buffer,10);
				AfxMessageBox(result);

				//操作Word中的书签,将修改过后的序号填到标签处
				bookmarks=oDoc.GetBookmarks();
				COleVariant temmark2(Markorder);
				bookmark=bookmarks.Item(&temmark2);
				range=bookmark.GetRange();
				range.SetText(result);
			}
			
			i++;
			m_pset->MoveNext();//下移数据查询指针
		}
		while(i<=iRecordcount && j<=count);
	}
	m_pset->Close();
	return 0;
}



void CMyWordDlg::OnFormWord() 
{
	// TODO: Add your control notification handler code here
	COleVariant vOpt(DISP_E_PARAMNOTFOUND,VT_ERROR);
	oDoc=oDocs.Add(COleVariant(swordpos),vOpt,vOpt,vOpt);

	//连接数据库
	if(::DatabaseConnect()!=NULL)
	{
		AfxMessageBox("数据库连接成功!");
	}

	//设置要删除的表格的记录值
	CString del[20]={"markt2c3.markt2c3range","markt2c5.markt2c5range","markt7c5.markt7c5range","markt8c5.markt8c5range","markt17c5.markt17c5range","markt1c6.markt1c6range"};
	CString strSql= "SELECT Mark_Order FROM Mark WHERE Mark_num=1 ORDER BY Mark_Order";
	int p1=1,p2=2,num=6;

	//调用函数进行删除及序号调整操作
	if(Rechange_TalbePic(del,num,strSql,p1,p2)!=0)
	{
		AfxMessageBox("序号调整失败!");
		CMyWordDlg::OnDestroy();
    }

    oDoc.SaveAs(COleVariant("E:\\temp.doc"),vOpt,vOpt,vOpt,vOpt,vOpt,vOpt,vOpt,vOpt,vOpt,vOpt);
	
	
	oDoc.Close(vOpt,vOpt,vOpt);
	oApp.Quit(vOpt,vOpt,vOpt);
	    
/*
	//自定义文件名保存
	OPENFILENAME ofn;
	ZeroMemory(&ofn,sizeof(ofn));
	TCHAR lpstrFileName[MAX_PATH]="";
	ofn.lStructSize=sizeof(OPENFILENAME);
    ofn.hwndOwner=m_hWnd;
	ofn.lpstrFilter="Microsoft Word文档(.doc)\0*.doc\0";
	ofn.nMaxFile=MAX_PATH;
	ofn.lpstrFile=lpstrFileName;
	ofn.hInstance=AfxGetInstanceHandle();
	ofn.Flags=OFN_EXPLORER;
    
	//出现另存为对话框
	if(GetSaveFileName(&ofn)==IDOK)
	{
		CString sFile=ofn.lpstrFile;
		try
		{
			oDoc.SaveAs(COleVariant(sFile),vOpt,vOpt,vOpt,vOpt,vOpt,vOpt,vOpt,vOpt,vOpt,vOpt);
		}
        catch(COleException *e)
		{
            LPVOID lpMsg;
			::FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER|FORMAT_MESSAGE_IGNORE_INSERTS
				|FORMAT_MESSAGE_FROM_SYSTEM,NULL,e->m_sc,
				MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), 
                (LPTSTR) &lpMsg,
                0,
                NULL);
			::MessageBox(NULL,(LPCTSTR) lpMsg,"COM Error",MB_OK|MB_SETFOREGROUND);
			::LocalFree(lpMsg);
		}
		catch(COleDispatchException *e)
		{
			char msg[512];
			sprintf(msg,"程序运行出错'%d',系统提示信息为:\n\n%s",
				e->m_strSource,e->m_strDescription);
			::MessageBox(NULL,msg,"无法保存文件",MB_OK|MB_SETFOREGROUND);
		}
	}
	oDoc.Close(vOpt,vOpt,vOpt);
*/

}

void CMyWordDlg::OnExit() 
{
	// TODO: Add your control notification handler code here
	OnCancel();
}

void CMyWordDlg::OnSelchangeCombo() 
{
	// TODO: Add your control notification handler code here
	UpdateData(false);
	m_wordpos.GetLBText(m_wordpos.GetCurSel(),swordpos);
}

void CMyWordDlg::OnDestroy() 
{
	CDialog::OnDestroy();
	
	// TODO: Add your message handler code here
	COleVariant vOpt(DISP_E_PARAMNOTFOUND,VT_ERROR);
	oApp.Quit(COleVariant((short)false),vOpt,vOpt);
	oDoc.ReleaseDispatch();
	oApp.ReleaseDispatch();

}

void CMyWordDlg::OnDrawItem(int nIDCtl, LPDRAWITEMSTRUCT lpDrawItemStruct) 
{
	// TODO: Add your message handler code here and/or call default
	
	CDialog::OnDrawItem(nIDCtl, lpDrawItemStruct);
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -