#1
|
|||
|
|||
VBA to set Content controls as non printing
Hi I am creating a form that uses LOTS of content controls as it will be protected when sent to clients but it should also be able to be printed and filled in by hand so I do not want the content controls to print if they have not been selected.
I want the code to work in the back ground and do not want the clients to have to click any where for the script to run. |
#2
|
||||
|
||||
What you would need for this to work is an event-driven 'DocumentBeforePrint' macro, coded along the lines of:
Code:
Private Sub oApp_DocumentBeforePrint(ByVal Doc As Document, Cancel As Boolean) Application.ScreenUpdating = False Dim CCtrl As ContentControl, i As Long MsgBox "!" With Doc For Each CCtrl In .ContentControls With CCtrl If .Type <> wdContentControlPicture And .Type <> wdContentControlCheckBox Then If .Range.Text = .PlaceholderText Then i = i + 1 .Range.Text = "_________________________" End If End If End With Next .PrintOut .Undo i End With Cancel = True Application.ScreenUpdating = True End Sub The message box exists merely to demonstrate that the macro has been called - it can be deleted once you have the code working correctly. To see how to implement such a macro, go to: Intercepting events like Save and Print
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] Last edited by macropod; 11-06-2013 at 11:35 PM. Reason: Added code for CheckBoxes |
#3
|
|||
|
|||
Thanks a bunch for the reply but unfortunately my content controls that have not been used still seem to be printing
|
#4
|
||||
|
||||
Did you do the full event-driven macro implementation as described in the link?
If you did and it's still not working, can you attach a document to a post with some representative data (delete anything sensitive)? You do this via the paperclip symbol on the 'Go Advanced' tab.
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#5
|
|||
|
|||
Hi yes i followed the steps but I might have done something wrong.
Sorry I am new to all of this and your help is greatly appreciated |
#6
|
||||
|
||||
I don't see any evidence of either the macro I posted or the event-driven macro implementation in your attachment.
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#7
|
|||
|
|||
Hi sorry I had done it in my original doc.
I have now added it to the demo for you.. |
#8
|
||||
|
||||
All you've done is to embed the macro I posted inside another. That will never work. What is more, there's still no evidence of the event-driven macro implementation described in the link in post #2.
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#9
|
|||
|
|||
|
#10
|
|||
|
|||
Hi Greg
I actually came across your website and tried the 3 options but for some reason my content controls still seem to be printing Starting to feel a bit hopeless vba is really not my strongest, I normally just used active X controls as I know they do not print. Had to use content controls this time as i am populating a drop down from sharepoint |
#11
|
||||
|
||||
See attached template.
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#12
|
|||
|
|||
I know this thread is old but this is exactly what I want to do. I downloaded the template that macropod posted and it worked once for me then stopped for some reason. The first time I printed from that template the message box appeared with the "!" but it hasn't showed again since and the placeholder text still prints. Anyone have any idea what's happening?
I'm using Microsoft Office 365 64-bit and it says my Word version is 2012 Build 13530.20316 if that helps any. Thanks |
#13
|
||||
|
||||
That suggests you no longer have the template loaded. In Word, whatever document you want code stored in a template to run on must be both attached to and accessible to the document.
Accordingly, you need to either: • copy all the code from the CCtrlBeforePrint template and paste into corresponding modules in your own template; or • create the relevant documents the CCtrlBeforePrint template (which can be renamed to something more suitable and reformatted beforehand), or at least attach the existing document(s) to that template.
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#14
|
|||
|
|||
But I'm still just working from the template that you posted, I haven't even tried to put the code into my document yet. I wanted to make sure it worked first, so I'm not sure how the template would no longer be loaded? Just so I understand, should that message box appear every time I print that template document? If yes, what would cause the message box to stop appearing, other than it not being loaded? I restarted my computer to see if that might help but it hasn't.
I appreciate your time, thank you. |
#15
|
||||
|
||||
Have you perhaps added the code to another template? If so, there may be a conflict preventing the code from running properly.
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
Thread Tools | |
Display Modes | |
|
Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
Content Controls | Sammie0Sue | Word | 6 | 11-06-2013 10:56 PM |
Rich Text Content Controls: Formatting? | tinfanide | Word VBA | 8 | 03-04-2013 04:15 AM |
Macro to link 2 content controls | bortonj88 | Word VBA | 2 | 08-21-2012 06:24 AM |
Content Controls Form Programming | beachdog | Word VBA | 4 | 09-20-2011 10:26 AM |
Grouping Content Controls | cksm4 | Word VBA | 2 | 03-01-2011 12:46 PM |