Excel VBA Workbook Object
The Excel VBA Workbook object in VBA represents a workbook in Excel. The Open, Close, Save, Saveas, Activate and Export to PDF etc. are some of the methods that you can use with the Workbook object:
Workbook Open Method examples
Sub workbook_open_method()
\'Workbooks.Open \"C:\\Users\\Shantilal\\Desktop\\WBOM\\information.xlsx\"
Workbooks.Open Filename:=\"C:\\Users\\Shantilal\\Desktop\\WBOM\\information.xlsx\"
End Sub
Sub get_dialogbox()
Application.GetOpenFilename \' dialogbox open
End Sub
Sub workbook_open_password()
Workbooks.Open Filename:=\"C:\\Users\\Shantilal\\Desktop\\WBOM\\information.xlsx\", Password:=\"abc\"
End Sub
Sub workbook_open_readonly()
Workbooks.Open Filename:=\"C:\\Users\\Shantilal\\Desktop\\WBOM\\information.xlsx\", ReadOnly:=True, Password:=\"abc\"
End Sub
Sub workbook_close_method()
\'Workbooks(\"Information\").Close
\'Workbooks(\"Information\").Close savechanges:=True
End Sub
Workbook Save and Saveas Methods
Sub create_newfile()
\'Workbooks.Add
Workbooks.Add.Save \' save to default location that is mydocument having default name i.e. book1/book2
End Sub
Sub save_file()
Workbooks(\"Book1\").Save \' save without macros
End Sub
Sub saveas_file()
Workbooks(\"book1\").SaveAs Filename:=\"C:\\Users\\Shantilal\\Desktop\\data\\d\\df\\book1\", FileFormat:=52, CreateBackup:=True
End Sub
Sub saveascopy()
Workbooks(\"book1\").SaveCopyAs Filename:=\"C:\\Users\\Shantilal\\Desktop\\data\\d\\df\\dr\\book1.xlsm\"
End Sub
Workbook Export to pdf methods
Sub test()
ActiveSheet.ExportAsFixedFormat _
Type:=xlTypePDF, _
Filename:=\"C:\\Users\\Shantilal\\Desktop\\data\\ss\", _
Quality:=xlQualityStandard, _
IncludeDocProperties:=False, _
IgnorePrintAreas:=False, _
From:=1, _
To:=5, _
OpenAfterPublish:=True
End Sub
Sub export_file()
ActiveWorkbook.ExportAsFixedFormat Type:=xlTypePDF
End Sub
Sub export_file_location()
ActiveWorkbook.ExportAsFixedFormat Type:=xlTypePDF, Filename:=\"C:\\Users\\Shantilal\\Desktop\\data\\sees\"
End Sub