下面是vb打開文件的代碼: If Dir('D:\excel.bz') = '' Then '判斷EXCEL是否打開 Set xlApp = CreateObject('Excel.Application') '創(chuàng)建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編程
|
|