View Single Post
  #3   Report Post  
Posted to microsoft.public.excel.programming,microsoft.public.vb.general.discussion
jack jack is offline
external usenet poster
 
Posts: 97
Default programming issue when using Excel automation


I do not understand your approach and how that can help me.
In my case I want to have both:
1. being able to pull data out of spreadsheet
and
2. do not interrupt user if he is using spreadsheet at the same time.

Is that possible?
Thanks,
Jack

"Peter T" <peter_t@discussions wrote in message
...
Indeed you'll get that massage trying to attach a reference to an Excel
instance when it is in edit mode (tying in a cell before pressing enter).

Following worked for me running in VB6 running from the IDE (having first
put Excel into edit mode):
-If Excel is not the active window bring it to front
- Sendkeys Escape

It means of course user might lose whatever was typed into some cell
before
pressing Enter, in practice relatively rare.

Private Declare Function FindWindow Lib "user32" _
Alias "FindWindowA" ( _
ByVal lpClassName As String, _
ByVal lpWindowName As String) As Long

Private Declare Function GetActiveWindow Lib "user32" () As Long

Private Declare Function SetForegroundWindow Lib "user32" ( _
ByVal hwnd As Long) As Long

Private moExcelApp As Object ' As Excel.Application


Private Sub Form_Click()
Dim bXLnotActive As Long
Dim hWinActive
Dim hXLwin As Long

hXLwin = FindWindow("XLMAIN", vbNullString)

If hXLwin = 0 Then
' no xl instance, use creatobject
Else

hWinActive = GetActiveWindow
bXLnotActive = hWinActive < hXLwin
If bXLnotActive Then SetForegroundWindow hXLwin

Set moExcelApp = GetObject(, "Excel.Application")
SendKeys "{ESC}"

'if bXLnotActive then reactivate the original active window, maybe

End If

Print moExcelApp.Caption
End Sub

Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)
Set moExcelApp = Nothing
End Sub

I spent a while fiddling around with the Sendkeys in advance of doing the
GetObject. Nothing worked, even using the keyboard event API instead of
SendKeys. It seems counter intuitive but putting SendKeys after the
GetObject seems to do the trick, at least it does for me. Not sure why
though and it might not be the best way.

Regards
Peter T

"Jack" <replyto@it wrote in message
...
Hello,
My application interacts with Excel spreadsheet. Periodically
it
reads some data from the predefined sheet's column.
Everything works fine, when user does not use that spreadsheet at the
same
time.
However, when user is typing in the sheet at the same time when my app

wants
to read data from it there is a problem. Excel displays 'Component Busy'
message with the Switch to..., Retry and Cancel options.

How to solve that problem?
My app uses Excel automation and it connects to Excel using this code:

Public WithEvents moExcelApp As Excel.Application

Set moExcelApp = GetObject(, "Excel.Application")
If moExcelApp Is Nothing Then
Set moExcelApp = CreateObject("Excel.Application")
End If

Your comments appreciated,
Jack