Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #16  
Old 01-12-2021, 02:11 PM
thedoost thedoost is offline VBA to set Content controls as non printing Windows 10 VBA to set Content controls as non printing Office 2010
Novice
 
Join Date: Jan 2021
Posts: 5
thedoost is on a distinguished road
Default

Nope. Something interesting though - I just made a copy of your template and opened it and tried to print. The message box appeared again, so that excited me, and it printed with the underscores in place of the placeholder text as it should. Yay!... then I tried it again and it's back to not working


So something is clearly happening that I can't explain and I'm sure it's almost impossible for you to figure out without access to my computer. I don't understand what could possibly be happening though and it's very frustrating.
I'm open to any more suggestions but I suppose I'll just move on and forget about implementing this into my document.
Reply With Quote
  #17  
Old 01-12-2021, 03:35 PM
macropod's Avatar
macropod macropod is online now VBA to set Content controls as non printing Windows 10 VBA to set Content controls as non printing Office 2016
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 20,710
macropod has a brilliant futuremacropod has a brilliant futuremacropod has a brilliant futuremacropod has a brilliant futuremacropod has a brilliant futuremacropod has a brilliant futuremacropod has a brilliant futuremacropod has a brilliant futuremacropod has a brilliant futuremacropod has a brilliant futuremacropod has a brilliant future
Default

It works for me - repeatedly. Did you have another document that's attached to another template containing a BeforePrint macro open at the same time?
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #18  
Old 01-12-2021, 03:45 PM
thedoost thedoost is offline VBA to set Content controls as non printing Windows 10 VBA to set Content controls as non printing Office 2010
Novice
 
Join Date: Jan 2021
Posts: 5
thedoost is on a distinguished road
Default

I have no other documents open, just that one template.
When things like this happen it really bugs me so I just want to figure it out lol. I am losing hope, however. Is there a way to debug it on my end? I'm not getting any errors or anything, it just prints with the placeholder text showing. I'll note that there is a popup for my printer indicating low ink levels, would that somehow get in the way? Seems silly but all I can think of at the moment.
Reply With Quote
  #19  
Old 01-12-2021, 04:08 PM
gmaxey gmaxey is offline VBA to set Content controls as non printing Windows 10 VBA to set Content controls as non printing Office 2016
Word MVP 2003-2009
 
Join Date: May 2010
Location: Marble, NC
Posts: 1,176
gmaxey has a spectacular aura aboutgmaxey has a spectacular aura aboutgmaxey has a spectacular aura about
Default

Maybe a timing issue with the print spool. You might try:

Code:
Option Explicit
Public WithEvents oApp As Word.Application
Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)

Private Sub oApp_DocumentBeforePrint(ByVal Doc As Document, Cancel As Boolean)
Application.ScreenUpdating = False
MsgBox "!"
Dim CCtrl As ContentControl, i As Long
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
  DoEvents
  Sleep 2000
  .Undo i
End With
Cancel = True
Application.ScreenUpdating = True
End Sub
__________________
Greg Maxey
Please visit my web site at http://www.gregmaxey.com/
Reply With Quote
  #20  
Old 01-12-2021, 04:22 PM
thedoost thedoost is offline VBA to set Content controls as non printing Windows 10 VBA to set Content controls as non printing Office 2010
Novice
 
Join Date: Jan 2021
Posts: 5
thedoost is on a distinguished road
Default

You might be right, Greg, but weird that it would work initially with the same printer popup. I hope you're right.
I just tried copying the code in but I get a Compile error:
"The code in this project must be updated for use on 64-bit systems. Please review and update Declare statements and then mark them with the PtrSafe attribute."
Any idea what I can do to fix it? It highlights the word "Sub" in the third line of code if that helps.
Reply With Quote
  #21  
Old 01-12-2021, 04:37 PM
gmaxey gmaxey is offline VBA to set Content controls as non printing Windows 10 VBA to set Content controls as non printing Office 2016
Word MVP 2003-2009
 
Join Date: May 2010
Location: Marble, NC
Posts: 1,176
gmaxey has a spectacular aura aboutgmaxey has a spectacular aura aboutgmaxey has a spectacular aura about
Default

Replace that single Declare Sub Line with:

#If VBA7 And Win64 Then
Private Declare PtrSafe Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
#Else
Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
#End If
__________________
Greg Maxey
Please visit my web site at http://www.gregmaxey.com/
Reply With Quote
  #22  
Old 01-12-2021, 04:44 PM
macropod's Avatar
macropod macropod is online now VBA to set Content controls as non printing Windows 10 VBA to set Content controls as non printing Office 2016
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 20,710
macropod has a brilliant futuremacropod has a brilliant futuremacropod has a brilliant futuremacropod has a brilliant futuremacropod has a brilliant futuremacropod has a brilliant futuremacropod has a brilliant futuremacropod has a brilliant futuremacropod has a brilliant futuremacropod has a brilliant futuremacropod has a brilliant future
Default

Since the content controls aren't being updated, that suggests Greg's delay implementation after '.PrintOut' won't make any difference.

Here's another version of the code:
Code:
Private Sub wdApp_DocumentBeforePrint(ByVal Doc As Document, Cancel As Boolean)
Application.ScreenUpdating = False
Dim CCtrl As ContentControl, objUndo As UndoRecord
Set objUndo = Application.UndoRecord: objUndo.StartCustomRecord
With Doc
  For Each CCtrl In .ContentControls
    With CCtrl
      If .Type <> wdContentControlPicture And .Type <> wdContentControlCheckBox Then
        If .Range.Text = .PlaceholderText Then
          .Range.Text = "_________________________"
        End If
      End If
    End With
  Next
  .PrintOut
  objUndo.EndCustomRecord
  .Undo
End With
Cancel = True
Application.ScreenUpdating = True
End Sub
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
Reply

Thread Tools
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Last Post
VBA to set Content controls as non printing Content Controls Sammie0Sue Word 6 11-06-2013 10:56 PM
VBA to set Content controls as non printing 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
VBA to set Content controls as non printing Grouping Content Controls cksm4 Word VBA 2 03-01-2011 12:46 PM

Other Forums: Access Forums - Senior Forums

All times are GMT -7. The time now is 05:40 PM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2021, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2021 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft