#1
|
|||
|
|||
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:
Last edited by Yong Xiang; 12-13-2018 at 07:06 PM. Reason: Not to reveal Personal information |
#2
|
||||
|
||||
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 |
#3
|
|||
|
|||
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:
|
#4
|
||||
|
||||
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 |
#5
|
|||
|
|||
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. |
#6
|
||||
|
||||
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 |
#7
|
|||
|
|||
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?
|
#8
|
||||
|
||||
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 |
#9
|
|||
|
|||
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:
PHP Code:
|
#10
|
||||
|
||||
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 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 |
#11
|
||||
|
||||
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... |
#12
|
|||
|
|||
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:
|
#13
|
||||
|
||||
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.
__________________
Andrew Lockton Chrysalis Design, Melbourne Australia |
#14
|
|||
|
|||
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. |
#15
|
||||
|
||||
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 |
|
Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
Field Code: Show a value only if bookmark is not empty | Cosmo | Word | 7 | 08-24-2018 01:46 PM |
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 |
VBA Code in template does not run in new document | highrise955 | Word VBA | 4 | 03-06-2016 04:48 PM |
Visual basic code for Word 2003 template | KateAus | Word VBA | 4 | 09-09-2012 08:40 PM |