open an external program

Tiger99

New Member
Joined
Sep 2, 2002
Messages
28
Hi
I am just wondering is it possible to open an external program ( ie Microsoft word) in the background with excel VBA?

Thanks for any help
 

Excel Facts

Enter current date or time
Ctrl+: enters current time. Ctrl+; enters current date. Use Ctrl+: Ctrl+; Enter for current date & time.

TommyGun

MrExcel MVP
Joined
Dec 9, 2002
Messages
4,202
Sure...just depends on what you want to do with it. This code will open Word, create a new document, drop some text, and print it, without the user knowing.

<font face=Courier New><SPAN style="color:#00007F">Sub</SPAN> OpenWord_Shhhhhh()
    <SPAN style="color:#00007F">Dim</SPAN> WordApp <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Object</SPAN>
    <SPAN style="color:#00007F">Dim</SPAN> WordDoc <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Object</SPAN>
    
    <SPAN style="color:#00007F">On</SPAN> <SPAN style="color:#00007F">Error</SPAN> <SPAN style="color:#00007F">Resume</SPAN> <SPAN style="color:#00007F">Next</SPAN>
    <SPAN style="color:#00007F">Set</SPAN> WordApp = CreateObject("Word.Application")
    <SPAN style="color:#00007F">If</SPAN> WordApp <SPAN style="color:#00007F">Is</SPAN> <SPAN style="color:#00007F">Nothing</SPAN> <SPAN style="color:#00007F">Then</SPAN>
        MsgBox "It didn<SPAN style="color:#007F00">'t work.", vbOKOnly + vbCritical, "Oops!"</SPAN>
        <SPAN style="color:#00007F">Exit</SPAN> <SPAN style="color:#00007F">Sub</SPAN>
    <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">If</SPAN>
    <SPAN style="color:#00007F">On</SPAN> <SPAN style="color:#00007F">Error</SPAN> <SPAN style="color:#00007F">GoTo</SPAN> 0
    
    <SPAN style="color:#00007F">Set</SPAN> WordDoc = WordApp.documents.Add
    
    <SPAN style="color:#00007F">With</SPAN> WordDoc
        .Range.Text = "This is a new Word document."
        .PrintOut <SPAN style="color:#00007F">False</SPAN>
        .<SPAN style="color:#00007F">Close</SPAN> <SPAN style="color:#00007F">False</SPAN>
    <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">With</SPAN>
    
    <SPAN style="color:#00007F">Set</SPAN> WordDoc = <SPAN style="color:#00007F">Nothing</SPAN>
    
    WordApp.Quit
    <SPAN style="color:#00007F">Set</SPAN> WordApp = <SPAN style="color:#00007F">Nothing</SPAN>
<SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">Sub</SPAN></FONT>
 

zilpher

Active Member
Joined
Mar 27, 2003
Messages
442
Sure, here's the method I use:

Code:
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

Sub g()
'open Word with a blank doc
ShellExecute Application.hwnd, vbNullString, "C:\Program Files\Microsoft Office\OFFICE11\winword.exe", vbNullString, "C:\", 1
'open a document
ShellExecute Application.hwnd, vbNullString, "C:\data\FAX.doc", vbNullString, "C:\", 1

End Sub

You'll need to change the paths accordingly.

HTH
 
Learn Excel from Bill Jelen

Understanding data is crucial, and the easiest place to start is with Microsoft Excel.

Forum statistics

Threads
1,151,560
Messages
5,765,095
Members
425,258
Latest member
brentmitchell

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Top