Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 01-30-2017, 07:12 PM
ferocious12 ferocious12 is offline Macro for creating word docs from existing template Windows 7 64bit Macro for creating word docs from existing template Office 2016
Novice
Macro for creating word docs from existing template
 
Join Date: Jan 2017
Posts: 6
ferocious12 is on a distinguished road
Default Macro for creating word docs from existing template

Hi,



1. I have word template doc that need to be tailored,
2. to include specific paragraphs based on applicable case

for example:

[paragraph to include if colour is Blue] Blue is good color
[paragraph to include if color is yellow] Yellow is bright colour

3. can you please propose what is my best option to create a doc out of this template
4. How can I choose a colour (blue or yellow) so that paragraphs associated with other choice (not selected) is deleted
5. Or paragraphs associated with the color blue or yellow are coded that only the paragraph associated with chosen color appears in the final doc

Thanks
Reply With Quote
  #2  
Old 01-30-2017, 10:00 PM
gmayor's Avatar
gmayor gmayor is offline Macro for creating word docs from existing template Windows 10 Macro for creating word docs from existing template Office 2016
Expert
 
Join Date: Aug 2014
Posts: 4,137
gmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud of
Default

It would be better to put the paragraphs into the document as required, rather than delete them when they are not. How you store and insert the paragraph texts rather depends on how many of them there are and how they are located in the finished document, and whether they are alternatives at the same location or separate, but bookmarks associated with autotexts stored in the template are an obvious possibility, but not the only one.

Where does the colour information, that drives the process, come from?

You can call the following macro, as required, to insert the autotexts

Code:
Public Sub BBToBM(strBMName As String, strTemplate As String, strBBName As String)
'Graham Mayor - http://www.gmayor.com
'strBMName is the name of the bookmark to fill
'strTemplate is the full path of the template that stores the building block
'Use ThisDocumment.FullName if the template is the template with this macro
'strBBName is the name of the building block to insert
Dim oRng As Range
Dim iLen1 As Integer, iLen2 As Integer
    With ActiveDocument
        iLen1 = Len(ActiveDocument.Range)
        On Error GoTo lbl_Exit
        Set oRng = .Bookmarks(strBMName).Range
        Application.Templates(strTemplate). _
                BuildingBlockEntries(strBBName).Insert _
                Where:=oRng, _
                RichText:=True
        iLen2 = Len(ActiveDocument.Range)
        oRng.End = oRng.End + (iLen2 - iLen1)
        oRng.Bookmarks.Add strBMName
    End With
lbl_Exit:
    Set oRng = Nothing
    Exit Sub
End Sub
__________________
Graham Mayor - MS MVP (Word) (2002-2019)
Visit my web site for more programming tips and ready made processes www.gmayor.com
Reply With Quote
  #3  
Old 01-31-2017, 04:58 PM
ferocious12 ferocious12 is offline Macro for creating word docs from existing template Windows 7 64bit Macro for creating word docs from existing template Office 2016
Novice
Macro for creating word docs from existing template
 
Join Date: Jan 2017
Posts: 6
ferocious12 is on a distinguished road
Default sample document attached

Many thanks. I have attached a sample doc from which I have to create a doc based on choices highlighted in yellow.

I have to make two type of selections one is to delete a certain word ("recent" in the attached template) if the communication is not recent.

Second I have to select a paragraph based on the addressee say Company, Partnership etc.

This is just a simple example but I have to make a lot of paragraph choices based on a criteria. Can I create a form like thing that will just include the paragraph based on a selection from a drop down menu & that drop down just disappear in the final doc ?

Thanks again for the help !
Attached Files
File Type: docx template.docx (11.9 KB, 13 views)
Reply With Quote
  #4  
Old 01-31-2017, 11:15 PM
gmayor's Avatar
gmayor gmayor is offline Macro for creating word docs from existing template Windows 10 Macro for creating word docs from existing template Office 2016
Expert
 
Join Date: Aug 2014
Posts: 4,137
gmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud of
Default

For a document like this I would create a userform to collect the information and write the appropriate values to bookmarks set in the template. At its simplest see the attached which you can use as the basis for your template project. Create a new document from the template and allow the macros to run.
Attached Files
File Type: dotm Example Template.dotm (31.3 KB, 18 views)
__________________
Graham Mayor - MS MVP (Word) (2002-2019)
Visit my web site for more programming tips and ready made processes www.gmayor.com
Reply With Quote
  #5  
Old 02-04-2017, 06:27 PM
ferocious12 ferocious12 is offline Macro for creating word docs from existing template Windows 7 64bit Macro for creating word docs from existing template Office 2016
Novice
Macro for creating word docs from existing template
 
Join Date: Jan 2017
Posts: 6
ferocious12 is on a distinguished road
Default Very helpful

Many thanks !! this is super helpful. In reality I have a template which is around 12 pages that will require 7-8 Combo boxes + form that is segregated in 3 or 4 pages.

I tried to apply your model but I am new to VBA. What is my best bet here.
Reply With Quote
  #6  
Old 02-04-2017, 09:57 PM
gmayor's Avatar
gmayor gmayor is offline Macro for creating word docs from existing template Windows 10 Macro for creating word docs from existing template Office 2016
Expert
 
Join Date: Aug 2014
Posts: 4,137
gmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud of
Default

I prepared the example so it would be easy to follow and expand upon. Unless you learn more VBA you will have to get someone to do it for you. If you want to discuss rates, contact me via my web site.
__________________
Graham Mayor - MS MVP (Word) (2002-2019)
Visit my web site for more programming tips and ready made processes www.gmayor.com
Reply With Quote
Reply

Tags
template drop-down, vba in microsoft word



Similar Threads
Thread Thread Starter Forum Replies Last Post
Macro for creating word docs from existing template Creating a new Table template based on existing table Cadmiumcadamium Word Tables 1 08-14-2016 08:58 PM
Macro for creating word docs from existing template Creating a Template using a Macro to fill in various information brojames Word 7 11-09-2015 02:51 PM
Creating a new template for existing document? Stephen0352 Word 6 09-12-2011 09:00 AM
How do you update existing Outlook calendar item from Word with macro? Joe Patrick Word VBA 0 07-09-2011 05:32 AM
Creating Template in Word pusherman Word 1 12-01-2008 03:26 PM

Other Forums: Access Forums

All times are GMT -7. The time now is 04:16 AM.


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