关于COM组件VARIANT类型参数

6057 2016-08-26 11:15:02
下图一是photoshop接口的一个函数

图二是我调用这个函数的代码

现在的问题是Open函数的两个VARIANT&类型参数我不知道该怎么传递,两个参数都用vtmissing也不行,提示参数错误,
使用AfxVariantInit初始化过的vt1,vt2也提示参数错误,使用不初始化的vt1,vt2提示不正确的参数类型.请问这是怎么回事,该怎么传递这两参数??
...全文
326 17 打赏 收藏 转发到动态 举报
写回复
用AI写文章
17 条回复
切换为时间正序
请发表友善的回复…
发表回复
6057 2016-08-30
  • 打赏
  • 举报
回复
引用 16 楼 zhao4zhong1 的回复:
VS IDE中,在不明白的符号上点鼠标右键,选转到定义。
谢谢赵老师的耐心回答,已经搞定了,首先用app.Load()加载psd文档,再doc=app.get_ActiveDocument()获取当前活动文档。
赵4老师 2016-08-29
  • 打赏
  • 举报
回复
VS IDE中,在不明白的符号上点鼠标右键,选转到定义。
6057 2016-08-26
  • 打赏
  • 举报
回复
引用 1 楼 chengbar 的回复:
看 VARIANT的构造函数,传构造函数的类型实例化就可以了
VARIANT是一个结构体
sevancheng 2016-08-26
  • 打赏
  • 举报
回复
看 VARIANT的构造函数,传构造函数的类型实例化就可以了
6057 2016-08-26
  • 打赏
  • 举报
回复
6057 2016-08-26
  • 打赏
  • 举报
回复
引用 13 楼 zhao4zhong1 的回复:
楼主看到这句
COleVariant vTrue((short)TRUE), vFalse((short)FALSE), vOpt((long)DISP_E_PARAMNOTFOUND, VT_ERROR);
难道没眼前一亮吗?


所有组合都试过了,还是提示参数错误
赵4老师 2016-08-26
  • 打赏
  • 举报
回复
楼主看到这句 COleVariant vTrue((short)TRUE), vFalse((short)FALSE), vOpt((long)DISP_E_PARAMNOTFOUND, VT_ERROR); 难道没眼前一亮吗?
Dobzhansky 2016-08-26
  • 打赏
  • 举报
回复
第一个参数你给bstr 试试

可以用 vc 的 _bstr_t
6057 2016-08-26
  • 打赏
  • 举报
回复
引用 10 楼 Dobzhansky 的回复:
别急 , com 还是 windows 下很有用的技术。 .net 什么的快完蛋了。
我这个问题你有什么办法没有?
Dobzhansky 2016-08-26
  • 打赏
  • 举报
回复
别急 , com 还是 windows 下很有用的技术。 .net 什么的快完蛋了。
6057 2016-08-26
  • 打赏
  • 举报
回复
引用 8 楼 zhao4zhong1 的回复:
[quote=引用 7 楼 qq_26611129 的回复:] [quote=引用 6 楼 zhao4zhong1 的回复:] 参考PhotoShop提供的例子代码或网上搜其它博主提供的例子代码。
photoshop的脚本指南我看过,只有vb和java的,且Open函数就一个参数[/quote] 参考一下这个?
//1.在VC中新建一控制台程序,选支持MFC(当然,你也可以不选择支持MFC的,不过会很麻烦)
//2.按CTRL+W调出MFC ClassWizard,Add Class->From a type library,选择你的word的类型库
//  (例如我的是word2003,安装在e盘,我的路径是"e:\edittools\microsoft office\office11\msword.olb"),
//  选择完毕后,在弹出的窗口中选择要让classwizard生成的包装类,在本例中要用到
//  _Application,
//  Documents,
//  _Document,
//  Range
//  这四个类,选中他们后按OK
//3.进入你的main函数所在的cpp文件,加入头文件引用
//  #include  "msword.h"    //引用刚才classwizard生成的idispatch包装类
//4.加入代码
// console_word.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include "console_word.h"
#include "msword.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// The one and only application object

CWinApp theApp;

int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
{
    int nRetCode = 0;

    // initialize MFC and print and error on failure
    if (!AfxWinInit(::GetModuleHandle(NULL), NULL, ::GetCommandLine(), 0))
    {
        // TODO: change error code to suit your needs
        printf(_T("Fatal Error: MFC initialization failed!\n"));
        nRetCode = 1;
    }
    else
    {
        // TODO: code your application's behavior here.
        if  (CoInitialize(NULL)  !=  S_OK)
        {
            AfxMessageBox("初始化COM支持库失败!");
            return  -1;
        }

        _Application  wordApp;
        Documents     docs;
        _Document     doc;
        Range         aRange;
        COleVariant   vTrue((short)TRUE), vFalse((short)FALSE), vOpt((long)DISP_E_PARAMNOTFOUND, VT_ERROR);
        CString       txt;

        wordApp.CreateDispatch("Word.Application",NULL);
        wordApp.SetVisible(FALSE);
            docs=wordApp.GetDocuments();
                doc=docs.Open(COleVariant("c:\\new\\测试.doc"),vFalse,vTrue,vFalse,vOpt,vOpt,vOpt,vOpt,vOpt,vOpt,vOpt,vOpt,vOpt,vOpt,vOpt,vOpt);
                    aRange=doc.Range(vOpt,vOpt);
                        txt=aRange.GetText();
                        AfxMessageBox(txt);//这里GetText得到的就是word文件的纯文本了,你可以将其写到txt文件中
                        printf("[%s]\n",txt.GetBuffer(txt.GetLength()));//里面的换行不是\r\n而是\r,所以需要输出重定向到文本文件看结果。
                    aRange.ReleaseDispatch();
                doc.Close(vOpt,vOpt,vOpt);
                doc.ReleaseDispatch();
            docs.ReleaseDispatch();
        wordApp.Quit(vOpt,vOpt,vOpt);
        wordApp.ReleaseDispatch();

        CoUninitialize();
    }

    return nRetCode;
}


汗这个?
// Copyright (C) 1992-1998 Microsoft Corporation
// All rights reserved.
//
// This source code is only intended as a supplement to the
// Microsoft Visual C++ Language  Reference and related
// electronic documentation provided with Microsoft Visual C++.
// See these sources for detailed information regarding the
// Microsoft Visual C++ product.

// NOTE: This example will only work with Excel8 in Office97
// Compile with cl /GX comexcel.cpp
// 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 4146)

#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;

void 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 = "@";

      _CommandBarsPtr pCmdbars = pXL->CommandBars;
      int iCmdbars = pCmdbars->GetCount();

      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();
    }
}


[/quote] 操作Office的word,excel,powerpoint我已经成功了,而且导入Office接口是通过在安装目录导入相应的OLB或EXE,photoshop我是通过注册表导入的,网上关于这方面的太少了,我已经黔驴技穷了
赵4老师 2016-08-26
  • 打赏
  • 举报
回复
引用 7 楼 qq_26611129 的回复:
[quote=引用 6 楼 zhao4zhong1 的回复:] 参考PhotoShop提供的例子代码或网上搜其它博主提供的例子代码。
photoshop的脚本指南我看过,只有vb和java的,且Open函数就一个参数[/quote] 参考一下这个?
//1.在VC中新建一控制台程序,选支持MFC(当然,你也可以不选择支持MFC的,不过会很麻烦)
//2.按CTRL+W调出MFC ClassWizard,Add Class->From a type library,选择你的word的类型库
//  (例如我的是word2003,安装在e盘,我的路径是"e:\edittools\microsoft office\office11\msword.olb"),
//  选择完毕后,在弹出的窗口中选择要让classwizard生成的包装类,在本例中要用到
//  _Application,
//  Documents,
//  _Document,
//  Range
//  这四个类,选中他们后按OK
//3.进入你的main函数所在的cpp文件,加入头文件引用
//  #include  "msword.h"    //引用刚才classwizard生成的idispatch包装类
//4.加入代码
// console_word.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include "console_word.h"
#include "msword.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// The one and only application object

CWinApp theApp;

int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
{
    int nRetCode = 0;

    // initialize MFC and print and error on failure
    if (!AfxWinInit(::GetModuleHandle(NULL), NULL, ::GetCommandLine(), 0))
    {
        // TODO: change error code to suit your needs
        printf(_T("Fatal Error: MFC initialization failed!\n"));
        nRetCode = 1;
    }
    else
    {
        // TODO: code your application's behavior here.
        if  (CoInitialize(NULL)  !=  S_OK)
        {
            AfxMessageBox("初始化COM支持库失败!");
            return  -1;
        }

        _Application  wordApp;
        Documents     docs;
        _Document     doc;
        Range         aRange;
        COleVariant   vTrue((short)TRUE), vFalse((short)FALSE), vOpt((long)DISP_E_PARAMNOTFOUND, VT_ERROR);
        CString       txt;

        wordApp.CreateDispatch("Word.Application",NULL);
        wordApp.SetVisible(FALSE);
            docs=wordApp.GetDocuments();
                doc=docs.Open(COleVariant("c:\\new\\测试.doc"),vFalse,vTrue,vFalse,vOpt,vOpt,vOpt,vOpt,vOpt,vOpt,vOpt,vOpt,vOpt,vOpt,vOpt,vOpt);
                    aRange=doc.Range(vOpt,vOpt);
                        txt=aRange.GetText();
                        AfxMessageBox(txt);//这里GetText得到的就是word文件的纯文本了,你可以将其写到txt文件中
                        printf("[%s]\n",txt.GetBuffer(txt.GetLength()));//里面的换行不是\r\n而是\r,所以需要输出重定向到文本文件看结果。
                    aRange.ReleaseDispatch();
                doc.Close(vOpt,vOpt,vOpt);
                doc.ReleaseDispatch();
            docs.ReleaseDispatch();
        wordApp.Quit(vOpt,vOpt,vOpt);
        wordApp.ReleaseDispatch();

        CoUninitialize();
    }

    return nRetCode;
}


汗这个?
// Copyright (C) 1992-1998 Microsoft Corporation
// All rights reserved.
//
// This source code is only intended as a supplement to the
// Microsoft Visual C++ Language  Reference and related
// electronic documentation provided with Microsoft Visual C++.
// See these sources for detailed information regarding the
// Microsoft Visual C++ product.

// NOTE: This example will only work with Excel8 in Office97
// Compile with cl /GX comexcel.cpp
// 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 4146)

#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;

void 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 = "@";

      _CommandBarsPtr pCmdbars = pXL->CommandBars;
      int iCmdbars = pCmdbars->GetCount();

      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();
    }
}


6057 2016-08-26
  • 打赏
  • 举报
回复
引用 6 楼 zhao4zhong1 的回复:
参考PhotoShop提供的例子代码或网上搜其它博主提供的例子代码。
photoshop的脚本指南我看过,只有vb和java的,且Open函数就一个参数
赵4老师 2016-08-26
  • 打赏
  • 举报
回复
参考PhotoShop提供的例子代码或网上搜其它博主提供的例子代码。
6057 2016-08-26
  • 打赏
  • 举报
回复
大神求帮忙,呼叫@赵4老师
6057 2016-08-26
  • 打赏
  • 举报
回复
引用 3 楼 oyljerry 的回复:
http://bbs.csdn.net/topics/390489050
这个我之前看过,他那里也是没有解决
oyljerry 2016-08-26
  • 打赏
  • 举报
回复

3,245

社区成员

发帖
与我相关
我的任务
社区描述
ATL,Active Template Library活动(动态)模板库,是一种微软程序库,支持利用C++语言编写ASP代码以及其它ActiveX程序。
社区管理员
  • ATL/ActiveX/COM社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧