CREATE EXCEL OR WORD FILE FROM ACCESS

Create Excel File or Word Document from Microsoft Access and write information into them. Every application that supports Automation provides at least one type of object. For example, a word processing application may provide an Application object, a Document object and a Toolbar object. To create an ActiveX object, assign the object returned by CreateObject to an object variable. The first example creates a Word File and writes some text into it and saves with a name.

Public Sub CreateWordDoc() 
Dim WordObj As Object  
On Error goto CreateWordDoc_Err  
Set WordObj = CreateObject("word.application") 
WordObj.Application.Visible = True 
WordObj.Application.Documents.Add "Normal", , 0, True 
WordObj.ActiveDocument.Content = "THIS IS MY TEST DOCUMENT." 
WordObj.Application.ActiveDocument.SaveAs "C:\myDocument2.doc" 
WordObj.Application.Quit  
Set WordObj = Nothing 

CreateWordDoc_Exit: 
Exit Sub  

CreateWordDoc_Err: 
msgbox Err.Description,,"CreateWordDoc" 
Resume CreateWordDoc_Exit  
End Sub 

The Next example creates an Excel Worksheet and writes a line of text in Column A, Row 1 and saves it with a Name. This code starts the application creating the object, in this case, a Microsoft Excel spreadsheet. Once an object is created, you reference it in code using the object variable you defined. You access properties and methods of the new object using the object variable, ExcelSheet and other Microsoft Excel objects, including the Application object and the Cells collection.

Public Sub CreateExcelSheet() 
Dim ExcelSheet As Object 
On Error goto CreateExcelSheet_Err 
Set ExcelSheet = CreateObject("Excel.Sheet") 
ExcelSheet.Application.Visible = True 
ExcelSheet.Application.Cells(1, 1).Value = "This is Column A, row 1" 
ExcelSheet.SaveAs "C:\TEST.XLS" 
ExcelSheet.Application.Quit 
Set ExcelSheet = Nothing 

CreateExcelSheet_Exit: 
Exit Sub 

CreateExcelSheet_Err: 
Msgbox Err.Description,,"CreateExcelSheet" 
Resume CreateExcelSheet_Exit 
End Sub 

Next >> Create Text File from Access.