|
C++/C如何用代码清楚word的历史记录
作者:wangweixing2000?
打开word,我们可以在工具->选项->常规->列出最近所用的文件 来实现我想要的功能!
1、word的_Application提供了一个方法可以设置是否显示最近使用的文件。
2、word也给我们提供RecentFiles接口,但是里面没有什么有用的。
3、word提供了Option接口,但是死活也找不到和word页面对应的方法。
没辙,我还是瞄向了第1种方法!
下面是我的简单代码:
// OfficeClear.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#import "C:\Program Files\\Common Files\\Microsoft Shared\\Office11\\MSO.DLL" rename_namespace("Office")
using namespace Office;
#import "C:\\Program Files\\Common Files\\Microsoft Shared\\VBA\\VBA6\\VBE6EXT.olb" rename_namespace("VBE6")
using namespace VBE6;
#import "C:\Program Files\Microsoft Office\Office11\MSWORD.olb" rename("ExitWindows","ExitWindowsEx"),named_guids,rename_namespace("MSWord")
using namespace MSWord;
int _tmain(int argc, _TCHAR* argv[])
{
?
//Initialize the COM libraries
?
::CoInitialize(NULL);
?// Create an instance of the Word application and obtain the
?
// pointer to the application's IDispatch interface.
?
CLSID clsid;
?
CLSIDFromProgID(L"Word.Application", &clsid);?
?IUnknown* pUnk;
?
HRESULT hr = GetActiveObject( clsid, NULL, (void**) &pUnk);
if (!SUCCEEDED(hr))
?
{
??
//如果当前没有打开word这里直接删除注册表的settings项返回就可以了!
?
}?
?
MSWord::_ApplicationPtr pApplication = pUnk;
MSWord::RecentFilesPtr pRecnts = pApplication->GetRecentFiles();
?
if (pRecnts != NULL)
?
{
??
RecentFilePtr pRecntFile = NULL;
??
int count = pRecnts->GetCount();
??
for (int i =0; i<count; i++)
??
{
???
pRecntFile = pRecnts->Item(1);
???
if (pRecntFile != NULL)
???
{
????
pRecntFile->delete();
???
}
??
}
?
}
?
pRecnts = NULL;
?
?::CoUninitialize();
?return 0;
}
这个程序还可以继续改进!
看来ms的架构还是比较清晰,没动手之前就可以猜到一二!不是为ms做广告哦!
上面程序是word2003的处理,其他版本只要把import更改一下就好了!
转帖请注明出处!谢谢!
数据库SQLite我选择我喜欢
最近由于项目的需要,需要一个小型的数据库的支持,我找到了SQLite ,它是我见到过最简单最方便的数据库,而且我可以免费获得原代码,对于SQLite的作者我更是佩服的五体投地!
SQLite的下载地址是: http://www.sqlite.org/download.html
我下载的是SQLite3,如果你只是的简单增删改的功能,那么下面几个api足够你使用的了!
typedef struct sqlite3 sqlite3; //一个标记数据对象的结构 int sqlite3_open(const char*, sqlite3**); //打开数据库,第一个参数是数据库文件名,第二个返回打开的数据库对象的结构指针 int sqlite3_open16(const void*, sqlite3**); //同样打开数据,不同的是编码方式是UTF16,上面的是UTF8的格式,需要注意的地方 int sqlite3_close(sqlite3*); //关闭数据库 const char *sqlite3_errmsg(sqlite3*); //获取最近一次操作错误信息,api中不带16标记的模式都是UTF8的格式 const void *sqlite3_errmsg16(sqlite3*); //同样不用多说 int sqlite3_errcode(sqlite3*); //这是返回错误代码 int sqlite3_exec(sqlite3 *db, const char *zSql, sqlite3_callback xCallback,void *pArg, char **pzErrMsg); //该api用的比较多 //具体意思就是执行sql语句,第三个和第四个参数用于设置回调函数的,最后一个参数返回错误信息.
还有一个比较方便的api: int sqlite3_get_table( sqlite3 *db, /* The database on which the SQL executes */ const char *zSql, /* The SQL to be executed */ char ***pazResult, /* Write the result table here */ int *pnRow, /* Write the number of rows in the result here */ int *pnColumn, /* Write the number of columns of result here */ char **pzErrMsg /* Write error messages here */ ) 这个api利用sql语句查询的时候结果直接返回到pazResult中,但是必须记着调用sqlite3_free_table(pazResult)释 放它! 好了,这个api足够你应付了,如果需要更深的了解,感觉看它的代码就好了,注视写的很全,不过都是e文,:)
说明:我下载的源代码是
该代码简单,而且就一个头文件,哈哈,下面给个简单的例子: // SQLiteDemo.cpp : 定义控制台应用程序的入口点。 //
#include "stdafx.h"
extern "C" { #include "sqlite3.h" };
//sqlite3的回调函数
// sqlite 每查到一条记录,就调用一次这个回调
int LoadMyInfo( void * para, int n_column, char ** column_value, char ** column_name )
{
//para是你在 sqlite3_exec 里传入的 void * 参数
//通过para参数,你可以传入一些特殊的指针(比如类指针、结构指针),然后在这里面强制转换成对应的类型(这里面是void*类型,必须强制转换成你的类型才可用)。然后操作这些数据 //n_column是这一条记录有多少个字段 (即这条记录有多少列) // char ** column_value 是个关键值,查出来的数据都保存在这里,它实际上是个1维数组(不要以为是2维数组),每一个元素都是一个 char * 值,是一个字段内容(用字符串来表示,以\0结尾) //char ** column_name 跟 column_value是对应的,表示这个字段的字段名称
//这里,我不使用 para 参数。忽略它的存在. int i; printf( "记录包含 %d 个字段\n", n_column );
for( i = 0 ; i < n_column; i ++ ) { printf( "字段名:%s ?> 字段值:%s\n", column_name[i], column_value[i] ); }
printf( "------------------\n" ); return 0; }
int _tmain(int argc, _TCHAR* argv[]) { sqlite3 * db = NULL; //声明sqlite关键结构指针
int result;
//打开数据库 //需要传入 db 这个指针的指针,因为 sqlite3_open 函数要为这个指针分配内存,还要让db指针指向这个内存区 result = sqlite3_open("D:\\Projects\\demo\\SQLiteDemo\\wwxSQLite.db", &db ); if( result != SQLITE_OK ) { //数据库打开失败 return -1; }
//数据库操作代码 //创建一个测试表,表名叫 MyTable_1,有2个字段: ID 和 name。其中ID是一个自动增加的类型,以后insert时可以不去指定这个字段,它会自己从0开始增加 char * errmsg = NULL; result = sqlite3_exec( db, "create table MyTable_2( ID integer primary key autoincrement, name nvarchar(32) , translate nvarchar(1024))", NULL, NULL, &errmsg ); if(result != SQLITE_OK) { printf( "创建表失败,错误码:%d,错误原因:%s\n", result, errmsg ); }
//插入一些记录 result = sqlite3_exec( db, "insert into MyTable_2( name, translate) values ( '走路', 'haha')", 0, 0, &errmsg ); if(result != SQLITE_OK ) { printf( "插入记录失败,错误码:%d,错误原因:%s\n", result, errmsg ); }
result = sqlite3_exec( db, "insert into MyTable_2( name,translate) values ( '骑单车', 'hello' )", 0, 0, &errmsg ); if(result != SQLITE_OK ) { printf( "插入记录失败,错误码:%d,错误原因:%s\n", result, errmsg ); }
result = sqlite3_exec( db, "insert into MyTable_2( name,translate ) values ( '坐汽车', 'good')", 0, 0, &errmsg ); if(result != SQLITE_OK ) { printf( "插入记录失败,错误码:%d,错误原因:%s\n", result, &errmsg ); }
//开始查询数据库,这里使用回调函数查询 result = sqlite3_exec( db, "select * from MyTable_2", LoadMyInfo, NULL, &errmsg );
//下面使用sqlite3_get_table //开始查询,传入的 dbResult 已经是 char **,这里又加了一个 & 取地址符,传递进去的就成了 char *** char ** dbResult; int nRow,nColumn; int index; result = sqlite3_get_table( db, "select * from MyTable_2", &dbResult, &nRow, &nColumn, &errmsg );
if( SQLITE_OK == result ) { //查询成功 index = nColumn; //前面说过 dbResult 前面第一行数据是字段名称,从 nColumn 索引开始才是真正的数据 printf( "查到%d条记录\n", nRow );
for(int i = 0; i < nRow ; i++ ) { printf( "第 %d 条记录\n", i+1 ); for(int j = 0 ; j < nColumn; j++ ) { printf( "字段名:%s ?> 字段值:%s\n", dbResult[j], dbResult [index] ); ++index; // dbResult 的字段值是连续的,从第0索引到第 nColumn - 1索引都是字段名称,从第 nColumn 索引开始,后面都是字段值,它
//把一个二维的表(传统的行列表示法)用一个扁平的形式来表示 } printf( "-------\n" ); } }
//到这里,不论数据库查询是否成功,都释放 char** 查询结果,使用 sqlite 提供的功能来释放 sqlite3_free_table( dbResult );
//关闭数据库 sqlite3_close( db );
system("PAUSE"); return 0; }
上面的代码是借别人的代码改的,很简单!希望对你有用,:)
作者:wangweixing2000 2007.8.24
|