Results 1 to 4 of 4

Thread: problem

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Nov 2002
    Posts
    23

    Question problem

    I am using an api function to open excel and then tell my program when the user closes excel.

    Declared constants and functions


    Code:
    Const SYNCHRONIZE = &H100000
    Const INFINITE = &HFFFF 'Wait forever
    Const WAIT_OBJECT_0 = 0 'The state of the specified object is signaled
    Const WAIT_TIMEOUT = &H102 'The time-out interval elapsed & the object’s state is nonsignaled.
     
    Private Declare Function OpenProcess Lib "kernel32" (ByVal dwDesiredAccess As Long, _
                ByVal bInheritHandle As Long, ByVal dwProcessId As Long) As Long
    
    Private Declare Function WaitForSingleObject Lib "kernel32" (ByVal hHandle As Long, _
                ByVal dwMilliseconds As Long) As Long
    
    Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long

    The problem is in this code here. Inorder to open to open the excel use the api code i have to use the shell command. But for the functionallity of my program, i need it to open a workbook. So I have done this. The problem is that it is opening 2 workbooks. Mine and a blank one, i need to eliminate the blank one or alter the code to work properly. Any Ideas. Thanks

    Code:
    Private Sub ExcelOpen()
        Dim lPid As Long
        Dim lHnd As Long
        Dim lRet As Long
        
        lPid = Shell("C:\Program Files\Microsoft Office\Office\EXCEL.EXE", vbNormalFocus)
        Set obExcelApp = CreateObject("Excel.Application")
        Set obWorkBook = obExcelApp.Workbooks.Open(App.Path & "\line.xls")
        obExcelApp.Visible = True
        If lPid <> 0 Then
            'Get a handle to the shelled process.
            lHnd = OpenProcess(SYNCHRONIZE, 0, lPid)
            'If successful, wait for the application to end and close the handle.
            If lHnd <> 0 Then
                    lRet = WaitForSingleObject(lHnd, INFINITE)
                    CloseHandle (lHnd)
            End If
            MsgBox "Just terminated.", vbInformation, "Shelled Application"
            'cmdSave_Click
        End If
        On Error GoTo Line1
        obWorkBook.Close
    Line1:
        Set obWorkBook = Nothing
        Set obExcelApp = Nothing
    End Sub

  2. #2
    Frenzied Member andreys's Avatar
    Join Date
    Sep 2002
    Location
    Los Angeles
    Posts
    1,615
    Try to use GetObject() instead of CreateObject()

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Nov 2002
    Posts
    23
    Code:
        Set obExcelApp = GetObject("Excel.Application")
    i used the getobject like you said however it just crashed the program. What am i doing wrong

    Thanks

  4. #4
    Frenzied Member andreys's Avatar
    Join Date
    Sep 2002
    Location
    Los Angeles
    Posts
    1,615
    By MSDN
    GetObject Function Example
    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.

    VB Code:
    1. ' Declare necessary API routines:
    2. Declare Function FindWindow Lib "user32" Alias _
    3. "FindWindowA" (ByVal lpClassName as String, _
    4.                ByVal lpWindowName As Long) As Long
    5.  
    6. Declare Function SendMessage Lib "user32" Alias _
    7. "SendMessageA" (ByVal hWnd as Long,ByVal wMsg as Long, _
    8.                ByVal wParam as Long, _
    9.                ByVal lParam As Long) As Long
    10.  
    11. Sub GetExcel()
    12.    Dim MyXL As Object   ' Variable to hold reference
    13.                         ' to Microsoft Excel.
    14.    Dim ExcelWasNotRunning As Boolean   ' Flag for final release.
    15.  
    16. ' Test to see if there is a copy of Microsoft Excel already running.
    17.    On Error Resume Next   ' Defer error trapping.
    18. ' Getobject function called without the first argument returns a
    19. ' reference to an instance of the application. If the application isn't
    20. ' running, an error occurs.
    21.    Set MyXL = Getobject(, "Excel.Application")
    22.    If Err.Number <> 0 Then ExcelWasNotRunning = True
    23.    Err.Clear   ' Clear Err object in case error occurred.
    24.  
    25. ' Check for Microsoft Excel. If Microsoft Excel is running,
    26. ' enter it into the Running Object table.
    27.    DetectExcel
    28.  
    29. ' Set the object variable to reference the file you want to see.
    30.    Set MyXL = Getobject("c:\vb4\MYTEST.XLS")
    31.  
    32. ' Show Microsoft Excel through its Application property. Then
    33. ' show the actual window containing the file using the Windows
    34. ' collection of the MyXL object reference.
    35.    MyXL.Application.Visible = True
    36.    MyXL.Parent.Windows(1).Visible = True
    37.     Do manipulations of your  file here.
    38.    ' ...
    39. ' If this copy of Microsoft Excel was not running when you
    40. ' started, close it using the Application property's Quit method.
    41. ' Note that when you try to quit Microsoft Excel, the
    42. ' title bar blinks and a message is displayed asking if you
    43. ' want to save any loaded files.
    44.    If ExcelWasNotRunning = True Then
    45.       MyXL.Application.Quit
    46.    End IF
    47.  
    48.    Set MyXL = Nothing   ' Release reference to the
    49.                         ' application and spreadsheet.
    50. End Sub
    51.  
    52. Sub DetectExcel()
    53. ' Procedure dectects a running Excel and registers it.
    54.    Const WM_USER = 1024
    55.    Dim hWnd As Long
    56. ' If Excel is running this API call returns its handle.
    57.    hWnd = FindWindow("XLMAIN", 0)
    58.    If hWnd = 0 Then   ' 0 means Excel not running.
    59.       Exit Sub
    60.    Else            
    61.    ' Excel is running so use the SendMessage API
    62.    ' function to enter it in the Running Object Table.
    63.       SendMessage hWnd, WM_USER + 18, 0, 0
    64.    End If
    65. End Sub

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width