Forums before death by AOL, social media and spammers... "We can't have nice things"
|    comp.lang.visual.basic    |    MS Visual Basic discussions, NOT dot-net    |    10,840 messages    |
[   << oldest   |   < older   |   list   |   newer >   |   newest >>   ]
|    Message 9,696 of 10,840    |
|    Otie to All    |
|    GetObject and Excel - working with VB 5    |
|    19 Oct 05 13:49:06    |
   
   XPost: comp.lang.basic.visual   
   From: otie_nospam@adelphia.net   
      
   I found the following under the GetObject help notes and in the   
   example for GetObject:   
      
   "This example uses the GetObject function to get a reference to a   
   specific Microsoft Excel worksheet (MyXL). It uses the worksheet's   
   Application property to make Microsoft Excel visible, to close it, and   
   so on. Using two API calls, the DetectExcel Sub procedure looks for   
   Microsoft Excel, and if it is running, enters it in the Running Object   
   Table. The first call to GetObject causes an error if Microsoft Excel   
   isn't already running. In the example, the error causes the   
   ExcelWasNotRunning flag to be set to True. The second call to   
   GetObject specifies a file to open. If Microsoft Excel isn't already   
   running, the second call starts it and returns a reference to the   
   worksheet represented by the specified file, mytest.xls. The file must   
   exist in the specified location; otherwise, the Visual Basic error   
   Automation error is generated. Next, the example code makes both   
   Microsoft Excel and the window containing the specified worksheet   
   visible. Finally, if there was no previous version of Microsoft Excel   
   running, the code uses the Application object's Quit method to close   
   Microsoft Excel. If the application was already running, no attempt is   
   made to close it. The reference itself is released by setting it to   
   Nothing."   
      
      
   Wonderful!   
      
   BUT, what I want is to reference the current open file that has the   
   focus in Excel - I do not want to reference the specified file,   
   mytest.xls. See "LOOK HERE" below.   
      
      
   ' Declare necessary API routines:   
   Declare Function FindWindow Lib "user32" Alias _   
   "FindWindowA" (ByVal lpClassName as String, _   
    ByVal lpWindowName As Long) As Long   
      
   Declare Function SendMessage Lib "user32" Alias _   
   "SendMessageA" (ByVal hWnd as Long,ByVal wMsg as Long _   
    ByVal wParam as Long _   
    ByVal lParam As Long) As Long   
      
   Sub GetExcel()   
    Dim MyXL As Object ' Variable to hold reference   
    ' to Microsoft Excel.   
    Dim ExcelWasNotRunning As Boolean ' Flag for final release.   
      
   ' Test to see if there is a copy of Microsoft Excel already running.   
    On Error Resume Next ' Defer error trapping.   
   ' Getobject function called without the first argument returns a   
   ' reference to an instance of the application. If the application   
   isn't   
   ' running, an error occurs.   
    Set MyXL = Getobject(, "Excel.Application")   
    If Err.Number <> 0 Then ExcelWasNotRunning = True   
    Err.Clear ' Clear Err object in case error occurred.   
      
   ' Check for Microsoft Excel. If Microsoft Excel is running,   
      
   ' enter it into the Running Object table.   
    DetectExcel   
      
   'LOOK HERE   
   'Set the object variable to reference the file you want to see.   
    Set MyXL = Getobject("c:\vb4\MYTEST.XLS")   
   'LOOK HERE   
      
   ' Show Microsoft Excel through its Application property. Then   
   ' show the actual window containing the file using the Windows   
   ' collection of the MyXL object reference.   
    MyXL.Application.Visible = True   
    MyXL.Parent.Windows(1).Visible = True   
    ' Do manipulations of your   
    ' file here.   
      
   ' ...   
   ' If this copy of Microsoft Excel was not running when you   
   ' started, close it using the Application property's Quit method.   
   ' Note that when you try to quit Microsoft Excel, the   
   ' title bar blinks and a message is displayed asking if you   
   ' want to save any loaded files.   
    If ExcelWasNotRunning = True Then   
    MyXL.Application.Quit   
    End IF   
      
    Set MyXL = Nothing ' Release reference to the   
    ' application and spreadsheet.   
   End Sub   
      
      
      
   Thanks,   
   Otie   
      
   --- SoupGate-Win32 v1.05   
    * Origin: you cannot sedate... all the things you hate (1:229/2)   
|
[   << oldest   |   < older   |   list   |   newer >   |   newest >>   ]
(c) 1994, bbs@darkrealms.ca