Solved

Division by zero error in VBA Office 2011 for Mac, AddPicture command

Posted on 2014-09-03
6
446 Views
Last Modified: 2014-09-10
This is a puzzle.  I've written a macro to let the user simply browse for and insert a picture from a pre-determined graphic library location.  Simple VBA code.  Works fine until I get to the very last line below, the AddPicture command.  It gives me a "Division by Zero" error, which I've never seen.  The weird thing is that I can go into debug mode, copy the same line that is generating an error into my immediate window and execute it there, and it inserts the picture just fine.  What else might be going on to cause the error?  This is in PowerPoint for Mac 2011.  Thanks for any suggestions.

Sub insertgraphic()

If Application.Presentations.Count = 0 Then
    MsgBox "A presentation must be open to use this command.", vbInformation
    Exit Sub
End If

Call makesureslideselected

Dim currentslide As Slide
Dim pickedgraphic As String
Dim pickedgraphicfolder As String
Dim storedgraphicfolder As String

Set currentslide = ActivePresentation.Slides(ActiveWindow.View.Slide.SlideIndex)

On Error Resume Next
    storedgraphicfolder = GetSetting("Microsoft PowerPoint", "HPCToolbar", "storedgraphicfolder", "")
    On Error GoTo 0
If storedgraphicfolder = "" Then storedgraphicfolder = "Macintosh HD:Library"

Dim sMacScript As String
             
sMacScript = "set pickedgraphic to (choose file with prompt " & """Select a file to import"" default location" _
& " alias """ & storedgraphicfolder & """) as string" & vbLf & "return pickedgraphic"

pickedgraphic = MacScript(sMacScript)

currentslide.Shapes.AddPicture fileName:=pickedgraphic, linktofile:=msoFalse, savewithdocument:=msoTrue, Left:=100, Top:=100

Open in new window

0
Comment
Question by:versatilebb
6 Comments
 
LVL 10

Expert Comment

by:HuaMinChen
ID: 40302775
Hi,
You need to locate the relevant place and then put one "if" statement there, to change 0 to 1.
0
 
LVL 48

Expert Comment

by:Rgonzo1971
ID: 40303070
Hi,

pls try

currentslide.Shapes.AddPicture fileName:=pickedgraphic, linktofile:=False, savewithdocument:=True, Left:=100, Top:=100

Regards
0
 

Author Comment

by:versatilebb
ID: 40305078
Rgonzo, I made the change you suggested but am still getting the same error.  Any other ideas?

HuaMinChen, can you be more specific?  What value do I need to change from 0 to 1?  I'm not using any numeric values in this code that I know of.

Thanks
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 23

Expert Comment

by:JSRWilson
ID: 40305256
I don't see any way the code you have could produce that error are you sure there isn't other code you haven't mentioned.

This is not the problem but you could replace this

 
   storedgraphicfolder = GetSetting("Microsoft PowerPoint", "HPCToolbar", "storedgraphicfolder", "")
    On Error GoTo 0
If storedgraphicfolder = "" Then storedgraphicfolder = "Macintosh HD:Library"

Open in new window


With

    storedgraphicfolder = GetSetting("Microsoft PowerPoint", "HPCToolbar", "storedgraphicfolder", "Macintosh HD:Library")

Open in new window

 which will do the same thing.
0
 

Accepted Solution

by:
versatilebb earned 0 total points
ID: 40307038
Well, this is definitely a workaround, but I was able to get this to work consistently by executing the command twice, and simply ignoring the "division by zero" error the first time:
On Error Resume Next
currentslide.Shapes.AddPicture fileName:=pickedgraphic, linktofile:=False, savewithdocument:=True, Left:=100,
currentslide.Shapes.AddPicture fileName:=pickedgraphic, linktofile:=False, savewithdocument:=True, Left:=100,
On Error Goto 0

Open in new window


Thanks, John, for the tip on the proper use of the default parameter in GetSettings.  I was very happy to learn that there is an equivalent to the Windows registry on the Mac and that you can access it from VBA using the same commands.
0
 

Author Closing Comment

by:versatilebb
ID: 40314024
Appreciate the suggestions from both experts, but neither one solved my issue.  In the meantime, I came up with a solution on my own.
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Using Word 2013, I was experiencing some incredible lag when typing.  Here's what worked for me....
Technology opened people to different means of presenting information, but PowerPoint remains to be above competition. Know why PPT still works today.
Learn how to make your own table of contents in Microsoft Word using paragraph styles and the automatic table of contents tool. We'll be using the paragraph styles in Word’s Home toolbar to help you create a table of contents. Type out your initial …
Learn how to create and modify your own paragraph styles in Microsoft Word. This can be helpful when wanting to make consistently referenced styles throughout a document or template.

757 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

24 Experts available now in Live!

Get 1:1 Help Now