首页 > 其他分享 >office二次开发设置页眉页脚

office二次开发设置页眉页脚

时间:2023-01-31 16:36:03浏览次数:40  
标签:font office app 视图 页眉 二次开发 sel TypeText

_Application app;
app.CreateDispatch(_T("Word.Application"));
app.SetVisible(TRUE);

//通过WORD宏可以知道,由于要使用Documents,于是我们定义一个并从app中取得

Documents docs = app.GetDocuments();
//准备调用Documents::Add函数了,需要定义4个参数。
//从WORD宏可以看出来3个参数的类型为:
//Template字符,NewTemplate布尔,DocumentType数值
//但Add函数还需要一个参数是Visible,傻子也能看出来这个值表示是否显示出新文档
//并且可以给默认值(VT_EMPTY)
CComVariant Template(_T(""));  //为了简单,没有使用WORD的文档模板
CComVariant NewTemplate(false), DocumentType(0), Visible;
docs.Add(&Template, &NewTemplate, &DocumentType, &Visible);
//通过WORD宏可以知道,由于要使用Selection,于是我们定义一个并从app中取得
//Selection表示输入点,即光标闪烁的那个地方

Selection sel = app.GetSelection();
//调用函数Selection::TypeText 向WORD发送字符
_Font font(sel.GetFont());

/*页眉页脚*/
Window   mWindowActive;   //定义活动窗口对象
View   mViewActive;   //定义活动视图对象
Pane   mPane;   //定义当前窗格对象
Paragraph para;
Fields fields;
mWindowActive = app.GetActiveWindow(); //获得当前窗口 
mPane = mWindowActive.GetActivePane();   //获得当前窗格
mViewActive = mPane.GetView();   //获得当前视图
mViewActive.SetSeekView(9);   //设置页眉视图
font.SetSize(15);
font.SetColor(RGB(0, 0, 0));
sel.TypeText(_T("设置页眉视图,,,,,,,,,,,,,"));
para.SetAlignment(1);   //居中
sel.TypeParagraph();
font.SetSize(9);
sel.TypeText(_T("设置页眉视图。。。。。。。。。。。"));
para.SetAlignment(1);   //居中
font.SetSize(7);
mViewActive.SetSeekView(10);   //设置页脚视图
sel.TypeText(_T("- "));
fields.Add(sel.GetRange(), COleVariant(short(33)), COleVariant(_T("PAGE  ")), COleVariant(short(1))); //增加页码域
sel.TypeText(_T(" -"));
font.SetSize(6);
font.SetColor(RGB(80, 80, 80));
para.SetAlignment(1);   //居中
sel.TypeText(_T("设置页脚视图"));
para.SetAlignment(1);   //居中


mViewActive.SetSeekView(0); //回到正文视图

标签:font,office,app,视图,页眉,二次开发,sel,TypeText
From: https://blog.51cto.com/u_15389623/6029749

相关文章