Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Open file with VB

442 views
Skip to first unread message

erin

unread,
Aug 29, 2002, 6:49:12 PM8/29/02
to
Hi all,

I'm trying to display the Open file dialog box in
powerpoint similar to this Excel example.

Application.Dialogs(xlDialogOpen).Show

The goal is to allow the user to choose a file to open.
How is this accomplished in VBA for PowerPoint? I've
never used VBA for PowerPoint before, and I'm having
trouble making the transition from Excel. Any help would
be appreciated. Thanks.

Erin

Brian Reilly, MS MVP

unread,
Aug 30, 2002, 7:38:32 AM8/30/02
to
Erin,

You can do this a coupla ways, here's one that uses an API call to
pause the code till a variable gets reset after the file is chosen.
It's for inserting a picture but you could just as easily modify it to
call the File open menu with a change in this line
CommandBars("Insert").Controls("Picture").Controls("From
File...").Execute to
CommandBars("Open . . .").Execute

Option Explicit

Public Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)

''' Comments: This routine uses an API call to Sleep while the
dialog is open.
''' Otherwise the code would continue running before the
user got to insert the picture.
''' Also captures the Cancel button by counting objects
after the dialog closes.
''' Also runs a counter after the dialog closes to capture
the Cancel click which cannot be captured normally.
''' Date Developer Action
''' 2/10/02 Brian Reilly Created

Sub Insert_Picture_on_Title_Dialog()

Dim strViewType As String
Dim lShapeCount As Long
Dim i As Integer, j As Integer
Dim iTimeCounter As Integer
On Error GoTo errorhandler

strViewType = ActiveWindow.ViewType

'Check for TitleMaster first
If ActivePresentation.HasTitleMaster = False Then
MsgBox "This presentation does not have a TitleMaster. Exiting
. . ."
Exit Sub
Else
'Continue
End If

lShapeCount = ActivePresentation.TitleMaster.Shapes.Count

'Just in the eventuality that you click the start button twice
'isRunning stores the current state of the macro
'TRUE = Running; FALSE = Idle

Static isRunning As Boolean
If isRunning = True Then
End
Else
isRunning = True

With ActivePresentation
ActiveWindow.ViewType = ppViewTitleMaster
With .TitleMaster
For i = 1 To .Shapes.Count
For j = 1 To .Shapes(i).Tags.Count
If .Shapes(i).Tags.Name(j) =
"TITLEMASTERPICTURE" Then
.Shapes(i).Delete
'Now reset the shape counter
lShapeCount =
ActivePresentation.TitleMaster.Shapes.Count
End If
Next j
Next i
End With
End With
CommandBars("Insert").Controls("Picture").Controls("From
File...").Execute

Do While (ActivePresentation.TitleMaster.Shapes.Count =
lShapeCount)
' Suspend program execution for 1/5 second (500
milliseconds)
iTimeCounter = iTimeCounter + 1
Sleep 500
If iTimeCounter > 2 Then
ActiveWindow.ViewType = strViewType
'The user canceled the dialog
Exit Do
ElseIf ActivePresentation.TitleMaster.Shapes.Count <>
lShapeCount Then
Exit Do
Else
End If '
' Very crucial else the display won't refresh itself
DoEvents
Loop

isRunning = False
'Now capture the Cancel button click and exit
If ActivePresentation.TitleMaster.Shapes.Count = lShapeCount
Then
Exit Sub
Else
'continue
End If
With ActivePresentation.TitleMaster.Shapes(lShapeCount + 1)
.Tags.Add "TitleMasterPicture", "Present"
'Next line out in case heights of pictures vary
'.LockAspectRatio = false
.Top = 0
.Left = 0
'Next line out in case heights of pictures vary
'.Height = 420
.Width = 720.66
.LockAspectRatio = True
End With
ActiveWindow.ViewType = strViewType
End
End If
Exit Sub
errorhandler:
MsgBox Error
End Sub

Sorry about the line breaks,
Brian Reilly, PowerPoint MVP

Erin

unread,
Aug 30, 2002, 10:28:17 AM8/30/02
to
Thanks for the advice Brian. Unfortunately executing the
Open... menu item is exactly what I am trying to avoid.
Our firm has a document management system and the Open...
menu item starts the open dialog box for that system. I
was hoping to directly access PowerPoint's Open dialog
box. Any more suggestions? Thanks.

Erin

>.. . ."

>.
>

Brian Reilly, MS MVP

unread,
Aug 30, 2002, 10:50:30 PM8/30/02
to
>Unfortunately executing the
>Open... menu item is exactly what I am trying to avoid.
>Our firm has a document management system and the Open...
>menu item starts the open dialog box for that system. I
>was hoping to directly access PowerPoint's Open dialog
>box.

Erin,
Huh? What specifically do you mean I lose you here.
Brian Reilly, PowerPoint MVP

Erin

unread,
Sep 3, 2002, 10:55:44 AM9/3/02
to
I'm beginning to get the idea that what I want to do is
not possible, but I'll try to explain myself anyway. The
way that our system is set up, if the user selects 'Open
an Existing Presentation' or clicks File, Open... another
application, a document managemet system (iManage), is
opened. I want to avoid this in my code because the file
in question will never be stored in this system, but
rather on the user's c:\ drive. Working through this
system will create more steps for the user so I would like
to by pass it entirely. In Excel this is possible with
Application.Dialogs(xlDialogOpen).Show
which specifically brings up Excel's File Open dialog box
as opposed to
CommandBars("File").Controls("Open...").Execute
which brings up the document management system as our
firm's MS applications are set up to do. I'm getting the
idea that there is no equivalent to
Application.Dialogs(xlDialogOpen).Show
in PPT. Is there any other way to allow the user to
select a presentation to open without referencing the
File, Open... menu item? Thanks.

>.
>

Brian Reilly, MS MVP

unread,
Sep 3, 2002, 11:41:33 AM9/3/02
to
Erin,
You could try an API call to get the file name and then try PPT's
Presentations.open method and see if this will skip the other
program's control. The equiv. of the XLDialogs method is only
introduced in PPT 2002.
Brian Reilly, PowerPoint MVP

Shyam Pillai

unread,
Sep 3, 2002, 10:34:20 PM9/3/02
to
Erin,

Like Brian says, file dialogs were not supported prior to PPT XP. However
if you wish to use some undocumented code for file dialogs in PPT 97/ 2000
check out the following page. These do not work in PowerPoint XP. In XP make
use of the supported FileDialog object.

How to display 'Open/Save As' dialogs in PowerPoint
http://www.mvps.org/skp/ppt00043.htm

Regards
Shyam Pillai

Handout Wizard
http://www.mvps.org/skp/how/


"Erin" <erin_s...@yahoo.com> wrote in message
news:a51901c25359$faaa3a50$37ef2ecf@TKMSFTNGXA13...

0 new messages