Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 12-13-2018, 07:01 PM
Yong Xiang Yong Xiang is offline How do I code bookmark globally for different template? Windows 7 32bit How do I code bookmark globally for different template? Office 2016
Novice
How do I code bookmark globally for different template?
 
Join Date: Dec 2018
Posts: 9
Yong Xiang is on a distinguished road
Default How do I code bookmark globally for different template?


Hello, I'm having trouble referencing from a different template.
What I encounter is I need to open a bookmarked template and the rest of other
the template will reference from the main bookmarked. If not error will appear as there isn't any place to refer to.

My coding is okay but is there a way to globally bookmark so that whatever another template I throw in will automatically reference from it?



PHP Code:
My Code :
Sub Coding()
'
'
Coding Macro

End Sub

Sub Add_HT_PD
()

' Ensure style normal paragraph spacing is set to 0 before 0 after and single spacing
Location of the template is at the desktop
' Copying QPM HT PD

    ChangeFileOpenDirectory "C:\Users\YongX\Desktop"
    Selection.InsertFile FileName:="HT PD.docx", Range:="", _
        ConfirmConversions:=False, Link:=False, Attachment:=False
End Sub

Sub Add_HT_US()
'
' Copying HT US

    ChangeFileOpenDirectory "C:\Users\YongX\Desktop"
    Selection.InsertFile FileName:="HT US.docx", Range:="", _
        ConfirmConversions:=False, Link:=False, Attachment:=False
End Sub

Sub Add_TF_PD()
'
' Copying TF PD

    ChangeFileOpenDirectory "C:\Users\YongX\Desktop"
    Selection.InsertFile FileName:="TF PD.docx", Range:="", _
        ConfirmConversions:=False, Link:=False, Attachment:=False
End Sub

Sub Add_TF_US()
'
' Copying TF US

    ChangeFileOpenDirectory "C:\Users\YongX\Desktop"
    Selection.InsertFile FileName:="TF US.docx", Range:="", _
        ConfirmConversions:=False, Link:=False, Attachment:=False
    Selection.TypeBackspace
End Sub

Sub Add_HT()
'
' Copying TF US

    ChangeFileOpenDirectory "C:\Users\YongX\Desktop"
    Selection.InsertFile FileName:="HT.docx", Range:="", _
        ConfirmConversions:=False, Link:=False, Attachment:=False
    Selection.TypeBackspace
End Sub

Sub Add_LT()
'
' Copying TF US

    ChangeFileOpenDirectory "C:\Users\YongX\Desktop"
    Selection.InsertFile FileName:="LT.docx", Range:="", _
        ConfirmConversions:=False, Link:=False, Attachment:=False
    Selection.TypeBackspace
End Sub

Sub Update_Fields()

Application.ScreenUpdating = False
ActiveDocument.PrintPreview
ActiveDocument.ClosePrintPreview
Application.ScreenUpdating = True

End Sub 

Last edited by Yong Xiang; 12-13-2018 at 07:06 PM. Reason: Not to reveal Personal information
Reply With Quote
  #2  
Old 12-13-2018, 08:01 PM
Guessed's Avatar
Guessed Guessed is offline How do I code bookmark globally for different template? Windows 10 How do I code bookmark globally for different template? Office 2016
Expert
 
Join Date: Mar 2010
Location: Canberra/Melbourne Australia
Posts: 3,977
Guessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant future
Default

Your terminology is not good. Bookmarks have a specific meaning in Word and you are using this word in a way that doesn't make sense.

It appears you are wanting to store the path to a specific folder. That can be done either by creating a constant at the top of the module or by using a function that each macro calls. The benefit of the function approach is that the code could then be made to adapt to the relative path (ie the files are always in the desktop folder but that actual path will vary for each user). Using a hardcoded constant, the code could be written as follows.
Code:
Function InsertAFile(sFile As String)
  Dim sPath as String
  sPath = "C:\Users\YongX\Desktop\"
  Selection.InsertFile FileName:=sPath & sFile, ConfirmConversions:=False, Link:=False, Attachment:=False
  Selection.TypeBackspace
End Function

Sub Add_HT_PD()
  InsertAFile "HT PD.docx"
End Sub

Sub Add_HT_US()
  InsertAFile "HT US.docx"
End Sub

Sub Add_TF_PD()
  InsertAFile "TF PD.docx"
End Sub

Sub Add_TF_US()
  InsertAFile "TF US.docx"
End Sub

Sub Add_HT()
  InsertAFile "HT.docx"
End Sub

Sub Add_LT()
  InsertAFile "LT.docx"
End Sub

Sub Update_Fields()
  Application.ScreenUpdating = False
  ActiveDocument.PrintPreview
  ActiveDocument.ClosePrintPreview
  Application.ScreenUpdating = True
End Sub
__________________
Andrew Lockton
Chrysalis Design, Melbourne Australia
Reply With Quote
  #3  
Old 12-13-2018, 08:43 PM
Yong Xiang Yong Xiang is offline How do I code bookmark globally for different template? Windows 7 32bit How do I code bookmark globally for different template? Office 2016
Novice
How do I code bookmark globally for different template?
 
Join Date: Dec 2018
Posts: 9
Yong Xiang is on a distinguished road
Default

Thanks, I'm still new to VBA didn't know that I can set a hardcoded constant like this.
I have tried your code but there is one error saying that couldn't find my file? Runtime error '5174'
at Selection.InsertFile FileName:=sPath & sFile, ConfirmConversions:=False,

code :
PHP Code:
Function InsertAFile(sFile As String)
  
Dim sPath as String
  sPath 
"C:\Users\YongX\Desktop\"
  Selection.InsertFile FileName:=sPath & sFile, ConfirmConversions:=False, 
  Link:=False, Attachment:=False
  Selection.TypeBackspace
End Function

Sub Add_HT_PD()
  InsertAFile "
HT PD.docx"
End Sub

Sub Add_HT_US()
  InsertAFile "
HT US.docx"
End Sub

Sub Add_TF_PD()
  InsertAFile "
TF PD.docx"
End Sub

Sub Add_TF_US()
  InsertAFile "
TF US.docx"
End Sub

Sub Add_HT()
  InsertAFile "
HT.docx"
End Sub

Sub Add_LT()
  InsertAFile "
LT.docx"
End Sub

Sub Update_Fields()
  Application.ScreenUpdating = False
  ActiveDocument.PrintPreview
  ActiveDocument.ClosePrintPreview
  Application.ScreenUpdating = True
End Sub 
Reply With Quote
  #4  
Old 12-13-2018, 09:20 PM
Guessed's Avatar
Guessed Guessed is offline How do I code bookmark globally for different template? Windows 10 How do I code bookmark globally for different template? Office 2016
Expert
 
Join Date: Mar 2010
Location: Canberra/Melbourne Australia
Posts: 3,977
Guessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant future
Default

That would imply that either the path or filename is wrong. The following code should replace the function I previously supplied. In this one, we are dynamically working out the path to the Desktop rather than hard coding it (which will allow it to work on other machines). It also returns a message if the passed in file doesn't exist at that location.
Code:
Function InsertAFile(sFile As String)
  Dim sPath As String
  sPath = Environ("USERPROFILE") & "\Desktop\"
  If FileExists(sPath & sFile) Then
    Selection.InsertFile FileName:=sPath & sFile, ConfirmConversions:=False, Link:=False, Attachment:=False
    Selection.TypeBackspace
  Else
    MsgBox "File doesn't exist: " & vbCr & sPath & sFile, vbOKOnly, "Missing file"
  End If
End Function

Function FileExists(sFilePath As String) As Boolean
  Dim FSO As Object
  Set FSO = CreateObject("Scripting.FileSystemObject")
  FileExists = FSO.FileExists(sFilePath)
End Function
__________________
Andrew Lockton
Chrysalis Design, Melbourne Australia
Reply With Quote
  #5  
Old 12-14-2018, 12:30 AM
Yong Xiang Yong Xiang is offline How do I code bookmark globally for different template? Windows 7 32bit How do I code bookmark globally for different template? Office 2016
Novice
How do I code bookmark globally for different template?
 
Join Date: Dec 2018
Posts: 9
Yong Xiang is on a distinguished road
Default

I deleted my file and downloaded the same file and place the location of the file at a different place.
I named the pathway accordingly.
However, I still not able to use your code. I'm not sure what went wrong but one thing i know Msg box works and keeps saying file doesn't exist.
Attached Images
File Type: jpg Msg.JPG (23.0 KB, 32 views)
Reply With Quote
  #6  
Old 12-14-2018, 12:41 AM
Guessed's Avatar
Guessed Guessed is offline How do I code bookmark globally for different template? Windows 10 How do I code bookmark globally for different template? Office 2016
Expert
 
Join Date: Mar 2010
Location: Canberra/Melbourne Australia
Posts: 3,977
Guessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant future
Default

Have a look at the msgbox. It says the code is looking for an invalid path because it includes the C:\Users\YongX twice and it is also looking inside a folder on the desktop which is not what was previously specified.

Have a closer look at the code you are trying to implement and work out where those two parts get added together. We aren't trying to do your work for you - you have to make an effort to understand the code supplied a little bit too.

Step through the code one line at a time by pressing F8. Hover your mouse over the variables as the lines execute and pay attention to the values that those string variables have as the macro progresses. When you notice a problem, stop the code and fix it before trying again.
__________________
Andrew Lockton
Chrysalis Design, Melbourne Australia
Reply With Quote
  #7  
Old 12-16-2018, 08:27 PM
Yong Xiang Yong Xiang is offline How do I code bookmark globally for different template? Windows 7 32bit How do I code bookmark globally for different template? Office 2016
Novice
How do I code bookmark globally for different template?
 
Join Date: Dec 2018
Posts: 9
Yong Xiang is on a distinguished road
Default

Sorry for really late replies. And thank you so much!! It works so well, and if you don't mind do you know any coding function to declare bookmark and referencing?
Reply With Quote
  #8  
Old 12-16-2018, 10:45 PM
Guessed's Avatar
Guessed Guessed is offline How do I code bookmark globally for different template? Windows 10 How do I code bookmark globally for different template? Office 2016
Expert
 
Join Date: Mar 2010
Location: Canberra/Melbourne Australia
Posts: 3,977
Guessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant future
Default

You need to explain better what it is that you need. I don't understand what 'declare bookmark and referencing' is asking.

You must have changed the code I supplied in order to generate the error that you did so you might also need to post the code you are actually using.
__________________
Andrew Lockton
Chrysalis Design, Melbourne Australia
Reply With Quote
  #9  
Old 12-19-2018, 08:13 PM
Yong Xiang Yong Xiang is offline How do I code bookmark globally for different template? Windows 7 32bit How do I code bookmark globally for different template? Office 2016
Novice
How do I code bookmark globally for different template?
 
Join Date: Dec 2018
Posts: 9
Yong Xiang is on a distinguished road
Default

What I'm doing is when I open a new word document, I insert a template which I bookmarked using the code below and it works. Only the First template works but when I open the other few templates to reference from the First template it won't work

Basically, I want to link all dates, location, address to be the same in every page of a few different templates.


Userform Code:

PHP Code:
Private Sub CancelBut_Click()
QPMTesting.Hide

'Close the Userform

End Sub


Private Sub OKbut_Click()
    Dim Address As Range
    Dim Location As Range
    Dim HT_Type As Range
    Dim Time As Range
    Dim Weather As Range
    Dim TestDate As Range
    
'
Declaration
    
    Set Address 
ActiveDocument.Bookmarks("Customer").Range
    Set Location 
ActiveDocument.Bookmarks("HT_Location").Range
    Set HT_Type 
ActiveDocument.Bookmarks("HT_Type").Range
    Set Time 
ActiveDocument.Bookmarks("HT_Install_Date").Range
    Set Weather 
ActiveDocument.Bookmarks("Weather").Range
    Set TestDate 
ActiveDocument.Bookmarks("Date").Range

'Set declaration as an active bookmark

    Address.Text = Me.TextBox1.Text
    Location.Text = Me.TextBox2.Text
    HT_Type.Text = Me.TextBox3.Text
    Time.Text = Me.TextBox4.Value
    Weather.Text = Me.TextBox5.Text
    TestDate.Text = Me.TextBox6.Value
    
'
Set the text to be text or value
    
    Address
.Font.AllCaps True
    Location
.Font.AllCaps True
    HT_Type
.Font.AllCaps True
    Weather
.Font.AllCaps True

'Set Caps for all

    Me.Repaint

'
Refresh

    QPMTesting
.Hide

'Close the Userform
    
End Sub


Private Sub UserForm_Click()

End Sub 
Document Code:
PHP Code:
Private Sub Document_Open()
    
QPMTesting.Show
End Sub 
Reply With Quote
  #10  
Old 12-19-2018, 09:43 PM
gmayor's Avatar
gmayor gmayor is offline How do I code bookmark globally for different template? Windows 10 How do I code bookmark globally for different template? Office 2016
Expert
 
Join Date: Aug 2014
Posts: 4,105
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 of
Default

Your use of terminology is confusing. It seems that you are inserting files into a document that have common bookmark names. Bookmark names must be unique so if you insert a second document with the same bookmarks as the first, something has to give. The only way to do this is to delete the bookmarks in the target document before inserting the next document.

It would be better to use content controls than bookmarks as these can have duplicated names, then loop through each content control with the same name and apply the values The following will loop through all the controls in the document body (for controls in other story ranges you will have to loop through those also):
Code:
Dim OCC As ContentControl
    For Each OCC In ActiveDocument.ContentControls
        Select Case OCC.Title
            Case "Address"
                OCC.Range.Text = TextBox1.Text
            Case "Location"
                OCC.Range.Text = TextBox2.Text
            Case "HT_Type"
                OCC.Range.Text = TextBox3.Text
            Case "Time"
                OCC.Range.Text = TextBox4.value
            Case "Weather"
                OCC.Range.Text = TextBox5.Text
            Case "TestDate"
                OCC.Range.Text = TextBox6.value
        End Select
    Next OCC
     Set OCC = Nothing
You may find http://www.gmayor.com/insert_content_control_addin.htm useful for inserting and maintaining the controls.
Or you could use the following macro to convert your bookmarks to controls (with the same proviso about story ranges - I may add this to the aforementioned add-in later)
Code:
Sub ReplaceBookmarkWithCC()
'Graham Mayor - https://www.gmayor.com - Last updated - 20 Dec 2018
Dim oBM As Bookmark
Dim oRng As Range
Dim oCC As ContentControl
Dim strTitle As String
    For Each oBM In ActiveDocument.Bookmarks
        strTitle = oBM.Name
        Set oRng = oBM.Range
        oBM.Delete
        Set oCC = oRng.ContentControls.Add(wdContentControlText)
        With oCC
            .Range.Text = oRng.Text
            .Tag = strTitle
            .Title = .Tag
            .MultiLine = True
            .SetPlaceholderText , , "Click or tap here to enter text."
            .LockContentControl = True
        End With
    Next oBM
    MsgBox "Replacements complete"
lbl_Exit:
    Set oCC = Nothing
    Set oRng = Nothing
    Set oBM = 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
  #11  
Old 12-19-2018, 10:27 PM
Guessed's Avatar
Guessed Guessed is offline How do I code bookmark globally for different template? Windows 10 How do I code bookmark globally for different template? Office 2016
Expert
 
Join Date: Mar 2010
Location: Canberra/Melbourne Australia
Posts: 3,977
Guessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant future
Default

You've gone off on a new tack. That code is not related to any external file source data or file paths that you've been talking about previously.

I would be doing what you describe you want in a very different way. I would use an embedded xml file and then use linked Content Controls to display the related data. For each 'template' I would set up a single Word template which contains a series of building blocks - one for each document type. You would then insert one of the building blocks (to populate the xml file) and could then replace the content with any of the related Building Blocks to create each of the document instances.

See https://gregmaxey.com/word_tip_pages...rol_tools.html for a different way of setting up the file.
__________________
Andrew Lockton
Chrysalis Design, Melbourne Australia

Last edited by Guessed; 12-19-2018 at 10:28 PM. Reason: I see Graham has the same idea...
Reply With Quote
  #12  
Old 12-20-2018, 02:44 AM
Yong Xiang Yong Xiang is offline How do I code bookmark globally for different template? Windows 7 32bit How do I code bookmark globally for different template? Office 2016
Novice
How do I code bookmark globally for different template?
 
Join Date: Dec 2018
Posts: 9
Yong Xiang is on a distinguished road
Default

Thank you for taking your time Gmayor I don't think this coding help me or simply I don't understand cause I'm new towards VBA word. Sorry about that.

Thank you Guessed too! You have been helping me all the way. I took your idea and custom make XML file for both templates to try it out. I followed the script on this website: https://gregmaxey.com/word_tip_pages...word_2013.html
I Followed this mapping content: https://www.youtube.com/watchv=OtFhIqK0gec&t=459s

And now I do not know how to linked Content Controls between all the templates.

Xml:
PHP Code:
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<DemoXMLNode xmlns="HT">
<Customer></Customer>
<HT_Install_Date></HT_Install_Date>
<HT_Location></HT_Location>
<HT_Type></HT_Type>
<Weather></Weather>
<Date></Date>
</DemoXMLNode>
Reply With Quote
  #13  
Old 12-20-2018, 04:39 PM
Guessed's Avatar
Guessed Guessed is offline How do I code bookmark globally for different template? Windows 10 How do I code bookmark globally for different template? Office 2016
Expert
 
Join Date: Mar 2010
Location: Canberra/Melbourne Australia
Posts: 3,977
Guessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant future
Default

You can use VBA code to embed the XML file into any Word document. That would require you to populate the customer-specific values in the xml before embedding that file into each document. This is not particularly user friendly and doesn't make for an elegant workflow.

I would approach this differently to give you a better workflow. If your template contains a series of building blocks containing each of your document 'templates' then you can populate any one of the documents and then do a SaveAs before replacing the contents of the entire file with a different building block. See the attached template as a working example of this where I have saved three variant documents as building blocks. You can populate any of these variants and change variant to see the same info in different layouts. This template doesn't need any coding at all and could have been saved as a dotx and work exactly the same.
Attached Files
File Type: dotm Sample Linked XML Template.dotm (114.5 KB, 15 views)
__________________
Andrew Lockton
Chrysalis Design, Melbourne Australia
Reply With Quote
  #14  
Old 12-20-2018, 07:37 PM
Yong Xiang Yong Xiang is offline How do I code bookmark globally for different template? Windows 7 32bit How do I code bookmark globally for different template? Office 2016
Novice
How do I code bookmark globally for different template?
 
Join Date: Dec 2018
Posts: 9
Yong Xiang is on a distinguished road
Default

It works when I put every template together and the same XML mapping location. But when I saved separately, XML mapping is always not there. Is there a way to autorun XML mapping at the start?

As for the Modify Building Block, I tried creating a dotx. But there isn't any option form my dotx.
Attached Images
File Type: jpg XML Mapping.JPG (19.1 KB, 24 views)
File Type: jpg Modify Building Block.JPG (42.0 KB, 24 views)
Reply With Quote
  #15  
Old 12-23-2018, 04:41 PM
Guessed's Avatar
Guessed Guessed is offline How do I code bookmark globally for different template? Windows 10 How do I code bookmark globally for different template? Office 2016
Expert
 
Join Date: Mar 2010
Location: Canberra/Melbourne Australia
Posts: 3,977
Guessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant future
Default

You need to save the template into your User Templates or Workgroup Templates folder. The User Templates location is %appdata%\Microsoft\Templates

You then create a new document from the template and select it all and go to that same Modify Building Block dialog and Save in the template.
__________________
Andrew Lockton
Chrysalis Design, Melbourne Australia
Reply With Quote
Reply



Similar Threads
Thread Thread Starter Forum Replies Last Post
How do I code bookmark globally for different template? Field Code: Show a value only if bookmark is not empty Cosmo Word 7 08-24-2018 01:46 PM
How do I code bookmark globally for different template? VBA Code to Access Building Block Template in Word 2013 Startup folder rdross51 Word VBA 2 06-01-2016 07:05 PM
Find Bookmark, move to bookmark, execute code, repeat raymm3852 Word VBA 10 04-15-2016 06:21 PM
How do I code bookmark globally for different template? VBA Code in template does not run in new document highrise955 Word VBA 4 03-06-2016 04:48 PM
How do I code bookmark globally for different template? Visual basic code for Word 2003 template KateAus Word VBA 4 09-09-2012 08:40 PM

Other Forums: Access Forums

All times are GMT -7. The time now is 12:20 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