tags:

views:

946

answers:

3

I have some code which keeps causing an

Error 70: Permission Denied

in my VBA code. I can't work out why, because I know that the worksheet is unprotected and that I can make changes to it. The code in question is

sh.Name = "square"

It attempts to rename a shape that has been copied from another sheet and pasted into the sheet - there are no other shapes in the sheet with that name, because prior to these code I have already deleted all shapes with that name.

Any suggestion as to what might cause this permissions error?

A: 

Hi,

"Permission Denied" is not for a protected worksheet but for wrong access to a property or variable.

I believe that "sh" is null at the point you are trying to access it and set its "Name" property. try to see if you initialized it correctly before setting its properties.

yn2
Checked this, and sh is definitely a valid shape
a_m0d
+3  A: 

Generally that one is caused by trying to use the same name twice. Try doing this instead:

Sub Example()
    Dim lngIndx As Long
    Dim ws As Excel.Worksheet
    Dim shp As Excel.Shape
    Set ws = Excel.ActiveSheet
    Set shp = ws.Shapes.AddShape(msoShapeOval, 174#, 94.5, 207#, 191.25)
    If NameUsed(ws, "Foo") Then
        lngIndx = 2
        Do While NameUsed(ws, "Foo" & CStr(lngIndx))
            lngIndx = lngIndx + 1
        Loop
        shp.name = "Foo" & CStr(lngIndx)
    Else
        shp.name = "Foo"
    End If
End Sub

Private Function NameUsed(ByVal parent As Excel.Worksheet, ByVal name As String) As Boolean
    Dim shp As Excel.Shape
    Dim blnRtnVal As Boolean
    name = LCase$(name)
    For Each shp In parent.Shapes
        If LCase$(shp.name) = name Then
            blnRtnVal = True
            Exit For
        End If
    Next
    NameUsed = blnRtnVal
End Function
Oorang
That helped - I used your NameUsed function (with a couple modifications) and discovered that the other shape with that name doesn't always get deleted (sometimes an error occurs when I try to call sh.Cut on that shape)
a_m0d
Does that answer your question then?
Oorang
Yeah, that explains why I couldn't rename the shapes - thanks.
a_m0d
A: 

Hi,

I have the same problem. I am trying to upload directly a picture from excel to a Sharepoint group URL. Is that possible? If it's not then can you suggest another way of doing it? Thanks

perfs, I think that you should probably ask this as a new question, since I am not sure that it is exactly the same as mine.
a_m0d