C++Builder 程序员博客
24 Aug
我的程序中有调用word文档的按钮,本机使用一切正常,我先用
完全编译:
1.菜单:project- >options
2.compiler页 点击release按扭
3.linker页 去掉use dynamic rtl项的钩选
4.packages页 去掉build with runtime packages 项的钩选
得到exe文件,拷贝到目标机运行,开始一切正常,当点击那个"调用word文档的按钮"时,
报错" invalid Variant operation";
而后,我用installshield X Express打包的办法,其中我用向导动态扫描该程序相关的动态链接文件,
制作了一个setup文件,在目标机上运行,结果出现了同样的现象。
是我在编译选项上遗漏了什么吗?下面给出该按钮的代码,希望大侠们给点提示,先在此谢过了!
void __fastcall TForm1::WordFileClick(TObject *Sender)
{
Label5->Caption ="开始插入照片到word文档中,请稍候……";
// 有关word的操作
V=CreateOleObject("Word.application"); //得到Word对象
V.OlePropertySet("Visible",false); //使word不可见
Vd = V.Exec(PropertyGet("Documents")); //得到文档对象
Vd1=Vd.Exec(Procedure("Add")); //添加新文档
Vactive=V.OlePropertyGet("ActiveDocument");
Vactive.OlePropertyGet("Paragraphs").OleFunction("Add");
int i=1;//插入行数控制
int j=0;//插入的文件名的控制
double setsize;//设定的图像大小
if(Edit1->Text!="")
setsize=StrToFloat(Edit1->Text);
else setsize=10.0;
//插入标题
stext="附图:\n\n\n\n\n";
AddText(i,stext,0);
//循环插入图形和图名
i=3;
for(int mm=ListBox2->Items->Count -1;mm>=0;mm–)
{
stext="图:\n\n\n\n";
AddText(i,stext,1);
stext=ListBox2->Items->Strings[j].c_str() ;
//插入图名文字
AddText(i,stext,1);
//插入图形
AddPicture(ListBox2->Items->Strings[j],i-1,setsize);
j++;i=i+2;
}
Label5->Caption ="操作完成,请自行保存文档!";
V.OlePropertySet("Visible",true); //使word可见
}
对不起,我用installshield X Express不太熟,不过我又用
installshield X Express动态扫描了一下,同样出现了" invalid Variant operation"错误提示,
但在这个提示之后,还有一条提示:Could not convert variant of type(Array 0×0548)into
type(Dispatch).说我定义的variant类型变量,在进行类型转换时出错,我在此把头文件贴出来,帮
我分析一下,哪里不妥?
//—————————————————————————
#ifndef zhufile1H
#define zhufile1H
//—————————————————————————
#include <Classes.hpp>
#include <Controls.hpp>
#include <StdCtrls.hpp>
#include <Forms.hpp>
#include <Dialogs.hpp>
#include "Word_2K_SRVR.h"
#include <OleServer.hpp>
#include <sysvari.h>
#include "comobj.hpp"
//—————————————————————————
class TForm1 : public TForm
{
__published: // IDE-managed Components
TLabel *Label1;
TLabel *Label2;
TEdit *Path;
TEdit *File;
TButton *Serch;
TListBox *ListBox1;
TListBox *ListBox2;
TButton *Add;
TButton *AddAll;
TButton *Del;
TButton *DelAll;
TButton *WordFile;
TEdit *Edit1;
TLabel *Label3;
TLabel *Label4;
TOpenDialog *OpenDialog1;
TLabel *Label5;
TScrollBox *ScrollBox1;
TScrollBox *ScrollBox2;
TButton *Button1;
void __fastcall SerchClick(TObject *Sender);
void __fastcall AddClick(TObject *Sender);
void __fastcall DelClick(TObject *Sender);
void __fastcall AddAllClick(TObject *Sender);
void __fastcall DelAllClick(TObject *Sender);
void __fastcall WordFileClick(TObject *Sender);
void __fastcall PathDblClick(TObject *Sender);
void __fastcall ListBox1Click(TObject *Sender);
void __fastcall ListBox2Click(TObject *Sender);
void __fastcall Button1Click(TObject *Sender);
void __fastcall Edit1KeyPress(TObject *Sender, char &Key);
private: // User declarations
public: // User declarations
void __fastcall SearchFile(AnsiString Path,AnsiString filename,TStrings *list);
void __fastcall SearchPath(AnsiString Path,AnsiString filename,TStrings *list);
Variant V,Vd,Vd1,Vd2,Vactive;
void AddText(int i,Variant text,int duiqi);
void AddPicture(AnsiString name,int i,double setsize);
bool __fastcall DotInStr(AnsiString ass);
__fastcall TForm1(TComponent* Owner);
};
//—————————————————————————
extern PACKAGE TForm1 *Form1;
//—————————————————————————
#endif
另外,主文件的声明部分:
//—————————————————————————
#include <vcl.h>
#pragma hdrstop
#include "zhufile1.h"
//—————————————————————————
#pragma package(smart_init)
#pragma link "Word_2K_SRVR"
#pragma resource "*.dfm"
TForm1 *Form1;
#include <SysUtils.hpp>
Variant stext;
安装word了么?
在BCB5。0 下:
V.OlePropertySet("Visible",false);
在BCB6。0下:
V.OlePropertySet(WideString("Visible"),false);
其它也这么改
我找到答案了
还是完全编译的事情,在点击发布按钮时,会自动选择代码优化
我把代码优化去掉后,进行完全编译,
再将exe文件拿到目标机上试,就一切ok了;跟word是否可见没有关系
不过还是要谢谢"BCBPLC",这里有你的功劳。