📄 sample16.c
字号:
#include "msword8.h"
#include <utility.h>
#include <cvirte.h> /* Needed if linking in external compiler; harmless otherwise */
#include <userint.h>
#include "sample16.h"
#define mswordPath "D:\\Microsoft Office\\Office\\WINWORD.EXE"
static int panelHandle;
int error;
// 定义变量appHandle表示Word应用程序句柄
CAObjHandle appHandle;
// 定义变量docHandle表示Word文档句柄
CAObjHandle docHandle;
// 定义变量currSelHandle表示Word文档中的光标句柄
CAObjHandle currSelHandle;
int LaunchWord (void);
int ShutdownWord (void);
int AddContent (void);
int OpenDocFile (void);
int SaveDocFile (void);
int CloseDocFile (void);
int main (int argc, char *argv[])
{
if (InitCVIRTE (0, argv, 0) == 0)
return -1;
if ((panelHandle = LoadPanel (0, "sample16.uir", PANEL)) < 0)
return -1;
DisplayPanel (panelHandle);
RunUserInterface ();
return 0;
}
int CVICALLBACK PanelCallback (int panel, int event, void *callbackData,
int eventData1, int eventData2)
{
switch (event)
{
case EVENT_CLOSE:
QuitUserInterface (0);
break;
}
return 0;
}
int CVICALLBACK QuitCallback (int panel, int control, int event,
void *callbackData, int eventData1, int eventData2)
{
switch (event)
{
case EVENT_COMMIT:
QuitUserInterface (0);
break;
}
return 0;
}
int CVICALLBACK CreateReportCallback (int panel, int control, int event,
void *callbackData, int eventData1, int eventData2)
{
switch (event)
{
case EVENT_COMMIT:
//设置光标为等待模式
SetWaitCursor (1);
//调用自定义函数,修改log.doc文件
LaunchWord();
OpenDocFile();
AddContent();
SaveDocFile();
CloseDocFile();
ShutdownWord();
//设置光标为普通模式
SetWaitCursor (0);
SetCtrlAttribute (panel, PANEL_VIEWREAPORT, ATTR_DIMMED, 0);
break;
}
return 0;
}
int CVICALLBACK ViewReportCallback (int panel, int control, int event,
void *callbackData, int eventData1, int eventData2)
{
char fileName[MAX_PATHNAME_LEN];
char temp[MAX_PATHNAME_LEN];
switch (event)
{
case EVENT_COMMIT:
//打开工程目录下的log.doc文件
strcpy (fileName, mswordPath);
strcat (fileName, " ");
GetFullPathFromProject ("sample16.c", temp);
temp[strlen(temp)-10]=0;
strcat (temp, "log.doc");
strcat (fileName, temp);
LaunchExecutable (fileName);
break;
}
return 0;
}
//启动Word
int LaunchWord (void)
{
//调用Word_NewApplication函数创建_Application对象
//并获得创建的_Application对象的句柄
Word_NewApplication (NULL, &appHandle);
return 0;
}
//打开Doc文件
int OpenDocFile (void)
{
//定义变量
CAObjHandle tempHandle;
//获取文档句柄和文档的光标句柄
Word_GetProperty (appHandle, NULL, Word_ApplicationDocuments,
CAVT_OBJHANDLE, & tempHandle);
Word_DocumentsAdd (tempHandle, NULL, CA_DEFAULT_VAL,
CA_DEFAULT_VAL, &docHandle);
Word_GetProperty (appHandle, NULL, Word_ApplicationSelection,
CAVT_OBJHANDLE, &currSelHandle);
return 0;
}
//向Word文档中添加报表内容
int AddContent (void)
{
//定义变量styleNdxVt储存字体样式
VARIANT styleNdxVt;
//定义变量pgrphFmtHandle储存段落格式句柄
CAObjHandle pgrphFmtHandle;
char reportContent[1000];
//设置当前光标位置的字体样式为标题样式
CA_VariantSetLong (&styleNdxVt, WordConst_wdStyleTitle);
Word_SetProperty (currSelHandle, NULL, Word_SelectionStyle,
CAVT_VARIANT, styleNdxVt);
//设置段落的对齐方式
Word_GetProperty (currSelHandle, NULL,
Word_SelectionParagraphFormat,
CAVT_OBJHANDLE, &pgrphFmtHandle);
Word_SetProperty (pgrphFmtHandle, NULL,
Word_ParagraphFmtAlignment, CAVT_LONG,
WordConst_wdAlignParagraphCenter);
//输入标题
Word_SelectionTypeText (currSelHandle, NULL, "Test Report");
//输入回车键,换行
Word_SelectionTypeParagraph (currSelHandle, NULL);
//输入段落标题
Word_SelectionTypeText (currSelHandle, NULL, "Report Summary");
//设置设置当前光标位置的字体样式为标题1样式
CA_VariantSetLong (&styleNdxVt, WordConst_wdStyleHeading1);
Word_SetProperty (currSelHandle, NULL, Word_SelectionStyle,
CAVT_VARIANT, styleNdxVt);
//输入回车键,换行
Word_SelectionTypeParagraph (currSelHandle, NULL);
//设置段落左侧缩进1.27cm
Word_GetProperty (currSelHandle, NULL,
Word_SelectionParagraphFormat,
CAVT_OBJHANDLE, &pgrphFmtHandle);
Word_SetProperty (pgrphFmtHandle, NULL,
Word_ParagraphFmtLeftIndent,
CAVT_FLOAT, 36.00) ;
//获取TEXTBOX控件中的值
GetCtrlVal (panelHandle, PANEL_TEXTBOX, reportContent);
//输入正文文字
Word_SelectionTypeText (currSelHandle, NULL, reportContent);
//释放pgrphFmtHandle句柄
CA_DiscardObjHandle (pgrphFmtHandle);
return 0;
}
//储存Doc文件
int SaveDocFile (void)
{
//定义变量fileNameVt储存文件名
VARIANT fileNameVt;
char fileName[MAX_PATHNAME_LEN];
//获取该工程中文件sample16.c的路径名
GetFullPathFromProject ("sample16.c", fileName);
//修改文件名为工程目录下的log.doc文件
fileName[strlen(fileName)-10]=0;
strcat (fileName, "log.doc");
//设置fileNameVt为空
CA_VariantSetEmpty (&fileNameVt);
//将字符串数据转化成VARIANT类型数据
CA_VariantSetCString (&fileNameVt, fileName);
//存储文件
Word_DocumentSaveAs (docHandle, NULL, fileNameVt,
CA_DEFAULT_VAL, CA_DEFAULT_VAL,
CA_DEFAULT_VAL, CA_DEFAULT_VAL,
CA_DEFAULT_VAL, CA_DEFAULT_VAL,
CA_DEFAULT_VAL, CA_DEFAULT_VAL,
CA_DEFAULT_VAL, CA_DEFAULT_VAL);
//设置fileNameVt为空
CA_VariantClear (&fileNameVt);
return 0;
}
//关闭Doc文件
int CloseDocFile (void)
{
VARIANT wdSaveChangesVt ;
//释放currSelHandle句柄
CA_DiscardObjHandle (currSelHandle);
currSelHandle = 0;
//关闭Doc文件
CA_VariantSetLong (&wdSaveChangesVt, WordConst_wdDoNotSaveChanges);
Word_DocumentsClose (docHandle, NULL, wdSaveChangesVt,
CA_DEFAULT_VAL, CA_DEFAULT_VAL);
//释放docHandle句柄
CA_DiscardObjHandle (docHandle);
docHandle = 0;
return 0;
}
int ShutdownWord (void)
{
VARIANT wdSaveChangesVt;
//关闭Word应用程序
CA_VariantSetLong (&wdSaveChangesVt, WordConst_wdDoNotSaveChanges);
Word_ApplicationQuit (appHandle, NULL, wdSaveChangesVt,
CA_DEFAULT_VAL, CA_DEFAULT_VAL);
//释放appHandle句柄
CA_DiscardObjHandle (appHandle);
appHandle=0;
return 0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -