Dynamically Creating Excel and Word OLE Objects
Visual Basic 17 julio 2007
MS EXCEL Example
Dim x1 as Object ‘either put this declaration at Form level or make it static.
Set x1 = CreateObject(“Excel.Application”)
x1.Visible = True
x1.Workbooks.Add ‘next three commands specific to object
x1.Range(“A1″).Value = “Hello World”
‘ assign value in spreadsheet to a control on our Form
x1.Range(“A2″).Value = txtPerson.text
xl.Workbooks.Open “c:\invoice.xls” ‘open another doc
x1.ActiveWorkbook.Close ! close workbook (follow by “False” for no prompt)
x1.Quit !close App
Excel Example, with Error Handling
Dim xl As Excel.Application
Dim xlwb As Excel.WorkBook
On Error Resume Next
set xl = CreateObject (“Excel.Application”)
set xlwb = xl.Workbooks.Open (“c:\book1.xls “)
If Err <> 0 Then
MsgBox “Unable to open workbook”
UnLoad Me
End If
MS WORD Example
‘either put this declaration at Form level or make it static.
‘Otherwise, object is destroyed when script has run
Dim wd As Object
Set wd = CreateObject (“Word.Basic”)
wd.AppShow
wd.FileNewDefault
wd.FontSize 24
wd.Insert “Hello, World”
More Sample Word Code
wd.FileOpen “MyDoc.Doc”
wd.FileNewDefault
wd.FileNew “invoice.dot” ‘template name
wd.FileSave
wd.FileSaveAs “NEWNAME.DOC”
wd.FileClose
wd.Fileprint
Set wd = Nothing ‘closes Word if no one else is using it
Vota este artículo:
Posts anterior y posterior:
Posts Relacionados:
- Previo: « Setup and Use of Data Access Object (DAO) for Window that Maintains one Row at a Time
- Siguiente: Colocar icono junto al reloj (Visual Basic). »


Comentarios Recientes