Solved

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

Posted on 2014-09-03
6
495 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:Bryce Bassett
[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 11

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 52

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:Bryce Bassett
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
Office 365 Training for Admins - 7 Day Trial

Learn how to provision tenants, synchronize on-premise Active Directory, implement Single Sign-On, customize Office deployment, and protect your organization with eDiscovery and DLP policies.  Only from Platform Scholar.

 
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:
Bryce Bassett 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:Bryce Bassett
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

This article describes a serious pitfall that can happen when deleting shapes using VBA.
This article describes how you can use Custom Document Properties to store settings and other information in your workbook so that they will be available the next time you open the workbook.
This video shows where to find templates, what they are used for, and how to create and save a custom template using Microsoft Word.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

623 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