Workbook對(duì)象代表Excel的一個(gè)工作簿,Workbook和Application的關(guān)系就是工作簿和Excel程序的關(guān)系
1. ActiveSheet屬性
ActiveSheet是一個(gè)Worksheet對(duì)象(工作表)
- Dim ws As Worksheet
- Set ws = Application.ActiveSheet
- Debug.Print ws.name
- Set ws = ActiveSheet
- Debug.Print ws.name
- Set ws = Application.ActiveWorkbook.ActiveSheet
- Debug.Print ws.name
2. EnableAutoRecovery屬性,用于設(shè)置Excel自動(dòng)恢復(fù)功能
- If Not ActiveWorkbook.EnableAutoRecover Then
- ActiveWorkbook.EnableAutoRecover = True
- Debug.Print 'Manul Open'
- Else
- Debug.Print 'Opened'
- End If
- If ActiveWorkbook.HasPassword Then
- Debug.Print 'Has'
- Else
- Debug.Print 'Hasn't'
- End If
- Dim wb As Workbook
- Set wb = Application.WorkBooks.Open('C:\book.xlsx') '給C盤文件加密,密碼為haha
- wb.Password = 'haha'
- wb.Save
- wb.Close
去除密碼只需Password = ''5.其他屬性就不一一列舉了,用于獲取工作簿的信息,代碼如下
- Dim wb As Workbook
- Set wb = Application.ActiveWorkbook
- Debug.Print wb.name '文件名
- Debug.Print wb.FullName '全路徑
- Debug.Print wb.Path '路徑
- Debug.Print wb.ReadOnly '是否只讀
vba.xlsm
E:\Users\Desktop\vba.xlsm
E:\Users\Desktop
False
聯(lián)系客服