PDA

View Full Version : Do I have to declare it when its running?



prabhafriend
01-05-2010, 01:38 AM
Hi!
I already referenced the Excel application in a word VBA project. If its already opened (the excel application) then once again do I have to declare an application to work with that Excel application.

Sub macinwordwhenexcelisrunning()
excel.activecell.value = "Someval"
end sub

Why dont it works?

RolfJ
01-06-2010, 05:41 PM
From my experience it works best when you are very explicit about what you are trying to do. Here is an example that opens an Excel workbook from within a VBA module in MSWord and then writes a value to cell A1 in the first worksheet (pleae note that the path name is the full file name, e.g. "C:/..../Test.xls"):

Dim xl As New Excel.Application
xl.Visible = True
Dim wb As Workbook
Set wb = xl.Workbooks.Open({workbook path name})
Dim sh As Worksheet
Set sh = wb.Sheets(1)
sh.Range("A1").Value = "Someval"