make buttons invisible

R

rutica

I have a Microsoft Word 2003 document that has mail merge fields with
Excel as its data source.

I created a few command buttons on my main form. One button allows the
user to select the data source, one allows the user to select
recipients and the third Merges the data and creates a new document.

I would like those command buttons to *not* appear on the newly created
documents.

Here is my code for the button that creates the new document:

Private Sub cmdMergeDocument_Click()
With ActiveDocument.MailMerge
Destination = wdSendToNewDocument
SuppressBlankLines = True

Me.cmdSelectDataSource.Visible = False 'this doesn't work!
Me.cmdMergeDocument.Visible = False 'this doesn't work!
Me.cmdSelectRecipients.Visible = False 'this doesn't work!

With .DataSource
FirstRecord = wdDefaultFirstRecord
LastRecord = wdDefaultLastRecord
End With
Execute Pause:=False
End With

'now show the buttons again on the main form
Me.cmdSelectDataSource.Visible = True 'this doesn't work!
Me.cmdMergeDocument.Visible = True 'this doesn't work!
Me.cmdSelectRecipients.Visible = True 'this doesn't work!
End Sub

I get 'Object doesn't support this property or method'.

I am able to do: cmdSelectDataSource.Enabled = False, but it's not what
I need. I need the buttons invisible, not disabled.

Help!
Thanks,
 
D

Doug Robbins - Word MVP

See my response to your post of yesterday, or save the document as a
template to which you add a custom toolbar containing buttons to run your
code. ActiveX buttons are not suitable for use in a document that is to be
printed.

--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP, originally posted via msnews.microsoft.com
 
P

Peter Jamieson

Perhaps the best you can do is save their .Length and .Width properties,
set them each to 1 prior to the merge, then set them back to the saved
values.

Unless there is another approach that would fit your application, e.g.
you insert macrobutton fields and connect them to appropriate VBA
functions, then remove the buttons altogether prior to merging, then
re-insert them after.

Peter Jamieson

http://tips.pjmsn.me.uk
 
R

rutica

thanks for writing.

Peter, I tried Me.cmdMergeDocument.Width = 1 and it works!

The only weird thing is setting it back to normal on the main document.
I want my height=45 and my width=117.

But for some reason, if my code lists the height first:
Me.cmdSelectDataSource.height =45
Me.cmdSelectDataSource.width=117
then both the height and the width are 117.

If i list the width first:
Me.cmdSelectDataSource.width=117
Me.cmdSelectDataSource.height=45
then both the height and the width are 45.

It's seems to only consider the last command and then apply that value
to both height and width?

Here is my code that shows both the height and width as 45:

With ActiveDocument.MailMerge
Destination = wdSendToNewDocument
SuppressBlankLines = True
With .DataSource

'make the buttons small
Me.cmdMergeDocument.Width = 1
Me.cmdSelectDataSource.Width = 1
Me.cmdSelectRecipients.Width = 1

FirstRecord = wdDefaultFirstRecord
LastRecord = wdDefaultLastRecord

End With
Execute Pause:=False
End With

'now re-size the buttons again
Me.cmdSelectDataSource.Width = 117
Me.cmdSelectDataSource.Height = 45

Me.cmdSelectRecipients.Width = 117
Me.cmdSelectRecipients.Height = 45

Me.cmdMergeDocument.Width = 117
Me.cmdMergeDocument.Height = 45

How can i make the width=117 and the height=45?

Weird.
 
P

Peter Jamieson

I tried something more like

<<
Dim intHeight As Integer
Dim intWidth As Integer

' save the button dimensions

intHeight = Me.cmdSelectDataSource.Height
intWidth = Me.cmdSelectDataSource.Width

' do the merge
'

' restore the button dimensions

Me.cmdSelectDataSource.Height = intHeight
Me.cmdSelectDataSource.Width = intWidth
It seems logically pretty much the same as yours, so
a. it is possible that mine wasn't working quite as I imagined. I'll
have anther look.
b. let's make sure we are using the same kind of buttons. Are you
using the standard "ActiveX" buttons (I think they are "Forms.2"
buttons)? I was actually testing with Word 2007 but I'll have a look
with Word 2003.

Peter Jamieson

http://tips.pjmsn.me.uk
 
R

rutica

Peter, thank for writing again. I tried your way, but still got the
same result.

Something is forcing my code to be a square, I can't get the height and
width to be different. I am using the 'Control toolbox' to create a
command button. (In Office 2003: View, Toolbars, Control Toolbox). I
don't know if that is ActiveX...

I was able to get around my problem by having the height and width both
75. That suits my needs, this time at least.

Below is my code (to test, I only tried it with the cmdSelectDataSource
button). But the code doesn't work. Again, the height and width for
cmdSelectDataSource is the same even though the message box I created
for testing below shows a different height and width.

My AutoSize property for the 3 buttons is set to False. Could it
somehow to related to that? I tested by changing them to True, but no
change.

Private Sub cmdMergeDocument_Click()
On Error GoTo Err_error_Click
'merge document-step 3
Dim intHeight As Integer
Dim intWidth As Integer

intHeight = Me.cmdSelectDataSource.Height
intWidth = Me.cmdSelectDataSource.Width

'display the height and width for testing purposes
MsgBox "height is: " & intHeight & " and width is: " & intWidth

If MsgBox("Are you sure you want to create a new document(s)?",
vbOKCancel, "Create documents") = vbCancel Then
Exit Sub
End If

'first check if the document if protected. If so, unprotect it. Opening
the document triggers code to protect it (see the Document_Open()
event)
If ActiveDocument.ProtectionType = wdAllowOnlyFormFields Then
ActiveDocument.Unprotect "hi"
End If


With ActiveDocument.MailMerge
Destination = wdSendToNewDocument
SuppressBlankLines = True
With .DataSource

'make the buttons small
Me.cmdMergeDocument.Width = 1
Me.cmdSelectDataSource.Width = 1
Me.cmdSelectRecipients.Width = 1

FirstRecord = wdDefaultFirstRecord
LastRecord = wdDefaultLastRecord

End With
Execute Pause:=False
End With

'now re-size the buttons again
Me.cmdSelectDataSource.Height = intHeight
Me.cmdSelectDataSource.Width = intWidth

Me.cmdSelectRecipients.Height = 45
Me.cmdSelectRecipients.Width = 75

Me.cmdMergeDocument.Height = 45
Me.cmdMergeDocument.Width = 75


'protect document again
ThisDocument.Protect Type:=wdAllowOnlyFormFields, NoReset:=True,
Password:="hi"

Exit_cmdMergeDocument_Click:
Exit Sub

Err_error_Click:
MsgBox Err.Description
Resume Exit_cmdMergeDocument_Click

End Sub

Thanks,
 
P

Peter Jamieson

Hi, I tried it again with your code on Word 2003 and it is OK here.

If you open your main document and use Alt-F9 to look at the field codes
for the buttons, do you see

{ CONTROL Forms.CommandButton.1 \s }

or something else?

The few properties I changed experimentally (.Autosize, .Wordwrap) made
no difference. However, my test document is extremely simple (just the
three buttons in a Normal style paragraph, with a merge field to the
right or underneath. Again, simple tests (e.g. putting the buttons in a
table, or assigning an image to the button) made no difference, but I
wonder if there is something about the layout of your document that is
causing the difference?


Peter Jamieson

http://tips.pjmsn.me.uk
 
R

rutica

I figured it out!

I had to uncheck 'lock aspect ratio'. I right clicked on my command
button and selected 'Format Control'. Then in the Size tab, I had to
uncheck 'lock aspect ratio'.

Thanks for writing back and trying to help!
 

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