//*****************************************************************************
// Labwindows/cvi8.5 word打开,新建文件,添加数据,保存,关闭的功能演示
// 参照NI公司的官方例程 Word97demo
// szlihongtao
// 2010-09-07
//*****************************************************************************
// Example program to demostrate using ActiveX Automation instrument driver to
// control Microsoft Word 8.0.
//----------------------------------------------------------------------------
#include <cvirte.h>
#include <userint.h>
#include "word97.h"
#include "toolbox.h"
#include <utility.h>
#include "worddemo.h"
//----------------------------------------------------------------------------
// Defines
//----------------------------------------------------------------------------
// errChk - Macro in toolbox.h that will goto label "Error:"
// if error returned is less than zero.
#define caErrChk errChk
#define APP_AUTOMATION_ERR "Error: Microsoft Word Automation"
#define APP_WARNING "Warning"
#define kReportTitle "Widget Test Report"
#define kRepSummarySecTitle "Report Summary"
#define kDefReportSummaryTxt "A total of 2 tests were run. The success rate "\
"was 100%. The average high limit was 32. The average low limit was "\
"25. The average test value was 0\n"
//----------------------------------------------------------------------------
#define kTableTitle " - Test Results"
#define kNumTableCols 5
#define kTestResSecTitle "Test Results"
#define kTestNoColTitle "Test #"
#define kHighLimColTitle "High Limit"
#define kLowLimColTitle "Low Limit"
#define kValColTitle "Value"
#define kResColTitle "Result"
#define kTableCaptionLabel "Table"
//----------------------------------------------------------------------------
// Variables
//----------------------------------------------------------------------------
static int panelHandle = 0; // CVI面板句柄
static CAObjHandle appHandle = 0; // A handle to our Word application.,word句柄
static CAObjHandle docHandle = 0; // A handle to our Word document.word 文档的句柄
static CAObjHandle currSelHandle = 0; // A handle to our cursor for the document.光标的句柄
static int f_appVisible = 1; // 标志位,指示word软件是否可以看得见
static VARIANT wdWordVt;
static VARIANT wdCharVt;
static VARIANT wdCellVt;
static VARIANT wdExtendVt;
static VARIANT gotoBkMrkVt;
//----------------------------------------------------------------------------
// Prototypes
//----------------------------------------------------------------------------
static HRESULT LaunchWord (CAObjHandle *appHandlePtr, int visible, int *successPtr);
static HRESULT ShutdownWord (CAObjHandle *appHandlePtr, int *successPtr);
static HRESULT SaveDocument (CAObjHandle docHandle, char *fileName);
static HRESULT AddTitleToDoc (CAObjHandle docHandle, CAObjHandle currSelHandle);
static HRESULT AddParagraphToDoc (CAObjHandle docHandle, CAObjHandle currSelHandle);
static HRESULT AddTableToDoc (CAObjHandle docHandle, CAObjHandle currSelHandle);
static HRESULT SetCurrSelLeftMargin (CAObjHandle currSelHandle, float points);
static HRESULT SetSelectionStyle (CAObjHandle docHandle, CAObjHandle currSelHandle, int styleNdx);
static HRESULT FmtAllBorders (WordObj_Borders bordersHandle, long propertyId, long propertyValue);
static HRESULT ResultsTableFillRow (CAObjHandle selInCol1Handle, int numCols, ...);
static HRESULT AddRowToTable (CAObjHandle currSelHandle, WordObj_Table resultsTableHandle,
char *testNo, char *highNo, char *lowNo,
char *valueNo, char *result);
static void ReportAppAutomationError (HRESULT hr);
static void InitVariables(void);
static int UpdateUIRDimming(int panel);
//----------------------------------------------------------------------------
//----------------------------------------------------------------------------
//----------------------------------------------------------------------------
// Main 主程序
//----------------------------------------------------------------------------
//----------------------------------------------------------------------------
//----------------------------------------------------------------------------
int main (int argc, char *argv[])
{
if (InitCVIRTE (0, argv, 0) == 0)
return -1;
// Sets the ActiveX threading style for a thread.
// 设置 ActiveX 线程的风格
CA_InitActiveXThreadStyleForCurrentThread (0, COINIT_APARTMENTTHREADED); /* Sets the ActiveX threading style for a thread. */
/*
When the User Interface Library checks for an event from the operating system,
it can put your program in the background, to sleep, for a specified period of time.
This action gives other applications more processor time, but your program might run more slowly.
*/
SetSleepPolicy (VAL_SLEEP_MORE); // 这句话可以不要
if ((panelHandle = LoadPanel (0, "worddemo.uir", PANEL)) < 0)
return -1;
UpdateUIRDimming(panelHandle); // Setup,面板上的控件是否变为灰
InitVariables(); // InitVariables 变量初始化
DisplayPanel (panelHandle); // 显示面板
RunUserInterface ();
// Cleanup
if (appHandle) // Word软件打开了吗吗?
{
SetWaitCursor (1); // 设置光标状态
if (currSelHandle) // 关闭光标
{
CA_DiscardObjHandle (currSelHandle);
currSelHandle = 0;
}
if (docHandle) // 关闭文档
{
CA_DiscardObjHandle (docHandle);
docHandle = 0;
}
ShutdownWord(&appHandle, NULL); // 关闭word软件,This will NULL out appHandle
SetWaitCursor (0); // 设置光标状态
}
return 0;
}
//----------------------------------------------------------------------------
// InitVariables 变量初始化
//----------------------------------------------------------------------------
static void InitVariables(void)
{
// Demo path and filename
GetCtrlVal (panelHandle, PANEL_VISIBILITY, &f_appVisible); // doc文件是否可视
/* Stores a long integer value in a variant and sets the type field of the variant accordingly. */
CA_VariantSetLong (&wdCharVt, WordConst_wdCharacter);
CA_VariantSetLong (&wdWordVt, WordConst_wdWord);
CA_VariantSetLong (&wdExtendVt, WordConst_wdExtend);
CA_VariantSetLong (&gotoBkMrkVt, WordConst_wdGoToBookmark);
CA_VariantSetLong (&wdCellVt, WordConst_wdCell);
return;
}
//----------------------------------------------------------------------------
// UIR Callbacks
//----------------------------------------------------------------------------
// LaunchApp 打开word软件
//----------------------------------------------------------------------------
int CVICALLBACK LaunchApp (int panel, int control, int event,
void *callbackData, int eventData1, int eventData2)
{
int f_success;
switch (event)
{
case EVENT_COMMIT:
{
SetWaitCursor (1); // 设置光标等待状态
LaunchWord (&appHandle, f_appVisible, &f_success); // 打开word
SetWaitCursor (0); // 设置光标为正常状态
UpdateUIRDimming(panel); // 更新控件的显示
/* Activates your application and brings its topmost panel to the front.
MakeApplicationActive has no effect if you have not displayed any panels.
Linux This function is not supported.
*/
MakeApplicationActive (); // 激活应用
DisplayPanel(panel); // 不知道为什么又执行一次这语句
break;
}
}
return 0;
}
//----------------------------------------------------------------------------
// ShutdownApp 关闭word软件
//----------------------------------------------------------------------------
int CVICALLBACK ShutdownApp (int panel, int control, int event,
void *callbackData, int eventData1, int eventData2)
{
int success = 0;
switch (event)
{
case EVENT_COMMIT:
{
if (currSelHandle) // 光标句柄
{
CA_DiscardObjHandle (currSelHandle);
currSelHandle = 0;
}
if (docHandle) // word文档的句柄
{
CA_DiscardObjHandle (docHandle);
docHandle = 0;
}
if (appHandle) // word句柄
{
SetWaitCursor (1); // 设定光标状态
ShutdownWord(&appHandle, &success); // This will NULL out appHandle
SetWaitCursor (0);
}
UpdateUIRDimming(panel);
break;
}
}
return 0;
}
//----------------------------------------------------------------------------
// ChangeVisibility 是否可以看见
//----------------------------------------------------------------------------
int CVICALLBACK ChangeVisibility (int panel, int control, int event,
void *callbackData, int eventData1, int eventData2)
{
HRESULT error = 0;
switch (event)
{
case EVENT_COMMIT:
{
GetCtrlVal(panel, control, &f_appVisible);
if (appHandle)
{
error = Word_SetProperty (appHandle, NULL, Word_ApplicationVisible,
CAVT_BOOL, (f_appVisible)?VTRUE:VFALSE);
if (error < 0)
ReportAppAutomationError (error);
}
break;
}
}
return 0;
}
//----------------------------------------------------------------------------
// OpenAppFile 新建空白文件
//----------------------------------------------------------------------------
int CVICALLBACK OpenAppFile (int panel, int control, int event,
void *callbackData, int eventData1, int eventData2)
{
HRESULT error = 0;
CAObjHandle docsHandle = 0;
switch (event)
{
case EVENT_COMMIT:
{
if (!docHandle)
{
// Get handle to open documents
caErrChk( Word_GetProperty (appHandle, NULL,
Word_ApplicationDocuments, // Word_ApplicationDocuments 是一个预定义的常量
CAVT_OBJHANDLE, &docsHandle)); // 属性ID
caErrChk( Word_DocumentsAdd (docsHandle, NULL, CA_DEFAULT_VAL,
CA_DEFAULT_VAL, &docHandle)); // 文件的句柄
caErrChk( Word_GetProperty (appHandle, NULL,
Word_ApplicationSelection, // Word_ApplicationSelection 是一个预定义的常量
CAVT_OBJHANDLE, &currSelHandle)); // 光标的句柄
// Update UIR
UpdateUIRDimming(panel);
}
else
MessagePopup(APP_WARNING, "Document already open");
break;
}
}
Error:
if (docsHandle)
CA_DiscardObjHandle (docsHandle);
if (error < 0)
ReportAppAutomationError (error);
return 0;
}
//----------------------------------------------------------------------------
// SaveAppFile 保存文件
//----------------------------------------------------------------------------
int CVICALLBACK SaveAppFile (int panel, int control, int event,
void *callbackData, int eventData1, int eventData2)
{
char demoFileName[MAX_PATHNAME_LEN];
switch (event)
{
case EVENT_COMMIT:
{
if (docHandle)
{
GetProjectDir (demoFileName);
if (FileSelectPopup (demoFileName, "*.doc", "*.doc",
"Save file as...", VAL_SAVE_BUTTON,
0, 1, 1, 1, demoFileName)>0)
SetWaitCursor (1); // 设置光标
SaveDocument (docHandle, demoFileName); // 保存文件
SetWaitCursor (0);
}
break;
}
}
return 0;
}
//----------------------------------------------------------------------------
// PrintAppFile 打印文件
//----------------------------------------------------------------------------
int CVICALLBACK PrintAppFile (int panel, int control, int event,
void *callbackData, int eventData1, int eventData2)
{
HRESULT error = 0;
switch (event)
{
case EVENT_COMMIT:
{
caErrChk(Word_DocumentPrintOut (docHandle, NULL, 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, CA_DEFAULT_VAL,
CA_DEFAULT_VAL, CA_DEFAULT_VAL,
CA_DEFAULT_VAL));
MessagePopup("Print", "Document printed..."); // 提示文件打印完毕
break;
}
}
Error:
if (error < 0)
ReportAppAutomationError (error);
return 0;
}
//----------------------------------------------------------------------------
// CloseAppFile 关闭文件
//----------------------------------------------------------------------------
int CVICALLBACK CloseAppFile (int panel, int control, int event,
void *callbackData, int eventData1, int eventData2)
{
HRESULT error = 0;
VARIANT wdSaveChangesVt;
switch (event)
{
case EVENT_COMMIT:
{
if (currSelHandle)
{
CA_DiscardObjHandle (currSelHandle); // 关闭光标
currSelHandle = 0;
}
if (docHandle)
{
CA_VariantSetLong (&wdSaveChangesVt, WordConst_wdDoNotSaveChanges); // WordConst_wdDoNotSaveChanges为预定义的常量
error = Word_DocumentClose (docHandle, NULL, wdSaveChangesVt,
CA_DEFAULT_VAL, CA_DEFAULT_VAL);
CA_DiscardObjHandle (docHandle);
docHandle = 0; // 句柄复位,表示文件关闭了
UpdateUIRDimming(panel);
}
break;
}
}
return 0;
}
//----------------------------------------------------------------------------
// AddTitle
//----------------------------------------------------------------------------
int CVICALLBACK AddTitle (int panel, int control, int event,
void *callbackData, int eventData1, int eventData2)
{
switch (event)
{
case EVENT_COMMIT:
SetWaitCursor (1);
AddTitleToDoc(docHandle, currSelHandle);
SetWaitCursor (0);
break;
}
return 0;
}
//----------------------------------------------------------------------------
// AddParagraph
//----------------------------------------------------------------------------
int CVICALLBACK AddParagraph (int panel, int control, int event,
void *callbackData, int eventData1, int eventData2)
{
switch (event)
{
case EVENT_COMMIT:
SetWaitCursor (1); // 设置光标状态
AddParagraphToDoc(docHandle, currSelHandle);
SetWaitCursor (0);
break;
}
return 0;
}
//----------------------------------------------------------------------------
// AddTable 加入表格内容
//----------------------------------------------------------------------------
int CVICALLBACK AddTable (int panel, int control, int event,
void *callbackData, int eventData1, int eventData2)
{
switch (event)
{
case EVENT_COMMIT:
SetWaitCursor (1); // 设置光标状态
AddTableToDoc(docHandle, currSelHandle);
SetWaitCursor (0);
break;
}
return 0;
}
//----------------------------------------------------------------------------
// Quit
//----------------------------------------------------------------------------
int CVICALLBACK Quit (int panel, int control, int event,
void *callbackData, int eventData1, int eventData2)
{
switch (event) {
case EVENT_COMMIT:
QuitUserInterface (0);
break;
}
return 0;
}
//----------------------------------------------------------------------------
// UpdateUIRDimming 更新面板上的按钮,是否变为灰
//----------------------------------------------------------------------------
static int UpdateUIRDimming(int panel)
{
SetCtrlAttribute (panel, PANEL_LAUNCHAPP, ATTR_DIMMED, (int)appHandle);
SetCtrlAttribute (panel, PANEL_SHUTDOWNAPP, ATTR_DIMMED, !(int)appHandle);
SetCtrlAttribute (panel, PANEL_OPENFILE, ATTR_DIMMED, ((int)docHandle || !(int)appHandle));
SetCtrlAttribute (panel, PANEL_PRINTFILE, ATTR_DIMMED, !(int)docHandle);
SetCtrlAttribute (panel, PANEL_SAVEFILE, ATTR_DIMMED, !(int)docHandle);
SetCtrlAttribute (panel, PANEL_CLOSEFILE, ATTR_DIMMED, !(int)docHandle);
SetCtrlAttribute (panel, PANEL_ADDTITLE, ATTR_DIMMED, !(int)docHandle);
SetCtrlAttribute (panel, PANEL_ADDPARAGRAPH, ATTR_DIMMED, !(int)docHandle);
SetCtrlAttribute (panel, PANEL_ADDTABLE, ATTR_DIMMED, !(int)docHandle);
return 0;
}
//----------------------------------------------------------------------------
// LaunchWord 打开word
//----------------------------------------------------------------------------
// NOTE: We've encountered a problem with launching Word 8.0 from our Dell machines.
// The registry entries indicating the server application for Word objects
// were incorrect. This function is provided as a way of working around this
// rather than re-installing Word or editing the registry.
//----------------------------------------------------------------------------
#define APP_LAUNCH_ERROR -2146959355
#define kManuallyLaunchWordMsg "An error was encountered launching Word 8.0. Launch it manually and try again."
static HRESULT LaunchWord (CAObjHandle *appHandlePtr, int visible, int *successPtr)
{
int success = 0;
HRESULT error = 0;
error = Word_NewApplication (NULL, 1, LOCALE_NEUTRAL, 0, appHandlePtr);
if (error == APP_LAUNCH_ERROR)
{
// Try getting the active word application.
error = Word_ActiveApplication (NULL, 1, LOCALE_NEUTRAL, 0, appHandlePtr);
if (error != 0)
{
MessagePopup (APP_AUTOMATION_ERR, kManuallyLaunchWordMsg);
error = 0;
goto Error;
}
}
if (error < 0)
goto Error;
// Make visible
caErrChk (Word_SetProperty (appHandle, NULL, Word_ApplicationVisible,
CAVT_BOOL, (visible)?VTRUE:VFALSE));
success = (error == 0);
Error:
if (successPtr)
*successPtr = success;
if (error < 0)
ReportAppAutomationError (error);
return error;
}
//----------------------------------------------------------------------------
// ShutdownWord 关闭word软件
//----------------------------------------------------------------------------
static HRESULT ShutdownWord (CAObjHandle *appHandlePtr, int *successPtr)
{
HRESULT error = 0;
VARIANT wdSaveChangesVt;
CA_VariantSetLong (&wdSaveChangesVt, WordConst_wdDoNotSaveChanges);
Word_ApplicationQuit (*appHandlePtr, NULL, wdSaveChangesVt,
CA_DEFAULT_VAL, CA_DEFAULT_VAL);
CA_DiscardObjHandle (*appHandlePtr);
*appHandlePtr = 0;
if (successPtr)
*successPtr = (error == 0);
if (error < 0)
ReportAppAutomationError (error);
return error;
}
//----------------------------------------------------------------------------
// SaveDocument
//----------------------------------------------------------------------------
static HRESULT SaveDocument (CAObjHandle docHandle, char *fileName)
{
HRESULT error = 0;
VARIANT fileNameVt;
CA_VariantSetEmpty (&fileNameVt);
caErrChk (CA_VariantSetCString (&fileNameVt, fileName));
caErrChk (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));
Error:
CA_VariantClear (&fileNameVt);
if (error < 0)
ReportAppAutomationError (error);
return error;
}
//----------------------------------------------------------------------------
// AddTitleToDoc 文件新增加标题
//----------------------------------------------------------------------------
static HRESULT AddTitleToDoc (CAObjHandle docHandle, CAObjHandle currSelHandle)
{
CAObjHandle pgrphFmtHandle = 0;
HRESULT error = 0;
int i;
struct STR1
{
int wdAlign;
char string[64];
};
const struct STR1 arr_wdAlign[5]=
{
WordConst_wdAlignParagraphLeft, "WordConst_wdAlignParagraphLeft ", // 设定文本从左边对齐显示
WordConst_wdAlignParagraphCenter, "WordConst_wdAlignParagraphCenter ", // 设定文本显示居中
WordConst_wdAlignParagraphRight, "WordConst_wdAlignParagraphRight ", // 设定文本从右边对齐显示
WordConst_wdAlignParagraphJustify, "WordConst_wdAlignParagraphJustify ", // 实际显示效果为 “从左边对齐”
WordConst_wdAlignParagraphCenter, "WordConst_wdAlignParagraphCenter ", // 设定文本显示居中
};
caErrChk (SetSelectionStyle (docHandle, currSelHandle,
WordConst_wdStyleTitle)); // WordConst_wdStyleTitle 是预定义的常数
caErrChk (Word_GetProperty (currSelHandle, NULL,
Word_SelectionParagraphFormat, // Word_SelectionParagraphFormat 是预定义的常数
CAVT_OBJHANDLE, &pgrphFmtHandle));
for(i=0;i<5;++i)
{
caErrChk (Word_SetProperty (pgrphFmtHandle, NULL, Word_ParagraphFmtAlignment, CAVT_LONG, arr_wdAlign.wdAlign));
caErrChk (Word_SelectionTypeText (currSelHandle, NULL, kReportTitle)); // 正式的加入 字符串 进入 word
caErrChk (Word_SelectionTypeParagraph (currSelHandle, NULL)); // 换行
}
caErrChk (Word_SelectionTypeText (currSelHandle, NULL, kReportTitle)); // 正式的加入 字符串 进入 word
caErrChk (Word_SelectionTypeParagraph (currSelHandle, NULL));
Error:
if (pgrphFmtHandle) // 正常情况下 pgrphFmtHandle > 0
CA_DiscardObjHandle (pgrphFmtHandle);
if (error < 0) // 正常情况下 error>=0
ReportAppAutomationError (error);
return error;
}
//----------------------------------------------------------------------------
// AddParagraphToDoc 文件新增加段落内容
//----------------------------------------------------------------------------
static HRESULT AddParagraphToDoc (CAObjHandle docHandle, CAObjHandle currSelHandle)
{
CAObjHandle fontHandle = 0; // 设置字体的句柄
HRESULT error = 0;
int i;
struct STR1
{
int wdStyle;
char string[64];
};
const struct STR1 arr_styleNdx[92]=
{
WordConst_wdStyleNormal, "WordConst_wdStyleNormal ",
WordConst_wdStyleEnvelopeAddress, "WordConst_wdStyleEnvelopeAddress ",
WordConst_wdStyleEnvelopeReturn, "WordConst_wdStyleEnvelopeReturn ",
WordConst_wdStyleBodyText, "WordConst_wdStyleBodyText ",
WordConst_wdStyleHeading1, "WordConst_wdStyleHeading1 ", // 左边对齐,黑体,Times New Roman,2号
WordConst_wdStyleHeading2, "WordConst_wdStyleHeading2 ", // 左边对齐,黑体,Arial,3号
WordConst_wdStyleHeading3, "WordConst_wdStyleHeading3 ", // 左边对齐,黑体,Times New Roman,3号
WordConst_wdStyleHeading4, "WordConst_wdStyleHeading4 ", // 左边对齐,黑体,Arial,4号
WordConst_wdStyleHeading5, "WordConst_wdStyleHeading5 ", // 左边对齐,黑体,Times New Roman,4号
WordConst_wdStyleHeading6, "WordConst_wdStyleHeading6 ", // 左边对齐,黑体,Arial,小4号
WordConst_wdStyleHeading7, "WordConst_wdStyleHeading7 ", // 左边对齐,黑体,Times New Roman,小4号
WordConst_wdStyleHeading8, "WordConst_wdStyleHeading8 ",
WordConst_wdStyleHeading9, "WordConst_wdStyleHeading9 ",
WordConst_wdStyleIndex1, "WordConst_wdStyleIndex1 ",
WordConst_wdStyleIndex2, "WordConst_wdStyleIndex2 ",
WordConst_wdStyleIndex3, "WordConst_wdStyleIndex3 ",
WordConst_wdStyleIndex4, "WordConst_wdStyleIndex4 ",
WordConst_wdStyleIndex5, "WordConst_wdStyleIndex5 ",
WordConst_wdStyleIndex6, "WordConst_wdStyleIndex6 ",
WordConst_wdStyleIndex7, "WordConst_wdStyleIndex7 ",
WordConst_wdStyleIndex8, "WordConst_wdStyleIndex8 ",
WordConst_wdStyleIndex9, "WordConst_wdStyleIndex9 ",
WordConst_wdStyleTOC1, "WordConst_wdStyleTOC1 ",
WordConst_wdStyleTOC2, "WordConst_wdStyleTOC2 ",
WordConst_wdStyleTOC3, "WordConst_wdStyleTOC3 ",
WordConst_wdStyleTOC4, "WordConst_wdStyleTOC4 ",
WordConst_wdStyleTOC5, "WordConst_wdStyleTOC5 ",
WordConst_wdStyleTOC6, "WordConst_wdStyleTOC6 ",
WordConst_wdStyleTOC7, "WordConst_wdStyleTOC7 ",
WordConst_wdStyleTOC8, "WordConst_wdStyleTOC8 ",
WordConst_wdStyleTOC9, "WordConst_wdStyleTOC9 ",
WordConst_wdStyleNormalIndent, "WordConst_wdStyleNormalIndent ",
WordConst_wdStyleFootnoteText, "WordConst_wdStyleFootnoteText ",
WordConst_wdStyleCommentText, "WordConst_wdStyleCommentText ",
WordConst_wdStyleHeader, "WordConst_wdStyleHeader",
WordConst_wdStyleFooter, "WordConst_wdStyleFooter ",
WordConst_wdStyleIndexHeading, "WordConst_wdStyleIndexHeading ",
WordConst_wdStyleCaption, "WordConst_wdStyleCaption ",
WordConst_wdStyleTableOfFigures, "WordConst_wdStyleTableOfFigures ",
WordConst_wdStyleFootnoteReference, "WordConst_wdStyleFootnoteReference ",
WordConst_wdStyleCommentReference, "WordConst_wdStyleCommentReference ",
WordConst_wdStyleLineNumber, "WordConst_wdStyleLineNumber ",
WordConst_wdStylePageNumber, "WordConst_wdStylePageNumber ",
WordConst_wdStyleEndnoteReference, "WordConst_wdStyleEndnoteReference ",
WordConst_wdStyleEndnoteText, "WordConst_wdStyleEndnoteText ",
WordConst_wdStyleTableOfAuthorities, "WordConst_wdStyleTableOfAuthorities ",
WordConst_wdStyleMacroText , "WordConst_wdStyleMacroText",
WordConst_wdStyleTOAHeading, "WordConst_wdStyleTOAHeading ",
WordConst_wdStyleList , "WordConst_wdStyleList ",
WordConst_wdStyleListBullet , "WordConst_wdStyleListBullet ",
WordConst_wdStyleListNumber , "WordConst_wdStyleListNumber ",
WordConst_wdStyleList2 , "WordConst_wdStyleList2 ",
WordConst_wdStyleList3, "WordConst_wdStyleList3 ",
WordConst_wdStyleList4 , "WordConst_wdStyleList4 ",
WordConst_wdStyleList5 , "WordConst_wdStyleList5 ",
WordConst_wdStyleListBullet2 , "WordConst_wdStyleListBullet2 ",
WordConst_wdStyleListBullet3 , "WordConst_wdStyleListBullet3 ",
WordConst_wdStyleListBullet4 , "WordConst_wdStyleListBullet4 ",
WordConst_wdStyleListBullet5 , "WordConst_wdStyleListBullet5 ",
WordConst_wdStyleListNumber2 , "WordConst_wdStyleListNumber2 ",
WordConst_wdStyleListNumber3 , "WordConst_wdStyleListNumber3 ",
WordConst_wdStyleListNumber4 , "WordConst_wdStyleListNumber4 ",
WordConst_wdStyleListNumber5 , "WordConst_wdStyleListNumber5 ",
WordConst_wdStyleTitle , "WordConst_wdStyleTitle ",
WordConst_wdStyleClosing , "WordConst_wdStyleClosing ",
WordConst_wdStyleSignature , "WordConst_wdStyleSignature ",
WordConst_wdStyleDefaultParagraphFont , "WordConst_wdStyleDefaultParagraphFont ",
WordConst_wdStyleBodyTextIndent , "WordConst_wdStyleBodyTextIndent ",
WordConst_wdStyleListContinue , "WordConst_wdStyleListContinue ",
WordConst_wdStyleListContinue2, "WordConst_wdStyleListContinue2 ",
WordConst_wdStyleListContinue3 , "WordConst_wdStyleListContinue3 ",
WordConst_wdStyleListContinue4 , "WordConst_wdStyleListContinue4 ",
WordConst_wdStyleListContinue5 , "WordConst_wdStyleListContinue5 ",
WordConst_wdStyleMessageHeader, "WordConst_wdStyleMessageHeader ",
WordConst_wdStyleSubtitle , "WordConst_wdStyleSubtitle ",
WordConst_wdStyleSalutation , "WordConst_wdStyleSalutation ",
WordConst_wdStyleDate , "WordConst_wdStyleDate ",
WordConst_wdStyleBodyTextFirstIndent , "WordConst_wdStyleBodyTextFirstIndent ",
WordConst_wdStyleBodyTextFirstIndent2 , "WordConst_wdStyleBodyTextFirstIndent2 ",
WordConst_wdStyleNoteHeading , "WordConst_wdStyleNoteHeading ",
WordConst_wdStyleBodyText2 , "WordConst_wdStyleBodyText2 ",
WordConst_wdStyleBodyText3 , "WordConst_wdStyleBodyText3 ",
WordConst_wdStyleBodyTextIndent2 , "WordConst_wdStyleBodyTextIndent2 ",
WordConst_wdStyleBodyTextIndent3 , "WordConst_wdStyleBodyTextIndent3 ",
WordConst_wdStyleBlockQuotation , "WordConst_wdStyleBlockQuotation ",
WordConst_wdStyleHyperlink , " WordConst_wdStyleHyperlink",
WordConst_wdStyleHyperlinkFollowed , "WordConst_wdStyleHyperlinkFollowed ",
WordConst_wdStyleStrong , "WordConst_wdStyleStrong ",
WordConst_wdStyleEmphasis , "WordConst_wdStyleEmphasis ",
WordConst_wdStyleNavPane , "WordConst_wdStyleNavPane ",
WordConst_wdStylePlainText , "WordConst_wdStylePlainText ",
WordConst_wdStyleHeading1, "WordConst_wdStyleHeading1 ", // 左边对齐,黑体,Times New Roman,2号
};
for(i=0;i<92;++i)
{
/* Create the section heading. */
caErrChk (Word_SelectionTypeText (currSelHandle, NULL, arr_styleNdx.string));
caErrChk (SetSelectionStyle (docHandle, currSelHandle, arr_styleNdx.wdStyle));
caErrChk (Word_SelectionTypeParagraph (currSelHandle, NULL)); // word光标移到下面一行
}
/* Write the default paragraph text. */
caErrChk (SetCurrSelLeftMargin (currSelHandle, (float) 36)); // 设置左边距为36
caErrChk (Word_SelectionTypeText (currSelHandle, NULL, kDefReportSummaryTxt)); // 写入真正的段落内容
// 因为 kDefReportSummaryTxt 字符串已经包含了换行符 \n ,所以就不需要再设置一次光标了
// caErrChk (Word_SelectionTypeParagraph (currSelHandle, NULL)); // word光标移到下面一行
caErrChk (Word_SelectionTypeText (currSelHandle, NULL, kDefReportSummaryTxt)); // 写入真正的段落内容
//-------------------------------------------------------------------------------------------------------------
caErrChk (SetCurrSelLeftMargin (currSelHandle, 0.0)); // 设置左边距为0
caErrChk (Word_SelectionTypeText (currSelHandle, NULL, kDefReportSummaryTxt)); // 写入真正的段落内容
Error:
if (fontHandle) // 本子程序没有使用这个句柄
CA_DiscardObjHandle (fontHandle);
if (error < 0)
ReportAppAutomationError (error);
return error;
}
//----------------------------------------------------------------------------
// AddTableToDoc 文件新增加表格
//----------------------------------------------------------------------------
static HRESULT AddTableToDoc (CAObjHandle docHandle, CAObjHandle currSelHandle)
{
long cnt;
VARIANT cntVt, wdCapBelowVt, tableCapVt;
WordObj_Table resultsTableHandle;
WordObj_Range rangeHandle = 0;
WordObj_Tables tablesHandle = 0;
WordObj_Columns columnsHandle = 0;
WordObj_Borders bordersHandle = 0;
WordObj_Border borderHandle = 0;
CAObjHandle pgrphFmtHandle = 0;
HRESULT error = 0;
CA_VariantSetEmpty (&tableCapVt);
/* Create the section heading. */
caErrChk (Word_SelectionTypeText (currSelHandle, NULL,kTestResSecTitle)); // 写入段落的标题
caErrChk (SetSelectionStyle (docHandle, currSelHandle, WordConst_wdStyleHeading1)); // 黑体显示,左边对齐
caErrChk (Word_SelectionTypeParagraph (currSelHandle, NULL)); //换行
/* Create the table. Store the handle for future use. */
caErrChk (Word_GetProperty (currSelHandle, NULL,
Word_SelectionRange, CAVT_OBJHANDLE,
&rangeHandle));
caErrChk (Word_GetProperty (docHandle, NULL, Word_DocumentTables,
CAVT_OBJHANDLE, &tablesHandle)); // 表格句柄
#if 0 // 设定表格的内容居中显示
caErrChk (Word_GetProperty (currSelHandle, NULL,
Word_SelectionParagraphFormat,
CAVT_OBJHANDLE, &pgrphFmtHandle));
caErrChk (Word_SetProperty (pgrphFmtHandle, NULL,
Word_ParagraphFmtAlignment, CAVT_LONG,
WordConst_wdAlignParagraphCenter)); // 表格居中显示
#else
// 默认情况下,左对齐
#endif
caErrChk (Word_TablesAdd (tablesHandle, NULL, rangeHandle, 1, kNumTableCols, // 表格有5列
&resultsTableHandle));
caErrChk (Word_GetProperty (resultsTableHandle, NULL, Word_TableColumns, // Word_TableColumns 是预定义的常量
CAVT_OBJHANDLE, &columnsHandle));
caErrChk (Word_GetProperty (columnsHandle, NULL, Word_ColumnsCount, // Word_ColumnsCount 是预定义的常量
CAVT_LONG, &cnt)); // 仿真,cnt=5
CA_VariantSetLong (&cntVt, cnt);
caErrChk (Word_SelectionMoveRight (currSelHandle, NULL, wdWordVt, // 整行显示均变黑
cntVt, wdExtendVt, NULL));
/* Fill in the column titles. */
caErrChk (Word_SelectionHomeKey (currSelHandle, NULL, CA_DEFAULT_VAL,
CA_DEFAULT_VAL, NULL));
caErrChk (ResultsTableFillRow (currSelHandle, kNumTableCols,
kTestNoColTitle, kHighLimColTitle,
kLowLimColTitle, kValColTitle,
kResColTitle));
/* Fill in the column data */
caErrChk(AddRowToTable (currSelHandle, resultsTableHandle,
"000001", "100.000", "32.0", "39.123", "0"));
caErrChk(AddRowToTable (currSelHandle, resultsTableHandle,
"000002", " 99.999", "31.0", "41.321", "0"));
caErrChk(AddRowToTable (currSelHandle, resultsTableHandle,
"000003", " 98.345", "33.0", "40.456", "0"));
/* Add the table caption. */
CA_VariantSetLong (&wdCapBelowVt, WordConst_wdCaptionPositionBelow); // WordConst_wdCaptionPositionBelow是预定义的常量
CA_VariantSetCString (&tableCapVt, kTableCaptionLabel); // 变量赋值,#define kTableCaptionLabel "Table"
#if 0
caErrChk (Word_SelectionInsertCaption (currSelHandle, NULL,
tableCapVt,
CA_DEFAULT_VAL,
CA_DEFAULT_VAL, wdCapBelowVt));
// 不知道是什么原因,这句话总是执行出错
#endif
caErrChk (Word_GetProperty (currSelHandle, NULL,
Word_SelectionParagraphFormat,
CAVT_OBJHANDLE, &pgrphFmtHandle));
caErrChk (Word_SetProperty (pgrphFmtHandle, NULL,
Word_ParagraphFmtAlignment, CAVT_LONG,
WordConst_wdAlignParagraphCenter)); // 表格居中显示
caErrChk (Word_SelectionTypeText (currSelHandle, NULL,
kTableTitle));
caErrChk (Word_SelectionTypeParagraph (currSelHandle, NULL)); // 换行
caErrChk (Word_GetProperty (columnsHandle, NULL, Word_SelectionBorders,
CAVT_OBJHANDLE, &bordersHandle));
caErrChk (FmtAllBorders (bordersHandle, Word_BorderLineStyle,
WordConst_wdLineStyleSingle));
caErrChk (FmtAllBorders (bordersHandle, Word_BorderLineWidth, // Word_BorderLineWidth 是预定义的常量
WordConst_wdLineWidth050pt)); // 设置边界线条的线宽
Error:
if (error < 0)
ReportAppAutomationError (error);
if (rangeHandle)
CA_DiscardObjHandle (rangeHandle);
if (tablesHandle)
CA_DiscardObjHandle (tablesHandle);
if (columnsHandle)
CA_DiscardObjHandle (columnsHandle);
if (bordersHandle)
CA_DiscardObjHandle (bordersHandle);
if (borderHandle)
CA_DiscardObjHandle (borderHandle);
if (pgrphFmtHandle)
CA_DiscardObjHandle (pgrphFmtHandle);
if (resultsTableHandle)
CA_DiscardObjHandle (resultsTableHandle);
CA_VariantClear (&tableCapVt);
return error;
}
//----------------------------------------------------------------------------
// AddRowToTable
//----------------------------------------------------------------------------
static HRESULT AddRowToTable (CAObjHandle currSelHandle, WordObj_Table resultsTableHandle,
char *testNo, char *highNo, char *lowNo,
char *valueNo, char *result)
{
int numRows;
WordObj_Rows rowsHandle = 0;
WordObj_Row rowHandle = 0, newRowHandle = 0;
HRESULT error = 0;
caErrChk (Word_GetProperty (resultsTableHandle, NULL, Word_TableRows,
CAVT_OBJHANDLE, &rowsHandle));
caErrChk (Word_GetProperty (rowsHandle, NULL, Word_RowsCount, CAVT_LONG,
&numRows));
caErrChk (Word_RowsItem (rowsHandle, NULL, numRows, &rowHandle));
caErrChk (Word_RowSelect (rowHandle, NULL));
caErrChk (Word_SelectionMoveRight (currSelHandle, NULL, wdCharVt,
CA_DEFAULT_VAL,
CA_DEFAULT_VAL, NULL));
caErrChk (Word_SelectionMoveLeft (currSelHandle, NULL, wdCharVt,
CA_DEFAULT_VAL,
CA_DEFAULT_VAL, NULL));
caErrChk (Word_RowsAdd (rowsHandle, NULL, CA_DEFAULT_VAL,
&newRowHandle));
caErrChk (Word_RowSelect (newRowHandle, NULL));
caErrChk (Word_SelectionHomeKey (currSelHandle, NULL, CA_DEFAULT_VAL,
CA_DEFAULT_VAL, NULL));
caErrChk (ResultsTableFillRow (currSelHandle, kNumTableCols, testNo, highNo,
lowNo, valueNo, result));
Error:
if (rowsHandle)
CA_DiscardObjHandle (rowsHandle);
if (rowHandle)
CA_DiscardObjHandle (rowHandle);
if (newRowHandle)
CA_DiscardObjHandle (newRowHandle);
return error;
}
//----------------------------------------------------------------------------
// ResultsTableFillRow
//----------------------------------------------------------------------------
static HRESULT ResultsTableFillRow (CAObjHandle selInCol1Handle,
int numCols, ...)
{
char *currText;
va_list parmInfo;
int ndx;
HRESULT error = 0;
va_start (parmInfo, numCols);
for (ndx = 0; ndx < numCols - 1; ++ndx) {
currText = va_arg(parmInfo, char *);
caErrChk (Word_SelectionTypeText (selInCol1Handle, NULL, currText));
caErrChk (Word_SelectionMoveRight (selInCol1Handle, NULL, wdCellVt,
CA_DEFAULT_VAL,
CA_DEFAULT_VAL, NULL));
}
currText = va_arg(parmInfo, char *);
caErrChk (Word_SelectionTypeText (selInCol1Handle, NULL, currText));
Error:
va_end (parmInfo);
return error;
}
//----------------------------------------------------------------------------
// SetCurrSelLeftMargin
//----------------------------------------------------------------------------
static HRESULT SetCurrSelLeftMargin (CAObjHandle currSelHandle, float points)
{
CAObjHandle pgrphFmtHandle = 0;
HRESULT error = 0;
caErrChk (Word_GetProperty (currSelHandle, NULL,
Word_SelectionParagraphFormat,
CAVT_OBJHANDLE, &pgrphFmtHandle));
caErrChk (Word_SetProperty (pgrphFmtHandle, NULL,
Word_ParagraphFmtLeftIndent, CAVT_FLOAT,
points));
Error:
if (pgrphFmtHandle)
CA_DiscardObjHandle (pgrphFmtHandle);
return error;
}
//----------------------------------------------------------------------------
// SetSelectionStyle
//----------------------------------------------------------------------------
static HRESULT SetSelectionStyle (CAObjHandle docHandle,CAObjHandle currSelHandle,int styleNdx)
{
VARIANT styleNdxVt;
HRESULT error = 0;
CA_VariantSetLong (&styleNdxVt, styleNdx);
caErrChk (Word_SetProperty (currSelHandle, NULL, Word_SelectionStyle, // Word_SelectionStyle是预先定义的常量
CAVT_VARIANT, styleNdxVt));
Error:
return error;
}
//----------------------------------------------------------------------------
// FmtAllBorders
//----------------------------------------------------------------------------
static HRESULT FmtAllBorders (WordObj_Borders bordersHandle, long propertyId,
long propertyValue)
{
WordObj_Border borderHandle = 0;
HRESULT error = 0;
caErrChk (Word_BordersItem (bordersHandle, NULL, -1, &borderHandle));
caErrChk (Word_SetProperty (borderHandle, NULL, propertyId, CAVT_LONG,
propertyValue));
CA_DiscardObjHandle (borderHandle);
borderHandle = 0;
caErrChk (Word_BordersItem (bordersHandle, NULL, -2, &borderHandle));
caErrChk (Word_SetProperty (borderHandle, NULL, propertyId, CAVT_LONG,
propertyValue));
CA_DiscardObjHandle (borderHandle);
borderHandle = 0;
caErrChk (Word_BordersItem (bordersHandle, NULL, -3, &borderHandle));
caErrChk (Word_SetProperty (borderHandle, NULL, propertyId, CAVT_LONG,
propertyValue));
CA_DiscardObjHandle (borderHandle);
borderHandle = 0;
caErrChk (Word_BordersItem (bordersHandle, NULL, -4, &borderHandle));
caErrChk (Word_SetProperty (borderHandle, NULL, propertyId, CAVT_LONG,
propertyValue));
CA_DiscardObjHandle (borderHandle);
borderHandle = 0;
Error:
if (borderHandle)
CA_DiscardObjHandle (borderHandle);
return error;
}
//----------------------------------------------------------------------------
// ReportWordAutomationError
//----------------------------------------------------------------------------
static void ReportAppAutomationError (HRESULT hr)
{
char errorBuf[256];
if (hr < 0)
{
CA_GetAutomationErrorString (hr, errorBuf, sizeof (errorBuf));
MessagePopup (APP_AUTOMATION_ERR, errorBuf);
}
return;
}
//*****************************************************************************
//*****************************************************************************
//*****************************************************************************
用户1724215 2014-1-9 15:36