-
November 1st, 2002, 10:46 AM
#1
Launching applications
I am new to the VB program. I have had a couple of classes in it and know just enough to be frightened. I am currently in college, Heald college, and am preparing to graduate. My grad project is a VB program that I must write. I have laid my form,and revised it a couple of times. What I am trying to do is with the use of an option button take information from a label field on the click of a button and open/launch another application, powerpoint to be exact. Problem is I can't figure out the code to do this. Can somebody please point me in the right direction.
Last edited by BigDawg; November 1st, 2002 at 10:54 AM.
-
November 1st, 2002, 11:08 AM
#2
You can use SHELL() vb functin, or SHELLEXECUTE() API. Both of them will work. ShellExecute will open, for example, a powerpoint file. To open Powerpoint, you must know the EXE path:
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
Private Sub Command1_Click()
ShellExecute Me.hwnd, "open", "C:\Program Files\Microsoft Office\Office\POWERPNT.EXE", "", "", 0
End Sub
Private Sub Command2_Click()
Shell "C:\Program Files\Microsoft Office\Office\POWERPNT.EXE", vbNormalFocus
End Sub
If you want to open a powerpoint file, use the ShellExecute and change the file EXE for the file you want to open. Windows will open the file with the associate program, if .xls with Excel, powerpoint type with powerpoint
Shell only open EXE, but with most of them, you can past the filename to open in parameters. You can search the forum for more info
JeffB
-
November 1st, 2002, 11:30 AM
#3
Use the Excel.Aplication Object
All the applications as such are exposed outside as OLE objects.
Open up the Projects\refrences and check on Microsoft Excel object Library
On the button click event..
Dim obj as Excel. Application
set obj = new Excel.Applcation
and using obj you can do many things. and from there even you can raise any of the office products.
have fun..
sam
-
November 1st, 2002, 11:37 AM
#4
May be this code will help. you can do a lot of things with this excel object and the similar way you can also raise any of the application.
Ex:
Here is a form with two buttons and i do raise and close the excel application with this.
Option Explicit
Dim obj As Excel.Application
Private Sub Command1_Click()
obj.Quit
End Sub
Private Sub Command2_Click()
obj.Visible = True
End Sub
Private Sub Form_Load()
Set obj = New Excel.Application
End Sub
The best thing about this is you can use the same to open up any of the .xls files.
but the earlier shell code is also good to do that but its very limited.
have fun
Sam
-
November 4th, 2002, 04:02 PM
#5
Want to open presentation
The code worked so that I am able to launch the PowerPoint application. Now I am trying to get it to launch specific presentations based on user selection of a option button and a command button. I have tried several ideas but no luck so far.
-
November 4th, 2002, 04:22 PM
#6
If the powerpoint presentation are saved with the powerpoint extension (.pwp?), then use ShellExecute directly on these files, powerpoint will be open since it is associated with these files.
Code:
ShellExecute Me.hwnd, "open", "C:\TestPresentation.pwp", "", "", 0
Also, you can open the EXE and pass it the filename you with to open.
Code:
ShellExecute Me.hwnd, "open", "C:\Program Files\Microsoft Office\Office\POWERPNT.EXE C:\TestPresentation.pwp", "", "", 0
That last line might not work, I'm not sure it's the proper synthax
Since you seem focused on PowerPoint, you can use PowerPoint objects, take a look at this:
Code:
Private Sub Command1_Click()
'Add a reference to Microsoft PowerPoint object library
Dim objPowerPoint As New PowerPoint.Application
objPowerPoint.Visible = True
Set objPowerPoint = Nothing
End Sub
You may want to use the Object browser to find the good way to open a document, this last will only open a NEW instance of powerpoint
JeffB
-
November 4th, 2002, 06:52 PM
#7
Blows up
The whole thing is confusing
my syntax is:
Private Sub cmdOpen_Click()
If optDate(0).Value = True Then
Dim obj As PowerPoint.Application
Set obj = New PowerPoint.Application
obj.Visible = msoTrue
ShellExecute Me.hwnd, "open", "c:\program files\microsoft office\office\powerpnt.exe D:\school\oldsongs\ & strDate1 & .ppt", "", 0
I launch my form and select my option button, I have the strDate1 variable hard coded for now, then I click on my command button and the program blows up.
I get an Illegal Operation message and VB gets shutdown. It opens the powerpoint application but doesn't open the specific file. I am trying to use the variable, strDate1, as the file name that follows my current naming conventions for the ppt files. When I leave out the ShellExecute string I get the application to launch but not the file. I am close on this just need a little more to get it right.
BD
-
November 5th, 2002, 09:40 AM
#8
Those are 2 separate method ShellExecute and Micorosoft PowerPoint object. Here is an example of using BOTH of them:
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
Private Sub Command1_Click()
'This is the first method, using MICROSOFT OBJECT
'This create a new instance of PowerPoint and SHOW it
Dim objPowerPoint As New PowerPoint.Application
objPowerPoint.Visible = True
'This open a powerpoint file in our new PowerPoint
'instance, we keep the reference to the opened object
Dim objPresentation As PowerPoint.Presentation
Set objPresentation = objPowerPoint.Presentations.Open("C:\TEST.ppt")
'This is an example of changing a property of the
'opened document, with these object, you can
'do what you want with the document (If you know
'the good method)
'This is change the layout direction
objPresentation.LayoutDirection = ppDirectionLeftToRight
'Save the document
objPresentation.Save
'Destroy the reference
Set objPresentation = Nothing
Set objPowerPoint = Nothing
End Sub
Private Sub Command2_Click()
'Another method, if you want to open
'the file with powerpoint but DON'T CARE at all about
'what the user do with it
'This let the document be opened with powepoint
ShellExecute Me.hwnd, "open", "c:\test.ppt", "", "", 1
End Sub
Both of them are VALID way to open a powerpoint document, the difference reside in what do you want to do with the document. Read the comments in the code, you'll see that the Microsoft Objects allow you to interact with PowerPoint, the ShellExecute method do not.
I suggest you read a book or find some articles on the NET about Microsoft Office Family objects (POWERPOINT, EXCEL, WORD), they all works the same. SHELLEXECUTE is an API that open just any file using the system file association. Both of them can be used depending of what is your need, but do not mix them together
If it is not clear, don't bother to reply
JeffB
-
November 5th, 2002, 02:27 PM
#9
Works now
JeffB,
Thank you that was the information I needed. I don't appreciate the don't bother thing since my understanding of this forum is that it is a place to be able to ask questions. I realize that my lack of knowledge is not helpful, but I am trying and once pointed in the right direction can move on. I had already tried the MSDN but found that it wasn't addressing this problem clearly enough. Regardless I was able to get the code to work and now the rest is polishing. Thanks for you assistance.
BD
-
November 5th, 2002, 04:08 PM
#10
I'm sorry, I think I did not well express myself. What I mean by "don't bother" is that it would make me a pleasure to help you if my answer is not clear.
I'm sincerely sorry for the confusion, if you have any other problem, be at ease to ask it here.
Have a nice day
JeffB
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|