VBA 翻译成Delphi代码 ( 积分: 200 )

  • 主题发起人 主题发起人 lanyun2
  • 开始时间 开始时间
L

lanyun2

Unregistered / Unconfirmed
GUEST, unregistred user!
2007-1-13 12:34 守柔
此事很难,之前我也谈到过,以下代码供参考:
Sub Example()
Dim oTable As Table, oCell As Cell
Dim H As Variant, W As Single, R As Integer, C As Byte
Dim A As Single, B As Single
For Each oTable In ActiveDocument.Tables
For Each oCell In oTable.Range.Cells
With oCell
R = .RowIndex
C = .ColumnIndex
If .Height = wdUndefined Then
A = .Range.Information(wdVerticalPositionRelativeToPage)
.Range.Select
Selection.MoveDown
B = Selection.Information(wdVerticalPositionRelativeToPage)
H = Int(B - A)
Else
H = Int(.Height)
End If
W = Int(.Width)
.Range.InsertBefore "(" & R & "," & C & ")" & W & ":" & H
End With
Next
Next
End Sub

以上代码可以在Word中运行
谁能帮我翻译成Delphi可运行代码?
关键是 Information方法老是不能调用!
 
该问题已经好久没有 解决,如能 解决 500分相赠
 
给你一段代码吧,翻译了你想要的内容。
执行前先托两个Servers下的控件到Form上:TWordApplication,TWordDocument(比较方便)
或是你自己使用CreateOleObject创建他们也行。

//////////////////////////////////////////////////////////////
procedure TForm1.btnMacroClick(Sender: TObject);
const
csNetStyle = '网格型';
var
vData: OleVariant;
oDoc: _Document;
oRg: Range;
I, nRow, nCol : Integer;
sData: string;
A, B, H, W: Single;
begin
with WordApplication1 do
begin
Visible := True;
Activate;
AutoConnect;
end;

oDoc := WordApplication1.Documents.Add(EmptyParam, EmptyParam, EmptyParam, EmptyParam);
WordDocument1.ConnectTo(oDoc);

vData := 0;
with WordDocument1 do
begin
oRg := Range(vData, vData);
with Tables do
begin
// 在Word中添加一个 10×6的表格.
Add(oRg, 10, 6, EmptyParam, EmptyParam);
for I := 1 to Count do
begin
vData := Item(I).Get_Style;
sData := vData;
if csNetStyle <> sData then
begin
vData := csNetStyle;
// 设置表格的边框效果为 '网状型'
Item(I).Set_Style(vData);
end;

// 从上面的VBA宏翻译来的内容
for nRow := 1 to Item(I).Rows.Count do
for nCol := 1 to Item(I).Columns.Count do
with Item(I).Rows.Item(nRow).Cells.Item(nCol) do
begin
if Height = wdUndefined then
begin
A := Range.Information[wdVerticalPositionRelativeToPage];
Range.Select;
WordApplication1.Selection.MoveDown(EmptyParam, EmptyParam, EmptyParam);
B := Range.Information[wdVerticalPositionRelativeToPage];
H := B - A;
end
else
H := Height;
W := Width;
Range.InsertBefore('(' + IntToStr(nRow) + ',' +
IntToStr(nCol) + ')' + FloatToStr(W) + ':' + FloatToStr(H));
end;
end;
end;
end;
end;
 
后退
顶部