Removing/deleting button from document

G

GUNNAR BAARDSEN

Hi.

I have a CommandButton (cmdClose) on a template.
When the document is loaded (based on the template), the new document is
displaying the CommandButton.

When I click the cmdClose-button, a subroutine is called which saves the
document.
After the document is saved, I want to remove/delete the cmdClose-button
from my saved document.

I can't access it in code (i.e. Application.ActiveDocument....). I can't
find any code that allows me to do something like this:

Application.ActiveDocument.cmdClose.Delete

or

Application.ActiveDocument.cmdClose.Visible=False

Please help with this one, as I'm totally stuck here.

Thanks,
Gunnar
NORWAY
 
D

Doug Robbins - Word MVP

Hi Gunnar,

I would suggest that you use a userform rather than insert controls in the
template itself.

See the article “How to create a Userform” at:

http://www.mvps.org/word/FAQs/Userforms/CreateAUserForm.htm

Please post any further questions or followup to the newsgroups for the
benefit of others who may be interested. Unsolicited questions forwarded
directly to me will only be answered on a paid consulting basis.

Hope this helps
Doug Robbins - Word MVP
 
C

Cindy M -WordMVP-

Hi Gunnar,

You might find my article on using activeX controls in Word documents
helpful.

Try it with something like

activeDocument.InlineShapes(1).Delete

Where (1) is the index of the inline shapes in the document. Note that you
can also select it and assign a bookmark name, then delete the
bookmark.range. Or, if the button has text-flow formatting applied, use
the Shapes collection (and in this case, you can use the .Name).

The problem with addressing the control object, itself, is that you'd have
to be in Design mode to do anything with it, and you probably don't want
to do that.
I have a CommandButton (cmdClose) on a template.
When the document is loaded (based on the template), the new document is
displaying the CommandButton.

When I click the cmdClose-button, a subroutine is called which saves the
document.
After the document is saved, I want to remove/delete the cmdClose-button
from my saved document.

I can't access it in code (i.e. Application.ActiveDocument....). I can't
find any code that allows me to do something like this:

Application.ActiveDocument.cmdClose.Delete

or

Application.ActiveDocument.cmdClose.Visible=False

Cindy Meister
INTER-Solutions, Switzerland
http://homepage.swissonline.ch/cindymeister (last update Sep 30 2003)
http://www.mvps.org/word

This reply is posted in the Newsgroup; please post any follow question or
reply in the newsgroup and not by e-mail :)
 
V

VikingS6

Hi again.

I have tried you suggestion, and it works - ALMOST.
The problem is that the attached code seems to do the trick, it hides the
textbox with my command button.
But after this code, I save my document again to keep the command button
invisible/removed from my *.doc file.

But when I load the document later, the damn button reappears.
What the heck am I doing wrong???
I have made sure that the correct index of Shapes is 1.

Please advise, as this is about to drive me crazy.

-----------------------------------------
Private Sub cmdClose_Click()
On Error GoTo cmdCloseErr

With Dialogs(wdDialogFileSaveAs)
.Name = ActiveDocument.Variables("DocumentName").Value
.Show
End With

With ActiveDocument
.Shapes(1).Select
.Shapes(1).Delete

' or this one
' .Shapes(1).Visible = msoFalse

.Save
.Close
End With

Exit Sub

cmdCloseErr:
MsgBox ("Error: " & Err)
If Err Then
MsgBox ("Saving was interrupted by user.")
Exit Sub
End If
End Sub
 
C

Cindy M -WordMVP-

Hi VikingS6,

Is cmdClose the button you're trying to delete? If it is, I can certainly
see why this could give problems. Since the code is still executing, you
certainly can't delete the button. And probably not affect any other
properties.

In this case, you may need a more convoluted approach. Since you're using
the button as a "shape" I take it that you require text wrap formatting
around the button?

Then I would
- Insert a FRAME (from the FORMS toolbar). A frame will allow textwrap
formatting, but is not a SHAPE

- Insert the command button into the frame, and leave it in-line with the
text

- Now you can change the font formatting of the frame to "hidden" in order
to hide the button. ActiveDocument.Frames(1).Range.Font.Hidden = True

Since doesn't act on the button itself, but the text flow in which it is
embedded, it should work.

Another approach might be to have a macro trigger on saving or closing the
document (something that takes place when the button is not executing any
code) that deletes the Shape.
I have tried you suggestion, and it works - ALMOST.
The problem is that the attached code seems to do the trick, it hides the
textbox with my command button.
But after this code, I save my document again to keep the command button
invisible/removed from my *.doc file.

But when I load the document later, the damn button reappears.
What the heck am I doing wrong???
I have made sure that the correct index of Shapes is 1.

Cindy Meister
INTER-Solutions, Switzerland
http://homepage.swissonline.ch/cindymeister (last update Sep 30 2003)
http://www.mvps.org/word

This reply is posted in the Newsgroup; please post any follow question or
reply in the newsgroup and not by e-mail :)
 
G

Gunnar

Hi there.

Yes, now it's working.
I should have seen it, of course you can't delete the button with the code
still executing.

Thanks for all your help.

Gunnar
 

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