Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Word opens minimized

Status
Not open for further replies.

TysonLPrice

Programmer
Jan 8, 2003
859
US
An application opens word with the code I'll post below. The first time it opens Word 2003 it is on top of the current form. After closing and opening subsequent documents they all stay behind the current form. This only happened on a few machines. I've made sure the same Word software is running, same MDACs, Word options, etc. I'm basically stuck.

The application runs through some logic to take the extension and find out what to use to open the file. Then it uses the following code to launch it. Word pops up right at the code under the comment ' Start the shelled application:



Code:
Private Sub ExecCmd(cmdline$, proc As PROCESS_INFORMATION)
   'Dim proc As PROCESS_INFORMATION
   Dim start As STARTUPINFO
   Dim Ret&
   Dim bMissingProc As Boolean
   Dim lWaittime As Long
   
   bMissingProc = False
   lWaittime = 1   ' 1 milliseconds
   
   DoEvents
   ' Initialize the STARTUPINFO structure:
   start.cb = Len(start)

   ' Start the shelled application:
   Ret& = CreateProcessA(0&, cmdline$, 0&, 0&, 1&, _
      NORMAL_PRIORITY_CLASS, 0&, 0&, start, proc)

   ' Wait for the shelled application to finish:
   Ret& = WaitForSingleObject(proc.hProcess, lWaittime)
   proc.lReturn = Ret&
   
   If proc.lReturn = 0 Then
      Ret& = CloseHandle(proc.hProcess)
   End If
   
end sub

The contents cmdline& in the bolded code is:

C:\Program Files\Microsoft Office\OFFICE11\WINWORD.EXE "C:\DOCUME~1\pricet\LOCALS~1\Temp\tprice\Attachments\SI HOSPITAL INFO REQUEST.DOC - 20080520 065039.DOC"

The key to me is it is only behaving that way on certain machine. What could be different to make it come up on top of the form the first time and behind it after that?
 
Is there any special reason to avoid the ShellExecute API as in;

Public Declare Function ShellExecute Lib "shell32.dll" Alias "ShellExecuteA" (ByVal hWnd As Long, ByVal lpOperation As String, ByVal lpFile As String, ByVal lpParameters As String, ByVal lpDirectory As String, ByVal nShowCmd As Long) As Long

'called with something like
ShellExecute myform.hWnd, vbNullString, Path2MyDoc$, vbNullString, vbNullString, vbNormalFocus

...because that allows you to control the focus situation in its last argument.
 
Thanks for the suggestion. Right now I'm not looking to change the code. Out of a hundred users or so only a few a experiencing the problem. I'm thinking it is some type of setting on their machines. We just switched over to Word 2003 and I've been focusing on that. I was hoping someone has experienced a similar issue.

I'll keep our post im mind though.
 
Is there a chance that Word was closed (say by Task Manager) while minimized?
 
avanderlaan,

Not from what I've seen. My project leader wants me to hook Word 2003 after it is brought up and maximize the window. Any idea how to do that? I'm searching for an API but maybe you or someone else knows how.

Although it's clunky we are trying to avoid changing our program "AttachmentLauncher" which calls Word because it is complex and used all over the place (it basically looks at file extentions and decides what program to use).

 
I think you may want to reconsider HughLerwill's suggestion and look into ShellExecute.

Swi
 
>it basically looks at file extentions and decides what program to use

There are some really easy ways of doing that. I am sure, for example, that johnwm will be along any minute with his one-liner solution ...
 
Here. Same principle for any Office app...

This opens excel without showing it.

Code:
Option Explicit

Public xlAppTemp As Excel.Application
Public xlWorkBook As Excel.Workbook
Public xlSheet As Excel.Worksheet
Dim strDate$

Public Sub GenerateReport()
  On Error GoTo ErrHandler
  
  ' Creating Object for Excel File.....
  Set xlAppTemp = New Excel.Application
  
  ' Making it Invisible and non-Interactive.....
  xlAppTemp.Visible = False
  xlAppTemp.DisplayAlerts = False
    ' Opening Template Excel File.....
  Set xlWorkBook = xlAppTemp.Workbooks.Open(App.Path & "\Book1.xls", , False)
  Set xlSheet = xlWorkBook.Sheets(1)

  ' Making Active to Worksheet 1.....
  xlSheet.Activate

  ' I am doing lot of things in it, but to provide you with example
  xlSheet.Cells(15, 1) = "This is my report 1"

  ' Formating Date to attach with new file name.....
  strDate = Format(Date, "yyyy-mm-dd")
  
  ' Saving excel file with new name on different folder.....
  xlWorkBook.SaveAs App.Path & "\Output" & strDate & ".xls"

Cleanup:
  ' Destroying Objects.....
  Set xlSheet = Nothing
  xlWorkBook.Close SaveChanges:=False
  Set xlWorkBook = Nothing
'The Visible and DisplayAlerts settings should be reset, as they can affect 'manual' use of Excel too.
  xlAppTemp.Visible = True
  xlAppTemp.DisplayAlerts = True
  xlAppTemp.Quit
  Set xlAppTemp = Nothing
  Exit Sub
ErrHandler:
'I presume this section comes after ErrHandler, in which case you will want to close the workbook without changes.
'(save happens just above if no error occurs)
  xlWorkBook.Close SaveChanges:=False
  Set xlWorkBook = Nothing

'The Visible and DisplayAlerts settings should be reset, as they can affect 'manual' use of Excel too.
  xlAppTemp.Visible = True
  xlAppTemp.DisplayAlerts = True

  xlAppTemp.Quit
  Set xlAppTemp = Nothing
End Sub

Private Sub Command1_Click()
  Call GenerateReport
  Beep
End Sub

-David
2006, 2007 & 2008 Microsoft Most Valuable Professional (VB)
2006 Dell Certified System Professional (CSP)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top