如何通过语句来实现WORD中字符串的选中并定义书签???(尤其是在表格中的文字如何处理),问题解决了,分分啦!!!!!!!!!!! (200分)

  • 主题发起人 主题发起人 heqian
  • 开始时间 开始时间
H

heqian

Unregistered / Unconfirmed
GUEST, unregistred user!
var //查找替换WORD中的指定字符串
FindText, MatchCase, MatchWholeWord, MatchWildcards, MatchSoundsLike,MatchKashida,MatchDiacritics,
MatchAllWordForms, Forward, Wrap, Format, ReplaceWith, Replace,MatchAlefHamza,MatchControl: OleVariant;
Range_start,Range_end,i,Range1:OleVariant;
Document_range:Range;
begin
FindText := Find_string;
MatchCase := False;
MatchWholeWord := True;
MatchWildcards := False;
MatchSoundsLike := False;
MatchAllWordForms := False;
Forward := True;
Wrap := wdFindContinue;
Format := False;
ReplaceWith := Replaced_string;
Replace := wdReplaceAll;
MatchKashida:= False;
MatchDiacritics:= False;
MatchAlefHamza:= False;
MatchControl:= False;
Document_range:=Word_App.ActiveDocument.Range(Range_start,Range_end);
Document_range.Find.Execute( FindText, MatchCase, MatchWholeWord,
MatchWildcards, MatchSoundsLike, MatchAllWordForms, Forward,
Wrap, Format, ReplaceWith, Replace,MatchKashida,MatchDiacritics,
MatchAlefHamza,MatchControl);
end;
我采用上面的语句实现对WORD中指定字符串的查找替换,我现在想做的是如何将
替换后的部分选种,定义书签bookmark;我想通过定义BOOKMARK来保证替换后的
部分不被修改,这样做可以吗??应该如何选中替换后的字符串,并定义书签??
 
怎么,没有人回答我呀???
这个问题搞不定,这周没有办法完成工作任务了!!!!
 
var
Range : OleVariant;

Range := wordapplication.Selection.Range ;
wordapplication.ActiveDocument.Bookmarks.Add('SearchEd',Range);
 
谢谢小丸子xiaowanzi!
我已经基本上搞定了!!代码如下:
Procedure TForm1.BookMark_Create(Find_string:string;i:integer;Range1:OleVariant);
var //查找WORD中的指定字符串,并定义书签,将其字体颜色变为红色。
FindText, MatchCase, MatchWholeWord, MatchWildcards, MatchSoundsLike,MatchKashida,MatchDiacritics,
MatchAllWordForms, Forward, Wrap, Format, ReplaceWith, Replace,MatchAlefHamza,MatchControl: OleVariant;
Range_start,Range_end,myRange:OleVariant;
bookmark_index:integer;
adoquery_temp:TAdoquery;
Range_now:Range;
label Next_Search;
begin
Adoquery_temp:=Tadoquery.Create(self);
adoquery_temp.Connection:=ADOConnection1;
FindText := Find_string;
MatchCase := False;
MatchWholeWord := True;
MatchWildcards := False;
MatchSoundsLike := False;
MatchAllWordForms := False;
Forward := True;
//Wrap := wdFindContinue;
Format := False;
//ReplaceWith := Replaced_string;
//Replace := wdReplaceAll;
MatchKashida:= False;
MatchDiacritics:= False;
MatchAlefHamza:= False;
MatchControl:= False;
myRange:=Range1;
MyRange.Find.Execute( FindText, MatchCase, MatchWholeWord,
MatchWildcards, MatchSoundsLike, MatchAllWordForms, Forward,
Wrap, Format, ReplaceWith, Replace,MatchKashida,MatchDiacritics,
MatchAlefHamza,MatchControl);
bookmark_index:=i;
if myRange.Find.Found=true then
begin
myRange.select;
Range_now:=WordApp.Selection.Range;
//通过选种部分颜色代码判定是否为插入部分;
if (WordApp.Selection.Font.Color = 4278190080) or (Range_now.Bookmarks.Count>0) then goto next_search;
//4278190080为普通文本字体(黑色)选中后的颜色代码,
//插入部分我用其他颜色表示
myrange.Font.Color:=wdColorRed;//将字体颜色变为红色
wordApp.ActiveDocument.Bookmarks.Add('bookmark'+inttostr(bookmark_index),myRange);//添加书签
with adoquery_temp do //将标签名称与内容保存到表中
begin
sql.Clear;
sql.Add('insert into NeiRong_bookmark');
sql.Add('(bookmark,text)');
sql.Add('values(:v_mark,:v_text)');
parameters.ParamByName('v_mark').Value:='bookmark'+inttostr(i);
parameters.ParamByName('v_text').Value:=myRange.Text;
execsql;
end;
bookmarkindex:=Bookmark_index+1;
next_search:
Range_start:=Range_now.End_;
Range_end:=wordApp.ActiveDocument.Content.End_ ;
myrange:=WordApp.ActiveDocument.Range(range_start,range_end);
//重新定义MYrange,查找下一个
BookMark_Create(Find_string,Bookmarkindex,myrange);
end;
adoquery_temp.Destroy;
end;

我将代码写出来,希望对大家也有所帮助!!

这里存在一个问题没有解决:如果查找到的内容是在表格中,则程序的Range_start的值
将不在改变,只能查找到刚才查找到的哪个字符串,程序陷入死循环,请高手帮忙解决
这个问题!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
 
代码有多处错误!
 
什么错误呀???
我调试通过了呀?????????????
 
1,bookmark_index--bookmarkindex:=Bookmark_index+1;
BookMark_Create(Find_string,Bookmarkindex,myrange);
2,myrange没定义!

 
哦,谢谢!!
bookmarkIndex是全局变量!!
 
http://www.delphibbs.com/delphibbs/dispq.asp?lid=1898701
 
你上面的程序是用SERVER面板上的WORD控件组。
我建议你可以用创建WORD.APPLICATION动态创建COM对象
这样就可以编写出对WORD控制比较好的程序,因为它可以先在
WORD中进行宏录制,然后在WORD中进行操作,录制完后在VBA编辑器中查看
刚才进行WORD操作的所有VB代码。你把VB代码转化为DELPHI就OK了。
如果你会VBA的话,那就更好了,可以直接在WORD中编程。
 
来自:heqian, 时间:2003-5-31 14:40:00, ID:1906068 | 编辑

上面那段程序的死循环问题基本解决了!!

我采用的方法比较笨:上面的语句在用CreateOleObject('word.application')连接WORD时执行起来没有问题,即使遇到表格中的字符串;由于我需要调用
WordApplication.OnDocumentBeforeClose事件处理一些事情,所有采用如下方法:
unit Unit1;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs,comobj, StdCtrls,Word2000, DB, ADODB, OleServer;

type
TForm1 = class(TForm)
Button1: TButton;
ADOQuery_temp: TADOQuery;
WordApplication1: TWordApplication;
procedure Button1Click(Sender: TObject);
Procedure Create_BookMark(Find_string:string;i:integer;Word_Selection:Variant);
procedure FormClose(Sender: TObject; var Action: TCloseAction);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;
MyWord:OleVariant;
implementation

{$R *.dfm}

procedure TForm1.Create_BookMark(Find_string:string;i:integer;Word_Selection:Variant);
var //查找替换WORD中的指定字符串
FindText, MatchCase, MatchWholeWord, MatchWildcards, MatchSoundsLike,MatchKashida,MatchDiacritics,
MatchAllWordForms, Forward, Wrap, Format, ReplaceWith, Replace,MatchAlefHamza,MatchControl: OleVariant;
Range_start,Range_end:OleVariant;
bookmark_index,bookmarkIndex:integer;
Selection_now:Variant;
Direction,Range_now,unit_,Count,Extend:OleVariant;
label Next_Search;
begin
FindText := Find_string;
MatchCase := False;
MatchWholeWord := True;
MatchWildcards := False;
MatchSoundsLike := False;
MatchAllWordForms := False;
Forward := True;
Wrap := WdFindStop;
Format := False;
ReplaceWith := '';
Replace := wdReplaceNone;
MatchKashida:= False;
MatchDiacritics:= False;
MatchAlefHamza:= False;
MatchControl:= False;
Selection_now:=Word_Selection;
Selection_now.Find.ClearFormatting;
Selection_now.Find.Execute( FindText, MatchCase, MatchWholeWord,
MatchWildcards, MatchSoundsLike, MatchAllWordForms, Forward,
Wrap, Format, ReplaceWith, Replace,MatchKashida,MatchDiacritics,
MatchAlefHamza,MatchControl);
bookmark_index:=i;
if Selection_now.Find.Found=true then
begin
Range_now:=Selection_now.Range;
//通过选种部分颜色代码判定是否为插入部分;
if (Selection_now.Font.Color = 4278190080) or (Selection_now.Bookmarks.Count>0) then goto next_search;
MyWord.Selection.Font.Color:=wdColorRed;
Myword.ActiveDocument.Bookmarks.Add('bookmark'+inttostr(bookmark_index),Range_now);
with ADOQuery_temp do
begin
sql.Clear;
sql.Add('insert into xlgq_SCGL_LC_NeiRong_bookmark');
sql.Add('(bookmark,text)');
sql.Add('values(:v_mark,:v_text)');
parameters.ParamByName('v_mark').Value:='bookmark'+inttostr(i);
parameters.ParamByName('v_text').Value:=Selection_now.Text;
execsql;
end;
bookmarkindex:=Bookmark_index+1;
next_search:
Create_BookMark(Find_string,Bookmarkindex,Selection_now);
end;
end;
procedure TForm1.Button1Click(Sender: TObject);
var FileName,ConfirmConversions,ReadOnly,AddToRecentfiles,PasswordDocument,
PasswordTemplate,Revert,WritePasswordDocument,WritePasswordTemplate,
Format,Encoding,Visible:OleVariant;
begin
FileName:=GetCurrentDir+'/Doc_temp.doc';
MyWord:=CreateOleObject('word.application');
MyWord.visible:=true;
MyWord.Documents.add(Template:='c:/MoBan_Temp.dot',NewTemplate:=false,visible:=true);
MyWord.ActiveDocument.select;
Create_Bookmark('绝缘子',1,MyWord.selection);
showmessage('标记完毕');
MyWord.ActiveDocument.saveas(FileName:=Filename,fileFormat:=wdFormatDocument,AddToRecentFiles:=false);
MyWord.ActiveDocument.close;
MyWord.quit;
//MyWord.free;
//WordApplication1.OnDocumentBeforeClose:=WordAppDocumentBeforeClose;
ConfirmConversions:=false;
ReadOnly:=false;
AddToRecentfiles:=false;
PasswordDocument:='';
PasswordTemplate:='';
Revert:=false;
WritePasswordDocument:='';
WritePasswordTemplate:='';
Format:=wdFormatDocument;
//Encoding
Visible:=true;
WordApplication1.Documents.Open(FileName,ConfirmConversions,ReadOnly,AddToRecentfiles,
PasswordDocument,PasswordTemplate,Revert,WritePasswordDocument,
WritePasswordTemplate,Format,Encoding,Visible);
WordApplication1.Visible:=true;
end;
procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
WordApplication1.Disconnect;
WordApplication1.Quit;

end;

end.

用CreateOleObject处理完成之后将文件保存,然后再用WordApplication打开该文件让用户处理,呵呵,这个问题基本解决了!!明天可以放心的去帮朋友攒电脑了!!!!
 
今天高兴,问题解决了!

前来分分!!

怎么就这么几个人分分呀????

再等等吧!!!!!!!!
 
等了你很久你也不结束帖子,只好将这个给你发过来了。[:D]

嗨!CreateOleObject和WordApplication是可以同时使用的。不用保存以后再打开的。
WordAppp := CreateOleObject('Word.Application') as _Application;
具体在http://www.delphibbs.com/delphibbs/dispq.asp?lid=737517 中darnis的发言。你可以看看
 
哈哈,谢谢前辈
 
后退
顶部