Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 03-06-2016, 04:15 PM
highrise955 highrise955 is offline VBA Code in template does not run in new document Windows 10 VBA Code in template does not run in new document Office 2013
Advanced Beginner
VBA Code in template does not run in new document
 
Join Date: Mar 2016
Posts: 37
highrise955 is on a distinguished road
Default VBA Code in template does not run in new document


Hi all,

Just looking to get pointed in the right direction here. I created a template that starts with a userform which populates the template. Everything works great when I "Open" the document. However, when I select "New" for the template and it generates a new document based on the template (probably not using the proper nomenclature here, but I am guessing/hoping you know what I mean) the userform/code does not run.

I'm assuming I created the code in the wrong place to have it "inherit" to the spawned documents but I just can't figure out where it should go.

Any help would be appreciated.
Reply With Quote
  #2  
Old 03-06-2016, 04:25 PM
macropod's Avatar
macropod macropod is offline VBA Code in template does not run in new document Windows 7 64bit VBA Code in template does not run in new document Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,963
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

Your template should have code to trigger the userform via both a Document_New macro and a Document_Open macro.
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #3  
Old 03-06-2016, 04:33 PM
highrise955 highrise955 is offline VBA Code in template does not run in new document Windows 10 VBA Code in template does not run in new document Office 2013
Advanced Beginner
VBA Code in template does not run in new document
 
Join Date: Mar 2016
Posts: 37
highrise955 is on a distinguished road
Default

Quote:
Originally Posted by macropod View Post
Your template should have code to trigger the userform via both a Document_New macro and a Document_Open macro.



So obvious in hindsight. But...it usually is. Thanks a lot macropod. It worked perfectly.

I just did a copy and paste of my "Open" code and changed it to "New" as shown below. Was this the proper way of doing it?

Quote:
Private Sub Document_Open()
Dim oCC As ContentControl
Set oCC = ActiveDocument.SelectContentControlsByTitle("Your Name").Item(1)
oCC.Type = wdContentControlText
oCC.Range.Text = Application.UserName
oCC.Type = wdContentControlDropdownList
Document_ContentControlOnExit oCC, False
Load DrawingNumberEntryForm
DrawingNumberEntryForm.Show
lbl_Exit:
Exit Sub
End Sub

Private Sub Document_New()
Dim oCC As ContentControl
Set oCC = ActiveDocument.SelectContentControlsByTitle("Your Name").Item(1)
oCC.Type = wdContentControlText
oCC.Range.Text = Application.UserName
oCC.Type = wdContentControlDropdownList
Document_ContentControlOnExit oCC, False
Load DrawingNumberEntryForm
DrawingNumberEntryForm.Show
lbl_Exit:
Exit Sub
End Sub
Reply With Quote
  #4  
Old 03-06-2016, 04:46 PM
macropod's Avatar
macropod macropod is offline VBA Code in template does not run in new document Windows 7 64bit VBA Code in template does not run in new document Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,963
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

I'd be inclined to reduce the code redundancies thus:
Code:
Private Sub Document_Open()
Call InitializeDocument
End Sub
 
Private Sub Document_New()
Call InitializeDocument
End Sub
 
Sub InitializeDocument()
Dim oCC As ContentControl
Set oCC = ActiveDocument.SelectContentControlsByTitle("Your Name").Item(1)
With oCC
  .Type = wdContentControlText
  .Range.Text = Application.UserName
  .Type = wdContentControlDropdownList
End With
Document_ContentControlOnExit oCC, False
Load DrawingNumberEntryForm
DrawingNumberEntryForm.Show
End Sub
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #5  
Old 03-06-2016, 04:48 PM
highrise955 highrise955 is offline VBA Code in template does not run in new document Windows 10 VBA Code in template does not run in new document Office 2013
Advanced Beginner
VBA Code in template does not run in new document
 
Join Date: Mar 2016
Posts: 37
highrise955 is on a distinguished road
Default

Thanks for the input! I made the changes as you suggested.

Have a great week.
Reply With Quote
Reply

Tags
template, vba code



Similar Threads
Thread Thread Starter Forum Replies Last Post
Vba code to save document as pdf using document property text and rename folder. staicumihai Word VBA 1 12-21-2015 07:39 AM
VBA Code in template does not run in new document Visual basic code for Word 2003 template KateAus Word VBA 4 09-09-2012 08:40 PM
Creating document from template but macros are still linked to template webharvest Word 0 07-16-2011 09:34 AM
Update Document Template when Saving the Document File bjbercaw Word 3 11-16-2010 02:03 PM
how can I add a peace of code to MS document amlife Word VBA 0 03-03-2010 03:35 PM

Other Forums: Access Forums

All times are GMT -7. The time now is 07:34 AM.


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