0

I got "error '91': Object variable or With block variable not set" after running:

For Each obj In ActiveDocument.InlineShapes
    If obj.OLEFormat.Object.Name = "Button" Then '<-error line
        obj.Delete
    End If
Next

I thought that I need to declare obj as InlineShape or as Object, but results were the same. I am sure that I have command button with name Button and I know diference between name and caption, neither works. This code is part of the code that runs after the same button is pressed.

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Milan
  • 35
  • 1
  • 8
  • Looking at your code, if the delete buttons name is `Button` and the posted code is the code it fires, the macro should delete the clicked button? – Gareth May 27 '14 at 12:39
  • Yes, it should. Macro does something else and then it should delete clicked button. User cannot run that macro twice. I used something similiar in excel and it works. – Milan May 27 '14 at 12:58
  • If user click command button "Button", macro starts and does something and than delete command button "Button". – Milan May 27 '14 at 13:00
  • It's Command Button (ActiveX Control). How can I loop throught controls? – Milan May 27 '14 at 13:44

1 Answers1

0

I've tested the below code which works:

Sub delete()

Dim obj As Object

For Each obj In ActiveDocument.InlineShapes
    If obj.OLEFormat.Object.Name = "Button" Then
        obj.delete
    End If
Next obj

End Sub
Gareth
  • 5,140
  • 5
  • 42
  • 73
  • ActiveDocument doesn't support .Controls – Milan May 27 '14 at 14:05
  • Sorry I've amended, you should be using `InlineShapes` (my error). I've tested the edited code which works for me when there's an activex command button called 'Button' on the document. – Gareth May 27 '14 at 14:18
  • That is the same code as I posted :) I still get error. – Milan May 28 '14 at 11:28
  • It's not, it defines `obj` as an object which you hadn't in your code. Have you tried it with defining `obj`? – Gareth May 28 '14 at 11:40