Inserting Picture from File using VBA

  • Thread starter Thread starter Roderick O'Regan
  • Start date Start date
R

Roderick O'Regan

I am able to write the appropriate code to insert a graph in the
presentation slide but seem unable to follow the same principles when
attempting to write code that will do what I can achieve manually i.e. the
VBA equivalent of Insert|Picture|From File... followed by the Insert Picture
dialog opening.

Could someone nudge me in the right direction, please?

Regards

Roderick O'Regan
 
This is quick and ugly, but you only wanted nudged in the right direction,
not taken there, right?

Sub Macro1()

ActiveWindow.Selection.SlideRange _
Shapes.AddPicture(FileName:= _
"C:\My Documents\My Pictures\Picture.jpg", _
LinkToFile:=msoFalse, SaveWithDocument:=msoTrue, _
Left:=206, Top:=73, Width:=309, Height:=395).Select

End Sub


This will, when run in edit mode, add the picture "Picture.jpg" to your
slide.
--
Bill Dilworth, Microsoft PPT MVP
===============
Please spend a few minutes checking vestprog2@
out www.pptfaq.com This link will yahoo.
answer most of our questions, before com
you think to ask them.

Change org to com to defuse anti-spam,
ant-virus, anti-nuisance misdirection.
..
..
 
Thanks Bill. That was a pretty good shove! With what I already had taken
from VBA Help coupled to your suggestion and a bit of pushing and shoving
the code about I've created the following:

Sub Main()
'Declare a variable as a FileDialog object.
Dim fd As FileDialog
'Create a FileDialog object as a File Picker dialog box.
Set fd = Application.FileDialog(msoFileDialogFilePicker)
'Declare a variable to contain the path
Dim vrtSelectedItem As Variant
Dim FileName As String
'Use a With...End With block to reference the FileDialog object.
With fd
'Set the tile of the dialog box as well as the action button
.InitialFileName = "C:\"
.Title = "Insert Picture"
.ButtonName = "Insert"
.InitialView = msoFileDialogViewDetails
'Add a filter that includes GIF and JPEG images and make it the
first item in the list.
.Filters.Add "Images", "*.gif; *.jpg; *.jpeg,*.png,
*.eps,*.tif,*.tiff", 1
'Use the Show method to display the File Picker dialog box and
return the user's action.
'The user pressed the action button.
If .Show = -1 Then
'Step through each string in the FileDialogSelectedItems
collection.
For Each vrtSelectedItem In .SelectedItems
'vrtSelectedItem is a String that contains the path of each
selected item.

ActiveWindow.Selection.SlideRange.Shapes.AddPicture(vrtSelectedItem,
msoFalse, msoCTrue, 206, 206).Select
Next vrtSelectedItem
'The user pressed Cancel.
Else
End If
End With
'Set the object variable to Nothing.
Set fd = Nothing
End Sub

Works a treat!
 
[CRITICAL UPDATE - Anyone using Office 2003 should install the critical
update as soon as possible. From PowerPoint, choose "Help -> Check for
Updates".]

Hello,

Although this can be done through VBA, perhaps you might want some future
version of PowerPoint to provide a solution or feature which accomplishes
the same task, without having to resort to VBA or add-ins?

If you (or anyone else reading this message) think that it's important that
PowerPoint provide this kind of functionality, don't forget to send your
feedback (in YOUR OWN WORDS, please) to Microsoft at:

http://register.microsoft.com/mswish/suggestion.asp

As with all product suggestions, it's important that you not just state
your wish but also WHY it is important to you that your product suggestion
be implemented by Microsoft. Microsoft receives thousands of product
suggestions every day and we read each one but, in any given product
development cycle, there are only sufficient resources to address the ones
that are most important to our customers so take the extra time to state
your case as clearly and completely as possible.

IMPORTANT: Each submission should be a single suggestion (not a list of
suggestions).

John Langhans
Microsoft Corporation
Supportability Program Manager
Microsoft Office PowerPoint for Windows
Microsoft Office Picture Manager for Windows

For FAQ's, highlights and top issues, visit the Microsoft PowerPoint
support center at: http://support.microsoft.com/default.aspx?pr=ppt
Search the Microsoft Knowledge Base at:
http://support.microsoft.com/default.aspx?pr=kbhowto

This posting is provided "AS IS" with no warranties, and confers no rights.
Use of any included script samples are subject to the terms specified at
http://www.microsoft.com/info/cpyright.htm
 
Back
Top