powered by simpleCommunicator - 2.0.61     © 2026 Programmizd 02
Целевая тема:
Создать новую тему:
Автор:
Закрыть
Цитировать
Форумы / Microsoft Access [игнор отключен] [закрыт для гостей] / Как узнать, открыт ли Excel?
3 сообщений из 3, страница 1 из 1
Как узнать, открыт ли Excel?
    #32228543
Фотография Albert Gor
Скрыть профиль Поместить в игнор-лист Сообщения автора в теме
Участник
Вопрос бы не возник, да Excel генерит одну и ту же ошибку номер 1004 для различных ситуаций, которые требуется обрабатывать по разному. А именно:

1. Пользователь жмет на НЕТ в окошке сообщающем, что такой файл уже существует и предлагающем его затереть новым.

2. Старый файл существует и уже открыт в Экселе.
...
Рейтинг: 0 / 0
Как узнать, открыт ли Excel?
    #32228832
Alexus12
Скрыть профиль Поместить в игнор-лист Сообщения автора в теме
Участник
A2000 Help
================
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.

' 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

' Set the object variable to reference the file you want to see.
Set MyXL = Getobject("c:\vb4\MYTEST.XLS")

' 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

Sub DetectExcel()
' Procedure dectects a running Excel and registers it.
Const WM_USER = 1024
Dim hWnd As Long
' If Excel is running this API call returns its handle.
hWnd = FindWindow("XLMAIN", 0)
If hWnd = 0 Then ' 0 means Excel not running.
Exit Sub
Else
' Excel is running so use the SendMessage API
' function to enter it in the Running Object Table.
SendMessage hWnd, WM_USER + 18, 0, 0
End If
End Sub
...
Рейтинг: 0 / 0
Как узнать, открыт ли Excel?
    #32228930
Фотография Albert Gor
Скрыть профиль Поместить в игнор-лист Сообщения автора в теме
Участник
2 Alexus12 from Small city: MOS_COW

Спасибо!
Я позволил себе отрезать лишнее :)

Код: plaintext
1.
2.
Declare Function FindWindow Lib  "user32"  Alias _
 "FindWindowA"  (ByVal lpClassName As String, _
ByVal lpWindowName As Long) As Long


Код: plaintext
1.
2.
3.
4.
5.
6.
7.
8.
9.
Public Function DetectExcel() As Boolean
' Dectects a running Excel '
  Dim hWnd As Long
  hWnd = FindWindow( "XLMAIN" ,  0 )
  If hWnd =  0  Then
    DetectExcel = False
  Else
    DetectExcel = True
  End If
End Function
...
Рейтинг: 0 / 0
3 сообщений из 3, страница 1 из 1
Форумы / Microsoft Access [игнор отключен] [закрыт для гостей] / Как узнать, открыт ли Excel?
Найденые пользователи ...
Разблокировать пользователей ...
Читали форум (0):
Пользователи онлайн (0):
x
x
Закрыть


Просмотр
0 / 0
Close
Debug Console [Select Text]