Solved

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

Posted on 2014-09-03
6
454 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
Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

 
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

Backup Your Microsoft Windows Server®

Backup 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.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Wierd issue with missing "FROM" field in Outlook. 7 95
Excel 2013 Power Query 1 46
Excel Type grid in Publisher 1 32
Vlookup nonexact IP address match 32 54
Today, still in the boom of Apple, PC's and products, nearly 50% of the computer users use Windows as graphical operating systems. If you are among those users who love windows, but are grappling to keep the system's hard drive optimized, then you s…
PowerPoint is the go-to presentation software for millions of users around the world. Many presentations use basic text features but you can really make special text jump out of your slide by applying this bubble text design process. This article ha…
This video shows the viewer how to set up and create Footnotes in their document. Click on the References tab: Select "Insert Footnote": Type in desired text:
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

863 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

29 Experts available now in Live!

Get 1:1 Help Now