下面是vb打开文件的代码:
If Dir("D:/excel.bz") = "" Then '判断EXCEL是否打开
Set xlApp = CreateObject("Excel.Application") '创建EXCEL应用类
xlApp.Visible = True '设置EXCEL可见
Set xlBook = xlApp.Workbooks.Open("D:/test.xls") '打开EXCEL工作簿
Set xlSheet = xlBook.Worksheets(1) '打开EXCEL工作表
ActiveWorkbook.Save
xlApp.DisplayAlerts = False
xlApp.ScreenUpdating = True
xlApp.Quit
End If
然后打开EXCEL工作表,在工作表上做本来程序准备做的操作并录制成“宏”,直接拷贝到上面的空白处。就搞定了。
宏代码如下(VBA代码,其中少量代码按照VBA的编程语法做了修改):
' 下面信息来自宏
Range("A4:C4").Select
Selection.Copy
Range("A11").Select
ActiveSheet.Paste
Range("A3:C3").Select
Application.CutCopyMode = False
Selection.Copy
Range("A4").Select
ActiveSheet.Paste
Range("A5").Select
ActiveSheet.Paste
Range("A6").Select
ActiveSheet.Paste
Range("A7").Select
ActiveSheet.Paste
Range("A8").Select
ActiveSheet.Paste
Range("A9").Select
ActiveSheet.Paste
Range("A10").Select
ActiveSheet.Paste
Application.CutCopyMode = False
For i = 1 To 9
Range("A" & (i + 1)).Select
ActiveCell.FormulaR1C1 = "项目" & i
Range("B" & (i + 1)).Select
ActiveCell.FormulaR1C1 = i
Range("C" & (i + 1)).Select
ActiveCell.FormulaR1C1 = "说明" & i
Next
Range("B11").Select
ActiveCell.FormulaR1C1 = "=SUM(R[-9]C:R[-1]C)"
对WORD的操作很类似,其他语言(例如c++)可以按照这种语法结构做COM编程