Solved

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

Posted on 2014-09-03
6
458 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 49

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
Back Up Your Microsoft Windows Server®

Back up all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

 
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

Three Reasons Why Backup is Strategic

Backup is strategic to your business because your data is strategic to your business. Without backup, your business will fail. This white paper explains why it is vital for you to design and immediately execute a backup strategy to protect 100 percent of your data.

Question has a verified solution.

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

Suggested Solutions

Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
Using Word 2013, I was experiencing some incredible lag when typing.  Here's what worked for me....
The viewer will learn how to use the =DISCRINV command to create a discrete random variable, use this command to model a set of probabilities and outcomes in a Monte Carlo simulation, and learn how to find the standard deviation of a set of probabil…
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.

778 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