PDA

View Full Version : Enable / Disable Button in document using macro



mbuder
08-18-2016, 02:55 AM
Hi Guys,

I've got a problem accessing a button from a sub/macro which was placed on a word document.

For a document template I've created a word document with 3 buttons (Properties, Save As, PDF). By pressing the button "Properties" a user form appears to enter all the required document properties. The "Save As" and "PDF" button should be disabled "not clickable" until the user form was processed successfully.

My problem here is that I can only access the "Properties" button properties from "ThisDocument" and not from my "UserFormDocProperties" where my macro is located that checks the content of all user form fields. (see screen shot attached)

16902

Any advice on how to do this would be great.

Thanks a lot
Mathias

gmaxey
08-21-2016, 11:49 AM
You should be able to access ThisDocument controls via the class:

For example a UserForm command button click event:



Private Sub CommandButton1_Click()

If MsgBox("Doyou want to enable the document buttons?", vbQuestion + vbYesNo,"DECISION") = vbYes Then

ThisDocument.CommandButtonPdf.Enabled = True

ThisDocument.CommandButtonSave.Enabled = True

Else
ThisDocument.CommandButtonPdf.Enabled = False

ThisDocument.CommandButtonSave.Enabled = False

End If

Unload Me
lbl_Exit:
Exit Sub
End Sub

mbuder
08-22-2016, 01:08 AM
Hi Greg,

thanks a lot for the quick response. I've tried the code above and could access the button using the "ThisDocument" class in my template file. When I use the template to create a new file (by double clicking it) and do the same before I save it it does not work. The purpose for all that user form stuff is that I want to force the user to fill out the form before he is able to save the file. For that I want to disable the "save file" button until the form is filled. Is it neccessery to save the document to have access to the "ThisDocument" class?

Greetings,
Matt

gmaxey
08-23-2016, 12:57 PM
Matt the ActiveX controls in the active document are InlineShapes. You could also use something like this:


Dim oILS As InlineShape
For Each oILS In ActiveDocument.InlineShapes
If oILS.OLEFormat.Object.Name = "CommandButtonPdf" Then
oILS.OLEFormat.Object.Enabled = False
End If
Next oILS