wackywoo105
Registered User.
- Local time
- Today, 15:35
- Joined
- Mar 14, 2014
- Messages
- 203
I've been trying to adapt some code so I can take control of an open excel workbook.
For some reason it worked on and off at first. Now it just doesn't detect the already open workbook at all?
Previously I would check if the workbook was open and require it to be closed, so as not to open it again, but I would like to be able to just take control of it if it is already open. I tried using the code as below:
As the code doesn't recognise the already open excel it just opens the file again, which of course is read only and thus the title has read only in it.
Can anyone help with setting a oXLBook to an already open workbook and taking control of the corresponding excel application with oXLApp?
Code:
Dim WB As excel.Workbook
Dim myWB As String
myWB = "LabOrderSheet.xlsx"
For Each WB In Workbooks
MsgBox WB.Name
If WB.Name = myWB Then
WB.Activate
MsgBox "Workbook Found!"
Exit Sub
End If
Next WB
MsgBox "Not Found"
Set WB = Nothing
For some reason it worked on and off at first. Now it just doesn't detect the already open workbook at all?
Previously I would check if the workbook was open and require it to be closed, so as not to open it again, but I would like to be able to just take control of it if it is already open. I tried using the code as below:
Code:
If Dir(gdrive & "LabOrderSheet.xlsx") = "" Then
MsgBox "LabOrderSheet.xlsx sheet is missing."
Exit Sub
Else
'Ret = IsWorkBookOpen(gdrive & "LabOrderSheet.xlsx")
'If Ret = True Then
' MsgBox ("LabOrderSheet sheet must be closed before running this. Please close it and try again.")
' Exit Sub
'End If
End If
Dim oXLBook As excel.Workbook
Dim oXLSheet As excel.Worksheet
Dim oXLApp As excel.Application 'Declare the object variables
'https://excelchamps.com/vba/check-workbook-open/
Dim myWB As String
'myWB = InputBox(Prompt:="Enter the workbook name.")
myWB = "LabOrderSheet.xlsx"
For Each oXLBook In Workbooks
If oXLBook.Name = myWB Then
Set oXLApp = excel.Application
oXLBook.Activate
Set oXLSheet = oXLBook(oXLBook.Name).Worksheets(1)
MsgBox "Workbook Found!"
GoTo FoundIt
End If
Next oXLBook
MsgBox "Not Found"
Set oXLApp = New excel.Application 'Create a new instance of Excel
Set oXLBook = oXLApp.Workbooks.Open(gdrive & "LabOrderSheet.xlsx") 'Open an existing workbook
Set oXLSheet = oXLBook.Worksheets(1)
FoundIt:
Set oXLSheet = oXLBook.Worksheets(1)
*/ DO STUFF ./*
oXLApp.DisplayAlerts = True
oXLApp.Application.EnableEvents = True
oXLApp.Visible = True
oXLBook.Activate
oXLApp.Application.WindowState = xlMaximized
Set oXLSheet = Nothing
Set oXLBook = Nothing
Set oXLApp = Nothing
As the code doesn't recognise the already open excel it just opens the file again, which of course is read only and thus the title has read only in it.
Can anyone help with setting a oXLBook to an already open workbook and taking control of the corresponding excel application with oXLApp?