View Single Post
 
Old 05-28-2014, 02:20 AM
Sorcerer13 Sorcerer13 is offline Windows Vista Office 2010 32bit
Novice
 
Join Date: Sep 2012
Location: God's Own County
Posts: 16
Sorcerer13 is on a distinguished road
Default Nice try, but no cigar!

Paul....

Thanks for the reply, but the same thing happens with YOUR code (slightly modified, see below: duplicate "As" removed, and lines to render an existing Excel instance visible).

If you put this code in a word module, set the references, then step through via PF8, even if every instance of Excel has been shut down, it appears that the GetObject actually starts Excel!

Even if an existing Excel document is open when the Word VBA runs, the VBA opens another Excel document!

I don't think what I'm trying to do is Rocket Science, but it sure seems difficult!

Any ideas???


Code:
Public Sub TestExcelFromWord()
Dim gxlApp                  As Excel.Application
' Needs reference to Microsoft Excel 14.0 Object Library
Dim gbooExcelIsRunning      As Boolean
' Test whether Excel is already running.
  On Error Resume Next
  gbooExcelIsRunning = False ' Flag to record if we start Excel, so we can close it later.
  Set gxlApp = GetObject(, "Excel.Application")
'Start Excel if it isn't running
  If gxlApp Is Nothing Then
    Set gxlApp = CreateObject("Excel.Application")
    If gxlApp Is Nothing Then
      MsgBox "Can't start Excel.", vbExclamation
      Exit Sub
    End If
  ' Record that we've started Excel.
    gbooExcelIsRunning = True
   Else
    gxlApp.Visible = True
  End If
  On Error GoTo 0
End Sub
Reply With Quote