Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Deleting a command button in its own onclick event 1

Status
Not open for further replies.

ouzojd

Programmer
Jun 9, 2002
314
AU
Hello all, excuse my ignorance MSOffice isnt my usual thing but in the following code I have a command button that is to save a word document as a .doc file then delete the command button then save the document as a .htm file. The reason been I dont want the button showing on the .htm file. I am getting Invalid Call or Procedure when it gets to line commandbutton1.delete Any thoughts?



Private Sub CommandButton1_Click()

On Error GoTo error_handler

Dim sname As String
Dim fname As String
Dim response As Integer
Dim length As Long
Dim wname As String




wname = "eligible.doc"
fname = "eligible.htm"




length = FileLen("\\PIRNWFS1\VOL1\GRPDATA\HELPDESK\Operations\foobar\" & fname)






ChangeFileOpenDirectory "\\PIRNWFS1\VOL1\GRPDATA\HELPDESK\Operations\Foobar\"
ActiveDocument.SaveAs FileName:=wname, AddToRecentFiles:=True

Commandbutton1.Delete


ActiveDocument.SaveAs FileName:=fname, FileFormat:=wdFormatHTML, AddToRecentFiles:=True
ActiveWindow.View.Type = wdWebView



response = MsgBox("File saved as '" & fname & "', do you want to close document?", vbYesNo, "GODS")
If response = vbYes Then
ActiveDocument.Close
End If
Exit Sub

error_handler:
Select Case Err.Number
Case 53
length = -1
Resume Next
Case Else
MsgBox Err.Description
End Select

End Sub
 
ouzojd

There are 2 reasons you can not do what you want the way you are trying to.

1)You can not delete a control that has the focus; and,

2)You can not delete a control while it's code is running. If it were possible to do so, all the code after the delete would not be available to run.

So my suggestion is to write another sub containing all the code from the delete line down. The first line in this new sub should set the focus to any other control or area on your document. The remaining lines in the new sub are the delete and following lines from your button's sub.

The last line in the button sub should then call the new sub. That way the button's sub executes the first part of your original code then passes execution to the new sub which removes focus from the button and deletes it. Finally executing the remainder of the original code.

HTH,

Vic
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top