在程序中调用word时出错!

lingyunfeipu 2003-07-15 10:34:33
在执行的过程中出现错误:被呼叫方拒绝接收呼叫
我的机器装了word 2000
操作系统 win2000
bcb 6.0

怎么回事?



我的代码:


#include <vcl.h>
#pragma hdrstop
#include "Word_2k.h"
#include "utilcls.h"
#include "Unit1.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
#include <comobj.hpp>


Variant MSWord; //不定变量声明
AnsiString str;
int i;

MSWord=CreateOleObject("Word.Basic"); //对象链接和嵌入 。首先调用Create()方法,与Word应用服务器建立一个连接
//MSWord=CreateOleObject("Excel.Application");
MSWord.Exec(Procedure("AppShow")); //显示Word应用程序
MSWord.Exec(Procedure("FileNew")<<"Normal"); //应用程序新建一文档

str="";
for (i=0;i<100;i++)
{
str=str+AnsiString(i)+" "+"/"; //字段之间加"/"隔开
}

MSWord.Exec(Procedure("Insert")<<str+'\n'); //写入一行记录

MSWord.Exec(Procedure("FileSaveAs")<<Edit1->Text); //按在Edit1中输入的路径和文件名文件另寸为
MSWord.Exec(Procedure("AppClose")); //关闭Word应用程序
...全文
95 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
sunmedia 2003-07-17
  • 打赏
  • 举报
回复
帮你顶一下!学习!!!
  • 打赏
  • 举报
回复
我第一次见这样调用Word,很感兴趣。我曾用wordApplication+wordDocument做过类似的应用,把代码粘过来和你交流。
WordApplication->GetDefaultInterface()->Visible = True;
WordApplication->set_Caption(StringToOleStr("网通公司试卷"));
WordApplication->Documents->Add(Template, NewTemplate);
//Assign WordDocument component
WordDocument->ConnectTo(WordApplication->Documents->Item(ItemIndex));
//Turn Spell checking off because it takes a long time if enabled and slows down Winword
WordApplication->Options->CheckSpellingAsYouType = False;
WordApplication->Options->CheckGrammarAsYouType = False;
//Print Title
WordFont->set_Name(StringToOleStr("宋体"));
WordDocument->Range(EmptyParam, EmptyParam)->
InsertAfter(StringToOleStr(departmentName));
WordFont->ConnectTo(WordDocument->Sentences->get_Last()->get_Font());
WordFont->set_Size(18.0);
WordDocument->Range(EmptyParam, EmptyParam)->
InsertAfter(StringToOleStr(positionName+"岗位试卷"));
// WordDocument->Range(EmptyParam, EmptyParam)->
// InsertAfter(StringToOleStr('\n'+'\n'));
WordDocument->Range(EmptyParam, EmptyParam)->InsertAfter(StringToOleStr(
"--------------------------------------------------------------------"));
//Insert data
previousTypeID=currentTypeID=0;
for(TableTestpaper->First(),i=1;!TableTestpaper->Eof;TableTestpaper->Next())
{
if(testpaperID==TableTestpaper->FieldByName("testpaperID")->AsInteger)
{
currentTypeID=TableTestpaper->FieldByName("problemType")->AsInteger;
if(!(currentTypeID==previousTypeID))
{
for(TableTestpaperTemplete->First();!(TableTestpaperTemplete->
FieldByName("problemTypeID")->AsInteger==currentTypeID);
TableTestpaperTemplete->Next());
WordDocument->Range(EmptyParam, EmptyParam)->
InsertAfter(StringToOleStr("\n"));
WordDocument->Range(EmptyParam, EmptyParam)->
InsertAfter(StringToOleStr(TableTestpaperTemplete->
FieldByName("problemTypeName")->AsString+'\n'));
WordFont->ConnectTo(WordDocument->Sentences->get_Last()->get_Font());
WordFont->set_Size(12.0);
}
WordDocument->Range(EmptyParam, EmptyParam)->
InsertAfter(StringToOleStr(IntToStr(i++) + " "+'.'));
WordFont->ConnectTo(WordDocument->Sentences->get_Last()->get_Font());
WordFont->set_Size(10.0);
problemLine=TableTestpaper->Fields->Fields[4]->AsString
+" ("
+IntToStr(TableTestpaper->Fields->Fields[1]->AsInteger)
+"分)"+ '\n';
WordDocument->Range(EmptyParam, EmptyParam)->
InsertAfter(StringToOleStr(problemLine));

previousTypeID=currentTypeID;
}
}
ButtonCloseWord->Enabled = True;
ButtonPreview->Enabled = True;
saveChanges=wdSaveChanges;
originalFormat=Unassigned;
routeDocument=Unassigned;
WordDocument->Close(saveChanges, originalFormat, routeDocument);
//WordApplication->Quit(saveChanges, originalFormat, routeDocument);
WordApplication->Disconnect();
}
nanhui 2003-07-15
  • 打赏
  • 举报
回复
另一种方法:
Variant word_app; //application
Variant word_docs; //文档对象
Variant word_range; //range对象
Variant word_tables; //tables对象
Variant word_table; //table对象
Variant active_doc; //当前文档
Variant the_cell; //单元格对象
Variant cell_range; //单元格的范围
int cell_width ; //表格宽度
int cell_long ; //表格长度
//**************开始操作××××××××
word_app=Variant::CreateObject("Word.application");
//创建word。APPLICATION对象
word_docs=word_app.OlePropertyGet("documents");
//获得文档对象
word_docs.OleProcedure("add");
//添加新文档
word_app.OlePropertySet("Visible",true);
//可见
active_doc=word_app.OlePropertyGet("ActiveDocument");
//当前文档
word_range=active_doc.OleFunction("Range");
//range对象
word_tables=word_range.OlePropertyGet("Tables");
//得到table对象
//word_table=word_tables.OleFunction("Add",word_range,100,10);
cell_width=5;
cell_long=20;
word_table=word_tables.OleFunction("Add",word_range,cell_long+1,cell_width);
//*****画表格
Procedure InsertAfter("InsertAfter");
for(int i=0;i<cell_width;i++)
//插入标题
{
the_cell=word_table.OleFunction("cell",1,i+1);
cell_range=the_cell.OlePropertyGet("range");
InsertAfter.ClearArgs();
cell_range.Exec(InsertAfter<<title[i]);
}
for(int i=0;i<cell_width;i++)
//插入实际数据,先列后行
{
for(int j=0;j<cell_long;j++)
{
the_cell=word_table.OleFunction("cell",j+2,i+1);
cell_range=the_cell.OlePropertyGet("range");
InsertAfter.ClearArgs();
cell_range.Exec(InsertAfter<<a[i][j]);
CGauge1->Progress=(i*cell_long+j)*100/(cell_long*cell_width)+1;
//显示进度
}
}
CGauge1->Progress=0;
nanhui 2003-07-15
  • 打赏
  • 举报
回复
// 为自动对象接口声明一局部
Variant MSWord;
//Declare AutoCmd objects for methods on Automation interface
//PropertySet VisTrue("Visible");
Procedure QuitFalse("Quit");
PropertyGet GetDocs("Documents");
PropertyGet GetSel("Selection");
Function DocAdd("Add");
Procedure AddText("TypeText");
//Procedure AddDate("InsertDateTime");
Procedure AddPara("TypeParagraph");
MSWord = Variant::CreateObject("Word.Application");
//MSWord=CreateOleObject("Word.Application");
MSWord.OlePropertySet("Visible",true);
//MSWord.Exec(VisTrue);
//创建一个Word并设为可见

//初始化AutoCmd对象
//VisTrue << true;
QuitFalse << false;

Variant DocCollection = MSWord.Exec(GetDocs);
DocCollection.Exec(DocAdd);
Variant WordDoc=MSWord.OlePropertyGet("ActiveDocument");
//新建一个WORD
/* /*
Variant DocCollection = MSWord.Exec(GetDocs);
Variant Ws=DocCollection.Exec(DocAdd);
Button3->Enabled=true; */
// Function fileSaveAs("FileSaveAs");
Variant Sel = MSWord.Exec(GetSel);
AddText.ClearArgs();
for(int i=1;i<10;i++)
{
AnsiString name;
name=a[0][i]+" "+a[1][i]+" "+a[2][i]+" "+a[3][i]+" "+a[4][i];
AddText.ClearArgs();
Sel.Exec(AddText << WideString(name));
Sel.Exec(AddPara);
}
Sel.Exec(AddPara);
boat2002w 2003-07-15
  • 打赏
  • 举报
回复
帮你一下

13,825

社区成员

发帖
与我相关
我的任务
社区描述
C++ Builder相关内容讨论区
社区管理员
  • 基础类社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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