| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 2794 人关注过本帖
标题:c/c++ 利用com API 非MFC 操作word 如何 插入表格??
只看楼主 加入收藏
wzxiaodu
Rank: 1
等 级:新手上路
帖 子:32
专家分:2
注 册:2017-8-11
结帖率:50%
收藏
已结贴  问题点数:20 回复次数:2 
c/c++ 利用com API 非MFC 操作word 如何 插入表格??
程序代码:
#include <cstdio>
#include <conio.h>
#include <clocale>
#include <windows.h>
#include <ole2.h>

//-lole32 -loleaut32 -luuid
//-fexec-charset=gbk -finput-charset=gbk

// 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(nullptr, "nullptr IDispatch passed to AutoWrap()", "Error", 0x10010);
        _exit(0);
    }

    // Variables used...
    DISPPARAMS dp = { nullptr, nullptr, 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, nullptr, nullptr);

    // 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/err 0x%08lx", szName, hr);
        MessageBox(nullptr, 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)
    {
         = 1;
        dp.rgdispidNamedArgs = &dispidNamed;
    }

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

    delete [] pArgs;

    return hr;
}

int main()
{

    IDispatch*    m_pWApp;
    IDispatch*  m_pDocuments;
    IDispatch*    m_pActiveDocument;
    IDispatch*  m_pDocApp;
    IDispatch*  m_pSelection;

    HRESULT        m_hr;

    setlocale(LC_ALL,"");
    CoInitialize(nullptr);
    CLSID clsid;
    m_hr = CLSIDFromProgID(L"Word.Application", &clsid);
    if (SUCCEEDED(m_hr))
    {
        m_hr = CoCreateInstance(clsid, nullptr, CLSCTX_LOCAL_SERVER, IID_IDispatch, (void **)&m_pWApp);
    }

    {
        VARIANT x;
        x.vt = VT_I4;
        x.lVal = 1;
        m_hr = AutoWrap(DISPATCH_PROPERTYPUT, nullptr, m_pWApp, (LPOLESTR) L"Visible", 1, x);
    }

    {
        VARIANT result;
        VariantInit(&result);
        m_hr = AutoWrap(DISPATCH_PROPERTYGET, &result, m_pWApp, (LPOLESTR)L"Documents", 0);
        m_pDocuments = result.pdispVal;
    }

    {
        VARIANT result;
        VariantInit(&result);
        m_hr = AutoWrap(DISPATCH_METHOD, &result, m_pDocuments,(LPOLESTR) L"Add", 0);
        m_pActiveDocument = result.pdispVal;
    }

    {
        VARIANT result;
        VariantInit(&result);
        m_hr = AutoWrap(DISPATCH_PROPERTYGET, &result, m_pActiveDocument, (LPOLESTR)L"Application", 0);
        m_pDocApp= result.pdispVal;
    }

    {
        VARIANT result;
        VariantInit(&result);
        m_hr = AutoWrap(DISPATCH_PROPERTYGET, &result, m_pDocApp, (LPOLESTR)L"Selection", 0);
        m_pSelection=result.pdispVal;
    }

/////////////////////////////////////////////
    IDispatch*  m_pTables;
    {
        VARIANT result;
        VariantInit(&result);
        AutoWrap(DISPATCH_PROPERTYGET, &result, m_pActiveDocument, (LPOLESTR)L"Tables", 0);
        m_pTables=result.pdispVal;
    }
    printf("1111111111\n");
    _getch();

    IDispatch*  m_pRange;
    {
        VARIANT result;
        VariantInit(&result);
        AutoWrap(DISPATCH_PROPERTYGET, &result, m_pSelection, (LPOLESTR)L"Range", 0);
        m_pRange = result.pdispVal;
    }
    printf("222222222222\n");
    _getch();

    IDispatch*  m_pTable;
    {
        //这句是 VBA语句:  ActiveDocument.Tables.Add Range:=Selection.Range, NumRows:=4, NumColumns:=5, DefaultTableBehavior:=1, AutoFitBehavior:=2


        VARIANT NumRows, NumColumns, DefaultTableBehavior, AutoFitBehavior;

        NumRows.vt = VT_I4;
        NumRows.lVal = 6;

        NumColumns.vt = VT_I4;
        NumColumns.lVal = 4;

        DefaultTableBehavior.vt = VT_I4;
        DefaultTableBehavior.lVal = 1;

        AutoFitBehavior.vt = VT_I4;
        AutoFitBehavior.lVal = 2;

        VARIANT result;
        VariantInit(&result);
        m_hr = AutoWrap(DISPATCH_METHOD, &result, m_pTables, (LPOLESTR) L"Add", 5, AutoFitBehavior, DefaultTableBehavior, NumColumns, NumRows, m_pRange);
        //   m_hr = AutoWrap(DISPATCH_METHOD, &result, m_pTables, (LPOLESTR) L"Add", 3, NumColumns, NumRows, m_pSelection);
        m_pTable = result.pdispVal;
    }
    printf("3333333333\n");
    _getch();

    ////////////////////////////////////////

    m_pRange->Release();
    m_pSelection->Release();
    m_pDocApp->Release();
    m_pTable->Release();
    m_pTables->Release();
    m_pActiveDocument->Release();
    m_pDocuments->Release();
    m_pWApp->Release();

    CoUninitialize();

    _getch();
    return 0;
}
(win7 x64系统  mingw64)
程序运行到Add时,出错了?
大神,我改怎么写这句!!!!!


图片附件: 游客没有浏览图片的权限,请 登录注册

搜索更多相关主题的帖子: Add int Release result include 
2021-06-20 11:28
rjsp
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:版主
威 望:528
帖 子:9031
专家分:54061
注 册:2011-1-18
收藏
得分:20 
m_hr = AutoWrap(DISPATCH_METHOD, &result, m_pTables, (LPOLESTR) L"Add", 5, AutoFitBehavior, DefaultTableBehavior, NumColumns, NumRows, m_pRange);

AutoFitBehavior, DefaultTableBehavior, NumColumns, NumRows 这四个都是 VARIANT 类型,而 m_pRange 不是
    IDispatch*  m_pRange;
    {
        VARIANT result;
        VariantInit(&result);
        AutoWrap(DISPATCH_PROPERTYGET, &result, m_pSelection, (LPOLESTR)L"Range", 0);
        m_pRange = result.pdispVal;
    }

你可以将这里面的 result 传给 AutoWrap,而不是将 result.pdispVal 传给 AutoWrap。
2021-06-21 09:01
wzxiaodu
Rank: 1
等 级:新手上路
帖 子:32
专家分:2
注 册:2017-8-11
收藏
得分:0 
回复 2楼 rjsp
谢谢,大神,现在可以了。
2021-06-22 16:42
快速回复:c/c++ 利用com API 非MFC 操作word 如何 插入表格??
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.022321 second(s), 9 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved