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

📄 optionworddlg.cpp

📁 分析word的例子程序
💻 CPP
📖 第 1 页 / 共 2 页
字号:


    //保存word文档
    _Document oActiveDoc; 
    oActiveDoc = oWordApp.GetActiveDocument();
    oActiveDoc.SaveAs(COleVariant("c:\\doc1.doc"), 
                    COleVariant((short)0),
                    vFalse, COleVariant(""), vTrue, COleVariant(""),
                     vFalse, vFalse, vFalse, vFalse, vFalse);
 
    //退出word应用程序
	oWordApp.Quit(vOpt, vOpt, vOpt);
}

void COptionWordDlg::OnButton3() 
{
	// TODO: Add your control notification handler code here
	AfxEnableControlContainer();
    AfxOleInit();

	BeginWaitCursor();
	COleVariant vTrue((short)TRUE), vFalse((short)FALSE), vOpt((long)DISP_E_PARAMNOTFOUND, VT_ERROR);

	_Application m_App;					//定义Word提供的应用程序对象;
	Documents m_Docs;					//定义Word提供的文档对象;
	Selection m_Sel;					//定义Word提供的选择对象;

	//m_Docs.ReleaseDispatch();
	//m_Sel.ReleaseDispatch();
	m_App.m_bAutoRelease=true;
	if(!m_App.CreateDispatch("Word.Application"))
	{ 
		AfxMessageBox("创建Word2000服务失败!"); 
		exit(1); 
	}

	//下面是定义VARIANT变量;
	COleVariant varFilePath("c:\\doc1.doc");
	COleVariant varstrNull("");
	COleVariant varZero((short)0);
	COleVariant varTrue(short(1),VT_BOOL);
	COleVariant varFalse(short(0),VT_BOOL);
	m_Docs.AttachDispatch(m_App.GetDocuments());//将Documents类对象m_Docs和Idispatch接口关联起来;

	m_Docs.Open(varFilePath,varFalse,varFalse,varFalse,
	varstrNull,varstrNull,varFalse,varstrNull,
	varstrNull,varTrue,varTrue,varTrue);

	//打开Word文档; 
	m_Sel.AttachDispatch(m_App.GetSelection());//将Selection类对象m_Sel和Idispatch接口关联起来;	

	m_Sel.GetSections();
	m_Sel.WholeStory();
	_Font fon;
	fon = m_Sel.GetFont();                             

	fon.SetNameFarEast("宋体");
	fon.SetNameAscii("Times New Roman");
	fon.SetName("黑体");
	fon.SetSize(10);
	//fon.SetBold(0);
	fon.SetNameOther("Times New Roman");
	
	
	m_Sel.SetFont(fon);

	//save word file
	_Document oActiveDoc; 
	oActiveDoc = m_App.GetActiveDocument(); 
	oActiveDoc.Save();
	//oActiveDoc.SaveAs(COleVariant("c:\\填写后表格.doc"), 
	//COleVariant((short)0), 
	//vFalse, COleVariant(""), vTrue, COleVariant(""), 
	//vFalse, vFalse, vFalse, vFalse, vFalse);
	
	m_Docs.ReleaseDispatch();//断开关联;
	m_Sel.ReleaseDispatch();

	//退出WORD 
	m_App.Quit(vOpt, vOpt, vOpt); 
	m_App.Quit(vOpt, vOpt, vOpt);
	m_App.ReleaseDispatch();

	EndWaitCursor();

	MessageBox("word表格填写完毕!","提示",MB_ICONEXCLAMATION);

}

void COptionWordDlg::OnButton4() 
{
	// TODO: Add your control notification handler code here
	AfxEnableControlContainer();
    AfxOleInit();

	BeginWaitCursor();
	COleVariant vTrue((short)TRUE), vFalse((short)FALSE), vOpt((long)DISP_E_PARAMNOTFOUND, VT_ERROR);

	_Application m_App;					//定义Word提供的应用程序对象;
	Documents m_Docs;					//定义Word提供的文档对象;
	Selection m_Sel;					//定义Word提供的选择对象;

	m_Docs.ReleaseDispatch();
	m_Sel.ReleaseDispatch();
	m_App.m_bAutoRelease=true;
	if(!m_App.CreateDispatch("Word.Application"))
	{ 
		AfxMessageBox("创建Word2000服务失败!"); 
		exit(1); 
	}

	//下面是定义VARIANT变量;
	COleVariant varFilePath("c:\\doc1.doc");
	COleVariant varstrNull("");
	COleVariant varZero((short)0);
	COleVariant varTrue(short(1),VT_BOOL);
	COleVariant varFalse(short(0),VT_BOOL);

	m_Docs.AttachDispatch(m_App.GetDocuments());//将Documents类对象m_Docs和Idispatch接口关联起来;
	m_Docs.Open(varFilePath,varFalse,varFalse,varFalse,varstrNull,varstrNull,varFalse,varstrNull,varstrNull,varTrue,varTrue,varTrue);
	//打开Word文档; 
	m_Sel.AttachDispatch(m_App.GetSelection());//将Selection类对象m_Sel和Idispatch接口关联起来;	
	//全选
	m_Sel.GetSections();
	m_Sel.WholeStory();
	//m_Sel.SelectRow();
	//设置字体
	_Font fon;
	fon = m_Sel.GetFont();       
	fon.SetNameFarEast("宋体");
	fon.SetNameAscii("Times New Roman");
	fon.SetName("黑体");
	fon.SetSize(40);
	fon.SetBold(0);
	fon.SetNameOther("Times New Roman");	
	m_Sel.SetFont(fon);

	//保存文件
	_Document oActiveDoc; 
	oActiveDoc = m_App.GetActiveDocument(); 
	oActiveDoc.Save();
	//oActiveDoc.SaveAs(COleVariant("c:\\填写后表格.doc"), 
	//COleVariant((short)0), 
	//vFalse, COleVariant(""), vTrue, COleVariant(""), 
	//vFalse, vFalse, vFalse, vFalse, vFalse);
	
	m_Docs.ReleaseDispatch();//断开关联;
	m_Sel.ReleaseDispatch();

	//退出WORD 
	m_App.Quit(vOpt, vOpt, vOpt); 
	m_App.Quit(vOpt, vOpt, vOpt);
	m_App.ReleaseDispatch();

	EndWaitCursor();

	MessageBox("word表格填写完毕!","提示",MB_ICONEXCLAMATION);
}

void COptionWordDlg::OnButton5() 
{
	// TODO: Add your control notification handler code here
	AfxEnableControlContainer();
    AfxOleInit();

	BeginWaitCursor();
	COleVariant vTrue((short)TRUE), vFalse((short)FALSE), vOpt((long)DISP_E_PARAMNOTFOUND, VT_ERROR);

	_Application m_App;					//定义Word提供的应用程序对象;
	Documents m_Docs;					//定义Word提供的文档对象;
	Selection m_Sel;					//定义Word提供的选择对象;

	m_Docs.ReleaseDispatch();
	m_Sel.ReleaseDispatch();
	m_App.m_bAutoRelease=true;
	if(!m_App.CreateDispatch("Word.Application"))
	{ 
		AfxMessageBox("创建Word2000服务失败!"); 
		exit(1); 
	}

	//下面是定义VARIANT变量;
	COleVariant varFilePath("c:\\doc1.doc");
	COleVariant varstrNull("");
	COleVariant varZero((short)0);
	COleVariant varTrue(short(1),VT_BOOL);
	COleVariant varFalse(short(0),VT_BOOL);

	m_Docs.AttachDispatch(m_App.GetDocuments());//将Documents类对象m_Docs和Idispatch接口关联起来;
	m_Docs.Open(varFilePath,varFalse,varFalse,varFalse,varstrNull,varstrNull,varFalse,varstrNull,varstrNull,varTrue,varTrue,varTrue);
	//打开Word文档; 
	m_Sel.AttachDispatch(m_App.GetSelection());//将Selection类对象m_Sel和Idispatch接口关联起来;
	
	//PageSetup pagesetup;
	_Document doc = m_App.GetActiveDocument();

	PageSetup pageS; 
	pageS.AttachDispatch(doc.GetPageSetup()); 
	pageS.SetOrientation(0);
	pageS.SetTopMargin(72);
	pageS.SetBottomMargin(72);
	pageS.SetLeftMargin(89.85);
	pageS.SetRightMargin(89.85);
	pageS.SetGutter(0);
	pageS.SetHeaderDistance(42.5196);
	pageS.SetFooterDistance(49.663);
    pageS.SetPageWidth(595.2756);
    pageS.SetPageHeight(841.9);
    pageS.SetFirstPageTray(0);
    pageS.SetOtherPagesTray(0);
    pageS.SetSectionStart(2);
    pageS.SetOddAndEvenPagesHeaderFooter(VARIANT_FALSE);
    pageS.SetDifferentFirstPageHeaderFooter(VARIANT_TRUE);
    pageS.SetVerticalAlignment(0);
    pageS.SetSuppressEndnotes(0);
    pageS.SetMirrorMargins(VARIANT_FALSE);// = False
    pageS.SetTwoPagesOnOne(VARIANT_FALSE);// = False

    //pageS.SetBookFoldPrinting(VARIANT_FALSE);
    //pageS.SetBookFoldRevPrinting(VARIANT_FALSE);
    //pageS.SetBookFoldPrintingSheets(1);
    pageS.SetGutterPos(0);// = wdGutterPosLeft
    pageS.SetLayoutMode(1);


	//保存文件
	_Document oActiveDoc; 
	oActiveDoc = m_App.GetActiveDocument(); 
	oActiveDoc.Save();
	
	m_Docs.ReleaseDispatch();//断开关联;
	m_Sel.ReleaseDispatch();

	//退出WORD 
	m_App.Quit(vOpt, vOpt, vOpt); 
	m_App.Quit(vOpt, vOpt, vOpt);
	m_App.ReleaseDispatch();
	EndWaitCursor();
	MessageBox("word表格填写完毕!","提示",MB_ICONEXCLAMATION);
}
/*
With Selection.PageSetup
        .LineNumbering.Active = False
        .Orientation = wdOrientPortrait
        .TopMargin = CentimetersToPoints(2.54)
        .BottomMargin = CentimetersToPoints(2.54)
        .LeftMargin = CentimetersToPoints(3.17)
        .RightMargin = CentimetersToPoints(3.17)
        .Gutter = CentimetersToPoints(0)
        .HeaderDistance = CentimetersToPoints(1.5)
        .FooterDistance = CentimetersToPoints(1.75)
        .PageWidth = CentimetersToPoints(21)
        .PageHeight = CentimetersToPoints(29.7)
        .FirstPageTray = wdPrinterDefaultBin
        .OtherPagesTray = wdPrinterDefaultBin
        .SectionStart = wdSectionNewPage
        .OddAndEvenPagesHeaderFooter = False
        .DifferentFirstPageHeaderFooter = True
        .VerticalAlignment = wdAlignVerticalTop
        .SuppressEndnotes = False
        .MirrorMargins = False
        .TwoPagesOnOne = False
        .BookFoldPrinting = False
        .BookFoldRevPrinting = False
        .BookFoldPrintingSheets = 1
        .GutterPos = wdGutterPosLeft
        .LayoutMode = wdLayoutModeGrid
    End With
    With Selection.Font
        .NameFarEast = "宋体"
        .NameAscii = "Times New Roman"
        .NameOther = "Times New Roman"
        .Name = "黑体"
        .Size = 12
        .Bold = False
        .Italic = False
        .Underline = wdUnderlineNone
        .UnderlineColor = wdColorAutomatic
        .StrikeThrough = False
        .DoubleStrikeThrough = False
        .Outline = False
        .Emboss = False
        .Shadow = False
        .Hidden = False
        .SmallCaps = False
        .AllCaps = False
        .Color = wdColorAutomatic
        .Engrave = False
        .Superscript = False
        .Subscript = False
        .Spacing = 0
        .Scaling = 180
        .Position = 0
        .Kerning = 1
        .Animation = wdAnimationNone
        .DisableCharacterSpaceGrid = False
        .EmphasisMark = wdEmphasisMarkNone
    End With
    ActiveWindow.ActivePane.VerticalPercentScrolled = 70
    ActiveWindow.ActivePane.VerticalPercentScrolled = 70
    If ActiveWindow.View.SplitSpecial <> wdPaneNone Then
        ActiveWindow.Panes(2).Close
    End If
    If ActiveWindow.ActivePane.View.Type = wdNormalView Or ActiveWindow. _
        ActivePane.View.Type = wdOutlineView Then
        ActiveWindow.ActivePane.View.Type = wdPrintView
    End If
    ActiveWindow.ActivePane.View.SeekView = wdSeekCurrentPageHeader
    Selection.TypeText Text:="真他妈难"
    ActiveWindow.ActivePane.View.SeekView = wdSeekMainDocument
    If ActiveWindow.View.SplitSpecial <> wdPaneNone Then
        ActiveWindow.Panes(2).Close
    End If
    If ActiveWindow.ActivePane.View.Type = wdNormalView Or ActiveWindow. _
        ActivePane.View.Type = wdOutlineView Then
        ActiveWindow.ActivePane.View.Type = wdPrintView
    End If
    ActiveWindow.ActivePane.View.SeekView = wdSeekCurrentPageHeader
    ActiveWindow.ActivePane.View.SeekView = wdSeekMainDocument
    If ActiveWindow.View.SplitSpecial <> wdPaneNone Then
        ActiveWindow.Panes(2).Close
    End If
    If ActiveWindow.ActivePane.View.Type = wdNormalView Or ActiveWindow. _
        ActivePane.View.Type = wdOutlineView Then
        ActiveWindow.ActivePane.View.Type = wdPrintView
    End If
    ActiveWindow.ActivePane.View.SeekView = wdSeekCurrentPageHeader
    NormalTemplate.AutoTextEntries("第 X 页 共 Y 页").Insert Where:=Selection. _
        Range, RichText:=True
    ActiveWindow.ActivePane.View.SeekView = wdSeekMainDocument
    ActiveWindow.ActivePane.VerticalPercentScrolled = 71
*/

⌨️ 快捷键说明

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