3,245
社区成员
发帖
与我相关
我的任务
分享
操作word的ole库本身都可以在mfc程序和win32程序上通用的。你下载一个mfc的范例,把程序读懂,然后就可以把里面的操作word的代码移植到win32程序了。不懂你付费给我,我帮你弄。
// TO DO: Edit the #import paths
//#pragma message ("Make sure you go to Tools.Options.Directories and add the paths to mso97.dll and vbeext1.olb. Mso97.dll will usually be in c:\Program Files\Microsoft Office\Office, and vbeext1.olb will be in c:\Program Files\Common Files\Microsoft Shared\VBA")
#import "C:\\Program Files\\Common Files\\Microsoft Shared\\Office11\\mso.dll" no_namespace rename("DocumentProperties", "DocumentPropertiesXL")
#import "C:\\Program Files\\Common Files\\Microsoft Shared\\VBA\VBA6\\VBE6EXT.OLB" no_namespace
#import "C:\\Program Files\\Microsoft Office\\OFFICE11\\excel.exe" rename("DialogBox", "DialogBoxXL") rename("RGB", "RBGXL") rename("DocumentProperties", "DocumentPropertiesXL") no_dual_interfaces
#pragma warning (disable:4192)
#include <stdio.h>
#include <tchar.h>
void dump_com_error(_com_error &e) {
_tprintf(_T("Oops - hit an error!\n"));
_tprintf(_T("\a\tCode = %08lx\n"), e.Error());
_tprintf(_T("\a\tCode meaning = %s\n"), e.ErrorMessage());
_bstr_t bstrSource(e.Source());
_bstr_t bstrDescription(e.Description());
_tprintf(_T("\a\tSource = %s\n"), (LPCTSTR) bstrSource);
_tprintf(_T("\a\tDescription = %s\n"), (LPCTSTR) bstrDescription);
}
// If this is placed in the scope of the smart pointers, they must be
// explicitly Release(d) before CoUninitialize() is called. If any reference
// count is non-zero, a protection fault will occur.
struct StartOle {
StartOle() { CoInitialize(NULL); }
~StartOle() { CoUninitialize(); }
} _inst_StartOle;
int main() {
using namespace Excel;
_ApplicationPtr pXL;
try {
pXL.CreateInstance(L"Excel.Application");
pXL->Visible = VARIANT_TRUE;
WorkbooksPtr pBooks = pXL->Workbooks;
_WorkbookPtr pBook = pBooks->Add((long)xlWorksheet);
_WorksheetPtr pSheet = pXL->ActiveSheet;
RangePtr pRange;
pRange = pSheet->Range["A21"];
pRange->Value2 = 75L;
//pRange->NumberFormatLocal = "@";
Sleep(1000);
pRange = pSheet->Range["20:20"];
pRange->Insert( (long)Excel::xlDown );
pRange->Merge();
Sleep(1000);
pBook->Saved = VARIANT_TRUE;
pXL->Quit();
} catch(_com_error &e) {
dump_com_error(e);
pXL->Quit();
}
return 0;
}
_DocumentPtr Open (
VARIANT * FileName,
VARIANT * ConfirmConversions = &vtMissing,
VARIANT * ReadOnly = &vtMissing,
VARIANT * AddToRecentFiles = &vtMissing,
VARIANT * PasswordDocument = &vtMissing,
VARIANT * PasswordTemplate = &vtMissing,
VARIANT * Revert = &vtMissing,
VARIANT * WritePasswordDocument = &vtMissing,
VARIANT * WritePasswordTemplate = &vtMissing,
VARIANT * Format = &vtMissing,
VARIANT * Encoding = &vtMissing,
VARIANT * Visible = &vtMissing,
VARIANT * OpenAndRepair = &vtMissing,
VARIANT * DocumentDirection = &vtMissing,
VARIANT * NoEncodingDialog = &vtMissing,
VARIANT * XMLTransform = &vtMissing );
//其中把参数Format 从0调整到18都不行,打开docx就异常。。。。。