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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Startign other programs from Visual Basic 1

Status
Not open for further replies.

simdan42

Programmer
Jul 17, 2002
118
US
Is there a way from VB code to open an excel or word file. I would like to have a method that would take the filename and path and an arguement and open it using word or excel.
 
You can shell it out if you are just wanting to open the file for operations with the native application or you can use OLE and DDE by setting a reference to the application you want to use (Tools References) if you are wanting to use the native application to processes commands on the file in question.
 
Not familiar with the shell command? Can you give an example?
 
You can do this using the API ShellExecute

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


Private Sub FormLoad()

    ShellExecute Me.Hwnd, "open" "file path", "","", 1

End Sub
 

Create a new project and a textfile ("test.txt") and put it in the root directory ("C:\") i.e. ("C:\test.txt") and put the follow code in the click event of a command button. Run and click.


Shell "C:\test.txt", vbMaximizedFocus
 
If you want to use strictly Shell with no API you would have to do this:

Code:
Shell "C:\Windows\notepad.exe c:\test.txt", vbMaximizedFocus

For Shell, you must know the path of the exe that file is associated with. Shell only works with executable type files, ex. *.exe and *.bat. With ShellExecute, you don't need that info and it will work with any type of files that have an association in the registry, ex. *.txt, *.doc, *.xls

 
Can up pass to the program you are opening parameter that could tell excel how to open the file and perform formating on it? Like open as comma delimited for example.
 

With Shell, ShellExecute, and ShellExecuteEx the answer is no.

However using OLE/DDE you can do this programmatically but you will have to do all the work.
 
Is it possible to from the command line to open the excel file with parameters?
 
I believe that with Shell and ShellExecute you can pass parameters to the program, such as in the following example

dim cmdline as string
cmdline = "c:\windows\notepad c:\autoexec.bat"
Shell cmdline, vbNormalFocus

the program parameters must be all included in the first parameter to the shell function.
Good Luck
--------------
As a circle of light increases so does the circumference of darkness around it. - Albert Einstein
 
No, no, For example Click Start select run

I want to be able to type in the open filed: spreadsheet1.xls -s -t -w

the -s -t -w are examples but I would like to know if this is possible to tell excel what to do with the file when opening for example open it a text delimited by a comma.
 
Is there a way to close another program by using VB? If so, is there any way that a timer can be placed so that it would close at a certain time or after a certain number of hours? Thanks.
 
This is direct from the Visual Basic help in MSExcel (To access this help file start excel, activate the VB environment, Press F1, click on tab answer wizard, type in command line. The responce you are looking for is command function(below)). Please note the last sentance in in the first paragraph.

----

Command Function


Returns the argument portion of the command line used to launch Microsoft Visual Basic or an executable program developed with Visual Basic. The Visual Basic Command function is not available in Microsoft Office applications.

Syntax

Command

Remarks

When Visual Basic is launched from the command line, any portion of the command line that follows /cmd is passed to the program as the command-line argument. In the following example, cmdlineargs represents the argument information returned by the Command function.

VB /cmd cmdlineargs

For applications developed with Visual Basic and compiled to an .exe file, Command returns any arguments that appear after the name of the application on the command line. For example:

MyApp cmdlineargs

To find how command line arguments can be changed in the user interface of the application you're using, search Help for "command line arguments

----

What this means is you cannot build an on open macro that will process the commands that you want. On the other hand most applications will accept certain commands or action verbs (i.e. open and print). You should be able to find out more within the msexcel visual basic help file or at microsoft.
 


autoaccess you need to start your own thread and to answer your question YES
 
Using the intrinsic Shell function, you can do the following: Note the use of short names.

cmdline = "c:\progra~1\micros~2\office\excel /r /e c:\dir1\spreadshee1.xls"
Shell cmdline, vbNormalFocus
Good Luck
--------------
As a circle of light increases so does the circumference of darkness around it. - Albert Einstein
 
try this
add a reference to excel and word through
project|references

Sub openExcelWord(Doc, DocType)
If DocType = "WORD" Then
Dim WD As New Word.Application
Set WD = CreateObject("word.Application")
WD.Application.Visible = True
WD.Application.Documents.Open Doc
Set WD = Nothing

Else

Dim xl As New Excel.Application
Set xl = CreateObject("Excel.Application")
xl.Application.Visible = True
xl.Application.Workbooks.Open Sheet
Set xl = Nothing
End If

End Sub
Private Sub Command1_Click()
openExcelWord "C:\resume.doc", "WORD"
End Sub
 
Ok, CajunCenturion , do you know where I can get a list of the options like /r, /e so I know what they do?
 
The command line parameters will vary based on the application being invoked. For Excel as in the example, I only know of three, those being (I think)

/r - open spreadsheet read only,
/e - don't show opening screen
/p <dir pathname> - change the default working directory

Good Luck
--------------
As a circle of light increases so does the circumference of darkness around it. - Albert Einstein
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top