这段VBA代码如何转换成DELPHI语句???好人们帮帮忙呀!!头儿让最晚明天解决掉这个问题!!!(100分)

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

heqian

Unregistered / Unconfirmed
GUEST, unregistred user!
i = Selection.Range.Start
j = Selection.Range.End

Selection.Find.ClearFormatting
With Selection.Find
.Text = "绝缘子"
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchByte = True
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute '查找到后SELECTION自动变为查找到的字符串!
i = Selection.Range.Start '
j = Selection.Range.End '

With ActiveDocument.Bookmarks
.Add Range:=Selection.Range, Name:="a1"
.DefaultSorting = wdSortByName
.ShowHidden = True
End With
i = Selection.Range.Start
j = Selection.Range.End

Selection.Find.ClearFormatting
With Selection.Find
.Text = "绝缘子"
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchByte = True
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute
i = Selection.Range.Start
j = Selection.Range.End

With ActiveDocument.Bookmarks
.Add Range:=Selection.Range, Name:="a2"
.DefaultSorting = wdSortByName
.ShowHidden = True
End With

我改写的语句:
Procedure TFrame_xlgl_scgl_lc_neirong_new.BookMark_Create(Find_string:string;i:integer;Word_Selection:Selection);
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:integer;
Selection_now:Selection;
Direction,Range_now:OleVariant;
label Next_Search;
begin
FindText := Find_string;
MatchCase := False;
MatchWholeWord := True;
MatchWildcards := False;
MatchSoundsLike := False;
MatchAllWordForms := False;
Forward := True;
Wrap := wdFindContinue;
Format := False;
ReplaceWith := '';
Replace := wdReplaceNone;
MatchKashida:= False;
MatchDiacritics:= False;
MatchAlefHamza:= False;
MatchControl:= False;
//myRange:=Range1;
//myRange.select;
Selection_now:=Word_Selection;
selection_now.Range.Start;
Selection_now.Range.End_;
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
//myRange:=Selection_now;
Range_now:=Selection_now.Range;
//通过选种部分颜色代码判定是否为插入部分;
if (Selection_now.Font.Color = 4278190080) or (Selection_now.Bookmarks.Count>0) then goto next_search;
WordApp.Selection.Font.Shadow;
WordApp.Selection.Font.Color:=wdColorRed;
wordApp.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:
BookMark_Create(Find_string,Bookmarkindex,Selection_now);
//再次调用这个过程时,Selsection_now的值就不在改变,陷入死循环!!
//如何解决??
//如果将Selection_now.Range指定为:刚刚选种部分的尾部——文档的尾部,
//遇到查询到表格中的内容时就陷入死循环,怎么解决呀????
end;
end;
 
兄弟,我也不會VB,幫你UP吧,相信一定會有答案的。
 
http://www.delphibbs.com/delphibbs/dispq.asp?lid=737517
里面我说明如何转换VBA到Delphi的那段就是使用查找替换作为例子的

在那篇文章中查找下面的语句就可以了。
来自:yzhshi, 时间:2002-2-2 14:24:00, ID:902680
 
var
oStart,oEnd : olevariant;
oFindText,oForward,oWrap : OleVariant;
oFormat,oMatchCase,oMatchWholeWord : OleVariant;
oMatchByte,oMatchWildcards,oMatchSoundsLike : OleVariant;
oMatchAllWordForms : OleVariant;
Range : OleVariant;
I : Integer;
begin
oStart := WordApp.Selection.Range.Start ;
oEnd := WordApp.Selection.Range.End_ ;
WordApp.Selection.Find.ClearFormatting ;
WordApp.Selection.Find.Replacement.Text := '';
oFindText := '绝缘子';
oForward := True;
oWrap := wdFindContinue;
oMatchCase := False;
oMatchWholeWord := False;
oMatchByte := True;
oMatchWildcards := False;
oMatchSoundsLike := False;
oMatchAllWordForms := False;
for I := 1 to 2 do
begin
WordApp.Selection.Find.Execute(oFindText,oMatchCase,oMatchWholeWord,oMatchWildcards,
oMatchSoundsLike,oMatchAllWordForms,oForward,oWrap,oFormat,EmptyParam,EmptyParam);
oStart := WordApp.Selection.Range.Start;
oEnd := WordApp.Selection.Range.End_ ;
Range := WordApp.Selection.Range ;
WordApp.ActiveDocument.Bookmarks.Add('a'+IntToStr(I),Range);
WordApp.ActiveDocument.Bookmarks.DefaultSorting := wdSortByName;
WordApp.ActiveDocument.Bookmarks.ShowHidden := True;
end;
 
呵呵, yzhshi已经帮你搞定了。
 
yzhshi前辈:
我现在的问题是:[red]在VBA代码中selection的范围选中第一个查找道德字符串后,
再调用selection.find语句,VBA后自动查找后面的字符串,而在DELPHI中,selection
的范围选种为第一个查找到的字符串后,再调用selection.find查找字符串,查找的
还是刚刚查找到的字符串,不能查找下一个,我必须一个一个查找,因为我要在每找到
一个的时候,根据字体颜色判断他是不是我需要找的!![/red]
或者[blue]如果在查找、替换所有指定字符串过程中,每替换一个的时候,都将替换后的内容
保存为一个书签的功能如果能实现的话,就不用我逐个查找判断了!![/blue]

[purple]如果那位老大解决这个问题,我将所有分数——288给您!!!!!!!!!![/purple]
 
>>在VBA代码中selection的范围选中第一个查找道德字符串后,
>>再调用selection.find语句,VBA后自动查找后面的字符串,而在DELPHI中,selection
>>的范围选种为第一个查找到的字符串后,再调用selection.find查找字符串,查找的
>>还是刚刚查找到的字符串,不能查找下一个
1、哈哈,知道原因是什么吗?你查找成功以后在将发送一个“Right Arrow”的命令就可以了,因为你当前选中了东西,所以就不好办了。
Selection.MoveRight Unit:=wdCharacter, Count:=1

本来我认为查询的时候直接定义查询颜色就可以了,但是刚才试验了一下,似乎不行。
 
[blue]如果在查找、替换所有指定字符串过程中,每替换一个的时候,都将替换后的内容
保存为一个书签的功能如果能实现的话,就不用我逐个查找判断了!![/blue]

2、这个好办,宏代码如下,注意:替换的为多个,你其实是需要在程序中使用循环来进行的

Sub Macro3()
Selection.Find.ClearFormatting
With Selection.Find
.Text = "cvs"
End With
Selection.Find.Execute

With ActiveDocument.Bookmarks
.Add Range:=Selection.Range, Name:="c1"
.DefaultSorting = wdSortByName
.ShowHidden = False
End With

Selection.Find.ClearFormatting
With Selection.Find
.Text = "cvs"
End With
Selection.Find.Execute

With ActiveDocument.Bookmarks
.Add Range:=Selection.Range, Name:="c2"
.DefaultSorting = wdSortByName
.ShowHidden = False
End With
End Sub
 
多人接受答案了。
 
后退
顶部