Solved

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

Posted on 2014-09-03
6
466 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 50

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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Using Word 2013, I was experiencing some incredible lag when typing.  Here's what worked for me....
Many programs have tried to outwit PowerPoint in terms of technology and skill. These programs, however, still lack several characteristics that PowerPoint has possessed from the start. Here's why PowerPoint replacements won't entirely work for desi…
This video walks the viewer through the process of creating envelopes and labels, with multiple names and addresses. Navigate to the “Start Mail Merge” button in the Mailings tab: Follow the step-by-step process until asked to find the address doc…
The viewer will learn how to  create a slide that will launch other presentations in Microsoft PowerPoint. In the finished slide, each item launches a new PowerPoint presentation and when each is finished it automatically comes back to this slide: …

733 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