tags:

views:

37

answers:

1

How do I edit excel spreadsheets from word using VBA?

+3  A: 

First you need to set a reference to the version of Excel you are running. In the VBE go to Tools>References and click Microsoft Excel 12.0 Object Library (12.0 for 2007, 11.0 for 2003) etc.

Then you can code something like this (opens a new instance of Excel, opens, edits and saves a new workbook). You'd use GetObject to access a running instance of Excel:

Sub EditExcelFromWord()

Dim appExcel As Excel.Application
Dim wb As Excel.Workbook
Dim ws As Excel.Worksheet

Set appExcel = CreateObject("Excel.Application")
With appExcel
    .Visible = True
    Set wb = .Workbooks.Add
    Set ws = wb.Worksheets(1)
    ws.Range("A1").Value2 = "Test"
    wb.SaveAs ThisDocument.Path & Application.PathSeparator & "temp.xls"
    Stop 'admire your work and then click F5 to continue
    Set ws = Nothing
    Set wb = Nothing
    Set appExcel = Nothing

End With

End Sub
Doug Glancy
You do not need to use CreateObject("Excel.Application") if you have a reference to the Excel library. CreateObject is for late binding, you can use New instead: Dim appExcel As New Excel.Application
Remou
Remou, that's true!
Doug Glancy
Thanks for the STOP command! I was looking for that! This is the best answer without a question!
Arlen Beiler