起步软件技术论坛-X3

 找回密码
 立即注册
搜索
查看: 746|回复: 2

VBA代码到平台

[复制链接]
发表于 2011-9-7 15:08:52 | 显示全部楼层 |阅读模式
Dim rRow As Long
Dim lRow As Long
Dim i As Long
rRow = Sheet1.UsedRange.Row
lRow = rRow + Sheet1.UsedRange.Rows.Count - 1
For i = lRow To rRow Step -1
  If Application.WorksheetFunction.CountA(Rows(i)) = 0 Then
   Rows(i).Delete
  End If
Next

上面的VBA代码转换到平台中

var
    objApplication,objWorkBooks:Object;
    dispatchApplication,dispatchWorkBooks,dispatchSourceWB: System.DispatchHelper;
  objSheet, objUsedRange, objRows, objWorkFun, objRow, objWorkFun: Object;
  dispatchSheet, dispatchUsedRange, dispatchRows, dispatchWorkFun, dispatchRow, dispatchWorkFun: System.DispatchHelper;
  iRowsCount, rRow, lRow, I: Integer;
begin
  objApplication := ComObj.CreateOleObject('Excel.Application');
  dispatchApplication:= System.DispatchHelper.Create(objApplication);
  objWorkBooks := dispatchApplication.PropertyGet('WorkBooks',[]);
  dispatchWorkBooks := System.DispatchHelper.Create(objWorkBooks);
  dispatchSourceWB := System.DispatchHelper.Create(dispatchWorkBooks.InvokeMethod('Open',[fileName]));


   objsheet := dispatchSourceWB.PropertyGet('Sheets',[sheetName]);
  dispatchSheet := System.DispatchHelper.Create(objSheet);
  //******  删除空行
  objUsedRange := dispatchSheet.PropertyGet('UsedRange',[]);
  dispatchUsedRange := System.DispatchHelper.Create(objUsedRange);
  rRow := ObjectHelper.ToInt(dispatchUsedRange.PropertyGet('Row',[]));

  objRows := dispatchUsedRange.PropertyGet('Rows',[]);
  dispatchRows := System.DispatchHelper.Create(objRows);
  lRow := rRow + ObjectHelper.ToInt(dispatchRows.PropertyGet('Count',[])) - 1;
  objWorkFun := dispatchApplication.PropertyGet('WorksheetFunction',[]);
  dispatchWorkFun := System.DispatchHelper.Create(objWorkFun);
  for I := lRow downto rRow do
  begin
    objRow := dispatchUsedRange.PropertyGet('Rows',[I]);
    dispatchRow := System.DispatchHelper.Create(objRow);
    if ObjectHelper.ToInt(dispatchWorkFun.InvokeMethod('CountA',[objRow])) = 0 then   //执行到这句时出错,类型不匹配?怎么办?      
dispatchRow.InvokeMethod('Delete',[]);
  end;
  //*******
end;
回复

使用道具 举报

 楼主| 发表于 2011-9-16 09:23:17 | 显示全部楼层
起步的人员,帮忙看看啊
回复 支持 反对

使用道具 举报

发表于 2011-9-27 11:44:41 | 显示全部楼层
是遇到字符数据报错还是一运行就报错的?
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Justep Inc.

GMT+8, 2024-12-23 00:46 , Processed in 0.038412 second(s), 15 queries .

Powered by Discuz! X3.4

© 2001-2017 Comsenz Inc.

快速回复 返回顶部 返回列表