We help IT Professionals succeed at work.

Shell command in VB6

soft4u
soft4u asked
on
I use the shell command to start for instance winword.exe.
How can I check if word is running the next time i use the shell command and activate it and load my new .doc file. Now I run: Shell(RunCom, vbNormalFocus).

I need code please.
Comment
Watch Question

Commented:
Soft here is one way to do it:
Public Declare Function GetExitCodeProcess Lib "kernel32" (ByVal hProcess As Long, lpexitcode As Long) As Long

Public Declare Function OpenProcess Lib "kernel32" (ByVal dwDesiredaccess&, ByVal bInherithandle&, ByVal dwProcessid&) As Long

public sub StartWinWord()

dim status as long
dim hProcess as long

        status = Shell(RunCom,vbNormalFocus)
       
        hProcess = OpenProcess(PROCESS_QUERY_INFORMATION,False, status)
       
        Do
            GetExitCodeProcess hProcess, lExit
            DoEvents
        Loop While lExit = STILL_ACTIVE

End sub

This will work for 32 bit applications.  Hope it helps.
Ryan ChongSoftware Tead Lead / Business Analyst / System Analyst / Data Engineer
CERTIFIED EXPERT

Commented:
Richie_SimonettiIT Operations
CERTIFIED EXPERT

Commented:
You could use Word objects instead shell function. You could get more control over what is going on with your instance and documents loaded and many, many things more.... even know when word is closed!

Commented:

Commented:
Commented:
I don't want to be a wet towel here but, why don't you use the ShellExecute API function.  Here is an example.

<------General Declarations------>
Private 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
<----------End General Declarations----------->
<----------Event---------------->
Private Sub Your_Event
Dim Handle As Long
Dim operation As String
Dim lpFile As String
Dim lpParm As String
Dim lpDir As String
Dim nShowcmd As Long

Handle = Me.hWnd      'Handle calling application
operation = "open"    'Operation performed
lpFile = "C:\SomeWordDoc.doc"     'Name and path of the file to be opened less drive letter
lpParm = ""           'Set to null string in VB
lpDir = "X:\"         '3 character drive ("C:\")
nShowcmd = 1          'Show application window (Hidden, Max, etc.)

ShellExecute Handle, operation, lpFile, lpParm, lpDir, nShowcmd
End Sub
<---------------End Event Code------------>

This code will activate word if it is open and display the new word document specified in the lpFile param.  If word is not open, it will open it.  Very simple to use.

HTH

Commented:
Glen RichmondSenior Software Developer.
CERTIFIED EXPERT

Commented:
JUST USE THIS NO NEED TO CHANGE ALL YOUR EXCISTING CODE THIS IS FROM MSDN LIBARY, PLEASE TAKE NOTE OF THE REMARKS SECTION.

GOOD LUCK

GLEN


'Begin-----------------------------------------

AppActivate Statement
     

Activates an application window.

Syntax

AppActivate title[, wait]

The AppActivate statement syntax has thesenamed arguments:

Part Description
title Required.String expression specifying the title in the title bar of the application window you want to activate. The task ID returned by the Shell function can be used in place of title to activate an application.
wait Optional.Boolean value specifying whether the calling application has the focus before activating another. If False (default), the specified application is immediately activated, even if the calling application does not have the focus. If True, the calling application waits until it has the focus, then activates the specified application.


Remarks

The AppActivate statement changes the focus to the named application or window but does not affect whether it is maximized or minimized. Focus moves from the activated application window when the user takes some action to change the focus or close the window. Use the Shell function to start an application and set the window style.

In determining which application to activate, title is compared to the title string of each running application. If there is no exact match, any application whose title string begins with title is activated. If there is more than one instance of the application named by title, one instance is arbitrarily activated.

'end---------------------------------
CERTIFIED EXPERT

Commented:
CERTIFIED EXPERT

Commented:
Hi soft4u,
Here is some code that I use  with Word inside my VB projects. Its a small sample to work with....

'Word Objects
Private MyWord  As Word.Application
Private MyDoc   As Word.Document
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long

Make a sub procedure... with this in it
Dim sDataPath as string     '<---- Here is your var. for doc files

Call GetWord
     
Set MyDoc = MyWord.Documents.Open(FileName:=sDataPath & sTempDoc, Format:=wdOpenFormatAuto)
     
With MyWord
 .ActiveWindow.View.Type = wdPageView
 .ActiveDocument.PageSetup.Orientation = dOrientLandscape
 .ActiveDocument.PageSetup.LeftMargin =   MyWord.InchesToPoints(0.5)
 .ActiveDocument.PageSetup.RightMargin = MyWord.InchesToPoints(0.5)
 .ActiveDocument.PageSetup.TopMargin = MyWord.InchesToPoints(0.5)
 .ActiveDocument.PageSetup.BottomMargin = MyWord.InchesToPoints(0.5)
 .ActiveDocument.SaveAs sDataPath & sDir, FileFormat:=wdFormatDocument
 .Visible = False
End With
MyDoc.Close
     
If Not MyWord Is Nothing Then MyWord.Quit wdSaveChanges, wdFormatDocument
         
Set MyWord = Nothing
End sub

Public Sub GetWord()
   
    DetectWord
   
    On Error Resume Next
    'This will create a new Word App
    Set MyWord = CreateObject("WORD.APPLICATION", "")
   
    If Err = 429 Then
        Set MyWord = GetObject("", "WORD.APPLICATION")
    End If
    If Err.Number = 462 Then
       Set MyWord = CreateObject("WORD.APPLICATION", "")
    End If
    Err.Clear   ' Clear Err object in case error occurred.

    Set MyDoc = Nothing
End Sub
Public Sub DetectWord()
  Const WM_CLOSE = &HF060&
  Const WM_SYSCOMMAND = &H112
  Dim hwnd As Long

   hwnd = FindWindow("OpusApp", "Microsoft Word")
  'if Word is not running this API call returns its handle hwnd = 0
   If hwnd = 0 Then
      Exit Sub
   Else
      'if Word is running this API call will close the App when hwnd > 0
      SendMessage hwnd, WM_SYSCOMMAND, WM_CLOSE, 0
   End If
End Sub

'This Will get you going of you don't want to use  the Shell Command.

Explore More ContentExplore courses, solutions, and other research materials related to this topic.