InlineShapes index

K

Karen Hagerman

I am programmatically adding a command button to a document. I've named it
"InsertBodyText" because.....after the body text has been inserted, I want
the user to click the button to indicate 'OK' then I'll delete the button
and save the doc.

I would like to refer to the button by

ActiveDocuments.InlineShapes("InsertBodyText").Delete

but the only thing that works is

ActiveDocuments.InlineShapes(1).Delete

which could definitely become a problem is some other inline shape is added.

Is there any way to refer to an InlineShape with a name instead of a number?
 
K

Karen Hagerman

OK,

I've read enough help now so I know I can't use the name of the command
button so...................anyone know how to ascertain the index of the
newly-added command button without iterating through all inlineshapes in a
document?



--
Karen
I am programmatically adding a command button to a document. I've named it
"InsertBodyText" because.....after the body text has been inserted, I want
the user to click the button to indicate 'OK' then I'll delete the button
and save the doc.

I would like to refer to the button by

ActiveDocuments.InlineShapes("InsertBodyText").Delete

but the only thing that works is

ActiveDocuments.InlineShapes(1).Delete

which could definitely become a problem is some other inline shape is added.

Is there any way to refer to an InlineShape with a name instead of a number?
 
J

Jezebel

The index number won't help here, because it's not fixed. The inlineshapes
(like many Word collections) are indexed in order from the start of the
document, so the shape's index number will change if another shape is added
removed anywhere above it in the document.

A couple of approaches that come to mind --

1. Retain a module-level reference to the shape when you create it:

Private mButton as Word.InlineShape

Sub CreateButton()
Set mButton = ActiveDocument.AddOLEControl (....)
End Sub

Sub DestroyButton()
mButton.Delete
Set mButton = nothing
End Sub


2. Set a bookmark to the range to which the shape is anchored, and use that
to retrieve it.
 
K

Karen Hagerman

Jezebel,

Thanks, this is a great help but..........I added

Sub mbutton_click()
DestroyButton
End Sub

The click event doesn't happen, any hints in that direction?

--
Karen

The index number won't help here, because it's not fixed. The inlineshapes
(like many Word collections) are indexed in order from the start of the
document, so the shape's index number will change if another shape is added
removed anywhere above it in the document.

A couple of approaches that come to mind --

1. Retain a module-level reference to the shape when you create it:

Private mButton as Word.InlineShape

Sub CreateButton()
Set mButton = ActiveDocument.AddOLEControl (....)
End Sub

Sub DestroyButton()
mButton.Delete
Set mButton = nothing
End Sub


2. Set a bookmark to the range to which the shape is anchored, and use that
to retrieve it.
 
J

Jezebel

To create the click event for a commandbutton:

1. Display the VisualBasic toolbar. Click the Design Mode button.

2. Right-click the command button and select View Code.

3. This should display the VBE, with the code module for ThisDocument and a
function called CommandButton1_Click(). Call the DestroyButton code from
there.



Karen Hagerman said:
Jezebel,

Thanks, this is a great help but..........I added

Sub mbutton_click()
DestroyButton
End Sub

The click event doesn't happen, any hints in that direction?

--
Karen

The index number won't help here, because it's not fixed. The inlineshapes
(like many Word collections) are indexed in order from the start of the
document, so the shape's index number will change if another shape is added
removed anywhere above it in the document.

A couple of approaches that come to mind --

1. Retain a module-level reference to the shape when you create it:

Private mButton as Word.InlineShape

Sub CreateButton()
Set mButton = ActiveDocument.AddOLEControl (....)
End Sub

Sub DestroyButton()
mButton.Delete
Set mButton = nothing
End Sub


2. Set a bookmark to the range to which the shape is anchored, and use that
to retrieve it.
 
K

Karen Hagerman

Jezebel,

OK, I can create and I can now capture the click event but in the

Sub DestroyButton()

mButton.Delete
Set mButton = Nothing

End Sub

it throws the error "Object variable or With block variable not set"

Anyway, if you don't see anything obvious, I've got code that will only
break if another inlineshape is added......feels like quicksand :)

--
Karen
To create the click event for a commandbutton:

1. Display the VisualBasic toolbar. Click the Design Mode button.

2. Right-click the command button and select View Code.

3. This should display the VBE, with the code module for ThisDocument and a
function called CommandButton1_Click(). Call the DestroyButton code from
there.



Karen Hagerman said:
Jezebel,

Thanks, this is a great help but..........I added

Sub mbutton_click()
DestroyButton
End Sub

The click event doesn't happen, any hints in that direction?

--
Karen

The index number won't help here, because it's not fixed. The inlineshapes
(like many Word collections) are indexed in order from the start of the
document, so the shape's index number will change if another shape is added
removed anywhere above it in the document.

A couple of approaches that come to mind --

1. Retain a module-level reference to the shape when you create it:

Private mButton as Word.InlineShape

Sub CreateButton()
Set mButton = ActiveDocument.AddOLEControl (....)
End Sub

Sub DestroyButton()
mButton.Delete
Set mButton = nothing
End Sub


2. Set a bookmark to the range to which the shape is anchored, and use that
to retrieve it.
 
J

Jezebel

That error means that mButton doesn't contain anything. First, check your
CreateButton code: put in a test like
If mButton is nothing then ....

to make sure that that's working correctly.

Bear in mind that module-level variables get cleared if you reset the VBE
project. This happens automatically if you edit any code. So while you're
working on your macros you'll need to restart each time you test, or
manually delete the button then re-run CreateButton.




Karen Hagerman said:
Jezebel,

OK, I can create and I can now capture the click event but in the

Sub DestroyButton()

mButton.Delete
Set mButton = Nothing

End Sub

it throws the error "Object variable or With block variable not set"

Anyway, if you don't see anything obvious, I've got code that will only
break if another inlineshape is added......feels like quicksand :)

--
Karen
To create the click event for a commandbutton:

1. Display the VisualBasic toolbar. Click the Design Mode button.

2. Right-click the command button and select View Code.

3. This should display the VBE, with the code module for ThisDocument and a
function called CommandButton1_Click(). Call the DestroyButton code from
there.



Karen Hagerman said:
Jezebel,

Thanks, this is a great help but..........I added

Sub mbutton_click()
DestroyButton
End Sub

The click event doesn't happen, any hints in that direction?

--
Karen

The index number won't help here, because it's not fixed. The inlineshapes
(like many Word collections) are indexed in order from the start of the
document, so the shape's index number will change if another shape is added
removed anywhere above it in the document.

A couple of approaches that come to mind --

1. Retain a module-level reference to the shape when you create it:

Private mButton as Word.InlineShape

Sub CreateButton()
Set mButton = ActiveDocument.AddOLEControl (....)
End Sub

Sub DestroyButton()
mButton.Delete
Set mButton = nothing
End Sub


2. Set a bookmark to the range to which the shape is anchored, and use that
to retrieve it.
in
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top