导航
  • 主页
  • VC综合技术
  • 互联网技术
  • MFC AppLauncher
  • .NET 技术
  • 界面
  • 进程
  • 算法
  • 硬件/系统
  • 数据库
  • VC++技术资源

VC编程控制多版本Word(97,2000,XP,2003),高手,请教了!

snake1122 2004-04-07 09:11:38
不同操作系统,安装的路径不一样,还有不同版本Word所使用的库不一样,这些问题该如何处理呢?做过的大侠给个思路!
就是该如何#import *.olb,涉及路径和名称不一样。

这样行不行,
#if defined( word97)
#import "...\office97\...\*.olb" //没有97,那位仁兄给个路径和名称
#elif defined (word2000)
#import ""
...
#endif
好像也不行,defined(*),*在程序开始我没法判断?

大家讨论,
分不够再加!
...全文
72 点赞 收藏 3
写回复
3 条回复
切换为时间正序
当前发帖距今超过3年,不再开放新的回复
发表回复
1HelloWorld 2004-04-09
// Fotodocword.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <ole2.h>


//
// AutoWrap() - Automation helper function...
//
HRESULT AutoWrap(int autoType, VARIANT *pvResult, IDispatch *pDisp,
LPOLESTR ptName, int cArgs...)
{
// Begin variable-argument list...
va_list marker;
va_start(marker, cArgs);

if(!pDisp) {
MessageBox(NULL, "NULL IDispatch passed to AutoWrap()",
"Error", 0x10010);
_exit(0);
}

// Variables used...
DISPPARAMS dp = { NULL, NULL, 0, 0 };
DISPID dispidNamed = DISPID_PROPERTYPUT;
DISPID dispID;
HRESULT hr;
char buf[200];
char szName[200];

// Convert down to ANSI
WideCharToMultiByte(CP_ACP, 0, ptName, -1, szName, 256, NULL, NULL);

// Get DISPID for name passed...
hr = pDisp->GetIDsOfNames(IID_NULL, &ptName, 1, LOCALE_USER_DEFAULT,
&dispID);
if(FAILED(hr)) {
sprintf(buf,
"IDispatch::GetIDsOfNames(\"%s\") failed w/err0x%08lx",
szName, hr);
MessageBox(NULL, buf, "AutoWrap()", 0x10010);
_exit(0);
return hr;
}

// Allocate memory for arguments...
VARIANT *pArgs = new VARIANT[cArgs+1];

// Extract arguments...
for(int i=0; i<cArgs; i++) {
pArgs[i] = va_arg(marker, VARIANT);
}

// Build DISPPARAMS
dp.cArgs = cArgs;
dp.rgvarg = pArgs;

// Handle special-case for property-puts!
if(autoType & DISPATCH_PROPERTYPUT) {
dp.cNamedArgs = 1;
dp.rgdispidNamedArgs = &dispidNamed;
}

// Make the call!
hr = pDisp->Invoke(dispID, IID_NULL, LOCALE_SYSTEM_DEFAULT, autoType,
&dp, pvResult, NULL, NULL);
if(FAILED(hr)) {
sprintf(buf,
"IDispatch::Invoke(\"%s\"=%08lx) failed w/err 0x%08lx",
szName, dispID, hr);
MessageBox(NULL, buf, "AutoWrap()", 0x10010);
_exit(0);
return hr;
}
// End variable-argument section...
va_end(marker);

delete [] pArgs;

return hr;

}

int main(int argc, char* argv[])
{
// Initialize COM for this thread...
CoInitialize(NULL);

// Get CLSID for Word.Application...
CLSID clsid;
HRESULT hr = CLSIDFromProgID(L"Word.Application", &clsid);
if(FAILED(hr)) {
::MessageBox(NULL, "CLSIDFromProgID() failed", "Error",
0x10010);
return -1;
}

// Start Word and get IDispatch...
IDispatch *pWordApp;
hr = CoCreateInstance(clsid, NULL, CLSCTX_LOCAL_SERVER,
IID_IDispatch, (void **)&pWordApp);
if(FAILED(hr)) {
::MessageBox(NULL, "Word not registered properly",
"Error", 0x10010);
return -2;
}



// Get Documents collection
IDispatch *pDocs;
{
VARIANT result;
VariantInit(&result);
AutoWrap(DISPATCH_PROPERTYGET, &result, pWordApp, L"Documents",
0);

pDocs = result.pdispVal;
}


//cai begin

IDispatch *pBookMark;
{
VARIANT result;
VariantInit(&result);
//VARIANT x,y;
//x.vt = VT_I4;
//x.lVal = 0;
//x.vt=VT_ERROR;
//x.lVal=DISP_E_PARAMNOTFOUND;

// VARIANT used in place of optional-parameters.
VARIANT varOpt;
varOpt.vt = VT_ERROR;
varOpt.scode = (long)DISP_E_PARAMNOTFOUND;

//y.vt = VT_BSTR;
//y.bstrVal = ::SysAllocString(L"d:\\doctest1.doc");

//AutoWrap(DISPATCH_METHOD, &result, pDocs, L"Add", 3,y,x,x);
AutoWrap(DISPATCH_METHOD, &result, pDocs, L"Add", 4,varOpt,varOpt,varOpt,varOpt);
pBookMark = result.pdispVal;

// SysFreeString(x.lVal);
//SysFreeString(y.bstrVal);

}

/* IDispatch *pDoc;
{
VARIANT result;
VariantInit(&result);
VARIANT x;
x.vt = VT_BSTR;
x.bstrVal = ::SysAllocString(L"C:\\Doc1.doc");

AutoWrap(DISPATCH_METHOD, &result, pDocs, L"Open", 1, x);
pDoc = result.pdispVal;
SysFreeString(x.bstrVal);
}*/

IDispatch *pRang;
{
VARIANT result;
VariantInit(&result);
VARIANT x,y;
x.vt = VT_BSTR;
x.bstrVal = ::SysAllocString(L"0");
y.vt = VT_BSTR;
y.bstrVal = ::SysAllocString(L"0");

AutoWrap(DISPATCH_METHOD, &result, pBookMark, L"Range", 2, x, y);
pRang = result.pdispVal;
SysFreeString(x.bstrVal);
SysFreeString(y.bstrVal);
}

{
VARIANT result;
VariantInit(&result);
VARIANT x,y;
x.vt = VT_BSTR;
x.bstrVal = ::SysAllocString(L"just a test!");
y.vt = VT_BSTR;
y.bstrVal = ::SysAllocString(L"d:\\docment1.doc");

AutoWrap(DISPATCH_METHOD, &result, pRang, L"InsertAfter", 1, x);
AutoWrap(DISPATCH_METHOD, &result, pBookMark, L"SaveAs", 1, y);
//pRang = result.pdispVal;
SysFreeString(x.bstrVal);
SysFreeString(y.bstrVal);
}

//cai end


// Close the document without saving changes and quit Word
{
VARIANT x;
x.vt = VT_BOOL;
x.boolVal = false;
//AutoWrap(DISPATCH_METHOD, NULL, pDoc, L"Close", 1, x);
AutoWrap(DISPATCH_METHOD, NULL, pBookMark, L"Close", 1, x);
AutoWrap(DISPATCH_METHOD, NULL, pWordApp, L"Quit", 0);
}

// Cleanup
pBookMark->Release();
pRang->Release();
//pDoc->Release();
pDocs->Release();
pWordApp->Release();


// Uninitialize COM for this thread...
CoUninitialize();

return 0;
}

回复
snake1122 2004-04-07
字面上的意思是else if
回复
OLIVERY 2004-04-07
#elif应该是elseif吧
回复
发动态
发帖子
VC/MFC
创建于2007-09-28

1.5w+

社区成员

VC/MFC相关问题讨论
申请成为版主
社区公告

        VC/MFC社区版块或许是CSDN最“古老”的版块了,记忆之中,与CSDN的年龄几乎差不多。随着时间的推移,MFC技术渐渐的偏离了开发主流,若干年之后的今天,当我们面对着微软的这个经典之笔,内心充满着敬意,那些曾经的记忆,可以说代表着二十年前曾经的辉煌……
        向经典致敬,或许是老一代程序员内心里面难以释怀的感受。互联网大行其道的今天,我们期待着MFC技术能够恢复其曾经的辉煌,或许这个期待会永远成为一种“梦想”,或许一切皆有可能……
        我们希望这个版块可以很好的适配Web时代,期待更好的互联网技术能够使得MFC技术框架得以重现活力,……