dbTalk Databases Forums  

Opening XL and Word from Access 2010

comp.databases.ms-access comp.databases.ms-access


Discuss Opening XL and Word from Access 2010 in the comp.databases.ms-access forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
Bob Darlington
 
Posts: n/a

Default Opening XL and Word from Access 2010 - 11-11-2011 , 01:22 AM






I've got (among other things) an Excel and a Word button on a ribbon bar.
They each call the following code which works as intended, but Excel opens
on top of my application (which is Maximised), while the Word document opens
on the task bar only.
How can I get them both to open the same way?

Dim objOffice As Object
Dim strApp As String

Select Case vAppType
Case 1:
strApp = "Excel"
Case 2:
strApp = "Word"
End Select

If Not IsAppRunning(strApp) Then
Set objOffice = CreateObject(strApp & ".Application")
Else
Set objOffice = GetObject(, strApp & ".Application")
End If

objOffice.Application.Visible = True
Select Case vAppType
Case 1:
objOffice.Workbooks.Add
Case 2:
objOffice.Documents.Add
objOffice.Documents(1).Activate
objOffice.Application.WindowState = 0 ' wdWindowStateNormal
End Select

--
Bob Darlington
Brisbane

Reply With Quote
  #2  
Old   
Phil
 
Posts: n/a

Default Re: Opening XL and Word from Access 2010 - 11-11-2011 , 04:07 AM






On 11/11/2011 07:22:56, "Bob Darlington" wrote:
Quote:
I've got (among other things) an Excel and a Word button on a ribbon bar.
They each call the following code which works as intended, but Excel opens
on top of my application (which is Maximised), while the Word document
opens on the task bar only.
How can I get them both to open the same way?

Dim objOffice As Object
Dim strApp As String

Select Case vAppType
Case 1:
strApp = "Excel"
Case 2:
strApp = "Word"
End Select

If Not IsAppRunning(strApp) Then
Set objOffice = CreateObject(strApp & ".Application")
Else
Set objOffice = GetObject(, strApp & ".Application")
End If

objOffice.Application.Visible = True
Select Case vAppType
Case 1:
objOffice.Workbooks.Add
Case 2:
objOffice.Documents.Add
objOffice.Documents(1).Activate
objOffice.Application.WindowState = 0 ' wdWindowStateNormal
End Select

This may give you a start (or not)

Private Declare Function apiGetDesktopWindow Lib "user32" Alias _
"GetDesktopWindow" () As Long

Private Declare PtrSafe Function GetWindow Lib "user32" _
(ByVal hwnd As Long, ByVal wCmd As Long) As Long

Private vate Declare PtrSafe Function GetWindowLong Lib "user32" Alias
"GetWindowLongA" _ (ByVal hwnd As Long, ByVal nIndex As Long) As Long

Private Declare Function apiGetClassName Lib "user32" Alias _
"GetClassNameA" (ByVal hwnd As Long, _
ByVal lpClassName As String, ByVal nMaxCount As Long) As Long

Function fEnumWindows()

Dim lngX As Long
Dim lngStyle As Long, strCaption As String
Dim ThisDBCaption As String
Dim ThisDbHwnd As Long

Dim Msg As String

lngX = apiGetDesktopWindow()
'Return the first child to Desktop
lngX = apiGetWindow(lngX, mcGWCHILD)

Do While Not lngX = 0
strCaption = fGetCaption(lngX)
If Len(strCaption) > 0 Then
Debug.Print strCaption
lngStyle = apiGetWindowLong(lngX, mcGWLSTYLE)
'enum visible windows only
If fGetClassName(lngX) = "OMain" Then ' Databases
If lngStyle And mcWSVISIBLE Then
If ThisDBCaption = "" Then
ThisDBCaption = fGetCaption(lngX)
ThisDbHwnd = lngX
Else
If ThisDBCaption = fGetCaption(lngX) Then
Msg Msg = "Do you want 2 copies of " & ThisDBCaption & " running?" If If
MsgBox(Msg, vbQuestion + vbYesNo + vbDefaultButton2) = vbNo Then Call
fSetAccessWindow(SW_SHOWMAXIMIZED) Application.Quit
Else
Exit Function
End If
End If
End If
GoTo NextWindow
End If
End If
End If
NextWindow:
lngX = apiGetWindow(lngX, mcGWHWNDNEXT)
Loop

End Function

Private Function fGetClassName(hwnd As Long) As String
Dim strBuffer As String
Dim intCount As Integer

strBuffer = String$(mconMAXLEN - 1, 0)
intCount = apiGetClassName(hwnd, strBuffer, mconMAXLEN)
If intCount > 0 Then
fGetClassName = Left$(strBuffer, intCount)
End If

End Function

Reply With Quote
  #3  
Old   
Arvin Meyer
 
Posts: n/a

Default Re: Opening XL and Word from Access 2010 - 11-11-2011 , 07:16 PM



This will open any associated application the way you want:

http://access.mvps.org/access/api/api0018.htm

--
Arvin Meyer, MCP, MVP
http://www.datastrat.com
http://www.accessmvp.com
http://access.mvps.org
Co-author: "Access Solutions", published by Wiley


"Bob Darlington" <bob (AT) notheredpcman (DOT) com.au> wrote

Quote:
I've got (among other things) an Excel and a Word button on a ribbon bar.
They each call the following code which works as intended, but Excel opens
on top of my application (which is Maximised), while the Word document
opens on the task bar only.
How can I get them both to open the same way?

Dim objOffice As Object
Dim strApp As String

Select Case vAppType
Case 1:
strApp = "Excel"
Case 2:
strApp = "Word"
End Select

If Not IsAppRunning(strApp) Then
Set objOffice = CreateObject(strApp & ".Application")
Else
Set objOffice = GetObject(, strApp & ".Application")
End If

objOffice.Application.Visible = True
Select Case vAppType
Case 1:
objOffice.Workbooks.Add
Case 2:
objOffice.Documents.Add
objOffice.Documents(1).Activate
objOffice.Application.WindowState = 0 ' wdWindowStateNormal
End Select

--
Bob Darlington
Brisbane

Reply With Quote
  #4  
Old   
Bob Darlington
 
Posts: n/a

Default Re: Opening XL and Word from Access 2010 - 11-14-2011 , 12:57 AM



Thanks Phil & Arvin. I'll give it a run.

--
Bob Darlington
Brisbane
"Bob Darlington" <bob (AT) notheredpcman (DOT) com.au> wrote

Quote:
I've got (among other things) an Excel and a Word button on a ribbon bar.
They each call the following code which works as intended, but Excel opens
on top of my application (which is Maximised), while the Word document
opens on the task bar only.
How can I get them both to open the same way?

Dim objOffice As Object
Dim strApp As String

Select Case vAppType
Case 1:
strApp = "Excel"
Case 2:
strApp = "Word"
End Select

If Not IsAppRunning(strApp) Then
Set objOffice = CreateObject(strApp & ".Application")
Else
Set objOffice = GetObject(, strApp & ".Application")
End If

objOffice.Application.Visible = True
Select Case vAppType
Case 1:
objOffice.Workbooks.Add
Case 2:
objOffice.Documents.Add
objOffice.Documents(1).Activate
objOffice.Application.WindowState = 0 ' wdWindowStateNormal
End Select

--
Bob Darlington
Brisbane

Reply With Quote
  #5  
Old   
Bob Darlington
 
Posts: n/a

Default Re: Opening XL and Word from Access 2010 - 11-14-2011 , 01:30 AM



"Phil" <phil (AT) stantonfamily (DOT) co.uk> wrote

Quote:
On 11/11/2011 07:22:56, "Bob Darlington" wrote:
I've got (among other things) an Excel and a Word button on a ribbon bar.
They each call the following code which works as intended, but Excel
opens
on top of my application (which is Maximised), while the Word document
opens on the task bar only.
How can I get them both to open the same way?

Dim objOffice As Object
Dim strApp As String

Select Case vAppType
Case 1:
strApp = "Excel"
Case 2:
strApp = "Word"
End Select

If Not IsAppRunning(strApp) Then
Set objOffice = CreateObject(strApp & ".Application")
Else
Set objOffice = GetObject(, strApp & ".Application")
End If

objOffice.Application.Visible = True
Select Case vAppType
Case 1:
objOffice.Workbooks.Add
Case 2:
objOffice.Documents.Add
objOffice.Documents(1).Activate
objOffice.Application.WindowState = 0 ' wdWindowStateNormal
End Select


This may give you a start (or not)

Private Declare Function apiGetDesktopWindow Lib "user32" Alias _
"GetDesktopWindow" () As Long

Private Declare PtrSafe Function GetWindow Lib "user32" _
(ByVal hwnd As Long, ByVal wCmd As Long) As Long

Private vate Declare PtrSafe Function GetWindowLong Lib "user32" Alias
"GetWindowLongA" _ (ByVal hwnd As Long, ByVal nIndex As Long) As Long

Private Declare Function apiGetClassName Lib "user32" Alias _
"GetClassNameA" (ByVal hwnd As Long, _
ByVal lpClassName As String, ByVal nMaxCount As Long) As Long

Function fEnumWindows()

Dim lngX As Long
Dim lngStyle As Long, strCaption As String
Dim ThisDBCaption As String
Dim ThisDbHwnd As Long

Dim Msg As String

lngX = apiGetDesktopWindow()
'Return the first child to Desktop
lngX = apiGetWindow(lngX, mcGWCHILD)

Do While Not lngX = 0
strCaption = fGetCaption(lngX)
If Len(strCaption) > 0 Then
Debug.Print strCaption
lngStyle = apiGetWindowLong(lngX, mcGWLSTYLE)
'enum visible windows only
If fGetClassName(lngX) = "OMain" Then ' Databases
If lngStyle And mcWSVISIBLE Then
If ThisDBCaption = "" Then
ThisDBCaption = fGetCaption(lngX)
ThisDbHwnd = lngX
Else
If ThisDBCaption = fGetCaption(lngX) Then
Msg Msg = "Do you want 2 copies of " & ThisDBCaption & " running?" If If
MsgBox(Msg, vbQuestion + vbYesNo + vbDefaultButton2) = vbNo Then Call
fSetAccessWindow(SW_SHOWMAXIMIZED) Application.Quit
Else
Exit Function
End If
End If
End If
GoTo NextWindow
End If
End If
End If
NextWindow:
lngX = apiGetWindow(lngX, mcGWHWNDNEXT)
Loop

End Function

Private Function fGetClassName(hwnd As Long) As String
Dim strBuffer As String
Dim intCount As Integer

strBuffer = String$(mconMAXLEN - 1, 0)
intCount = apiGetClassName(hwnd, strBuffer, mconMAXLEN)
If intCount > 0 Then
fGetClassName = Left$(strBuffer, intCount)
End If

End Function
Phil.
I'm still trying to muddle through this one.
Did you have the code for fGetCaption() ?

--
Bob Darlington
Brisbane

Reply With Quote
  #6  
Old   
Phil
 
Posts: n/a

Default Re: Opening XL and Word from Access 2010 - 11-14-2011 , 12:01 PM



Quote:
Phil.
I'm still trying to muddle through this one.
Did you have the code for fGetCaption() ?

Hi Bob

Private Declare Function apiGetWindowText Lib "user32" Alias _
"GetWindowTextA" (ByVal hwnd As Long, ByVal _
lpString As String, ByVal aint As Long) As Long


Private Function fGetCaption(hwnd As Long) As String
Dim strBuffer As String
Dim intCount As Integer

strBuffer = String$(mconMAXLEN - 1, 0)
intCount = apiGetWindowText(hwnd, strBuffer, mconMAXLEN)
If intCount > 0 Then
fGetCaption = Left$(strBuffer, intCount)
End If

End Function

Phil

Reply With Quote
  #7  
Old   
Bob Darlington
 
Posts: n/a

Default Re: Opening XL and Word from Access 2010 - 11-14-2011 , 07:09 PM



Thanks Phil

--
Bob Darlington
Brisbane
"Phil" <phil (AT) stantonfamily (DOT) co.uk> wrote

Quote:

Phil.
I'm still trying to muddle through this one.
Did you have the code for fGetCaption() ?


Hi Bob

Private Declare Function apiGetWindowText Lib "user32" Alias _
"GetWindowTextA" (ByVal hwnd As Long, ByVal _
lpString As String, ByVal aint As Long) As Long


Private Function fGetCaption(hwnd As Long) As String
Dim strBuffer As String
Dim intCount As Integer

strBuffer = String$(mconMAXLEN - 1, 0)
intCount = apiGetWindowText(hwnd, strBuffer, mconMAXLEN)
If intCount > 0 Then
fGetCaption = Left$(strBuffer, intCount)
End If

End Function

Phil

Reply With Quote
  #8  
Old   
Bob Darlington
 
Posts: n/a

Default Re: Opening XL and Word from Access 2010 - 12-11-2011 , 04:46 PM



On 11/11/2011 5:22 PM, Bob Darlington wrote:
Quote:
I've got (among other things) an Excel and a Word button on a ribbon bar.
They each call the following code which works as intended, but Excel opens
on top of my application (which is Maximised), while the Word document opens
on the task bar only.
How can I get them both to open the same way?

Dim objOffice As Object
Dim strApp As String

Select Case vAppType
Case 1:
strApp = "Excel"
Case 2:
strApp = "Word"
End Select

If Not IsAppRunning(strApp) Then
Set objOffice = CreateObject(strApp& ".Application")
Else
Set objOffice = GetObject(, strApp& ".Application")
End If

objOffice.Application.Visible = True
Select Case vAppType
Case 1:
objOffice.Workbooks.Add
Case 2:
objOffice.Documents.Add
objOffice.Documents(1).Activate
objOffice.Application.WindowState = 0 ' wdWindowStateNormal
End Select

For anyone interesed, the following does the trick:

objOffice.Application.Visible = True

Select Case vAppType
Case 1:
objOffice.Workbooks.Add
objOffice.WindowState = -4143 'xlNormal
Case 2:
objOffice.Documents.Add
objOffice.WindowState = 0 'wdWindowStateNormal
objOffice.Activate
objOffice.Documents(1).Activate
End Select

I put the .Visible above the 'Select Case' and added objOffice.Activate.
Also used the XL value for xlNormal. I suppose there is a reason why XL
and Word use different values when they are part of the same Office suite.

--
Bob Darlington
Brisbane

Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.3
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.