how to retrieve the name of an attached template via VBA when the template path has changed - vba

I keep the template for the files I am working with in a dropbox and when I change to a computer with a different dropbox path, the template needs to be re-attached to the document. I would like to do this with a macro.
I can still see the name of the originally attached template in the "Templates and Add-Ins" Window, so the information must be somewhere stored within the document.
If however I try to retrieve the Name of the previously attached template via VBA by writing
strTemplate = ActiveDocument.AttachedTemplate
the result will be "Normal.dotm" and not "MyTemplate.dotm", which actually makes a lot of sense, but isn't what I am looking for.
Is there any way to retrieve the name and path of the previously attached template?

I think you can get the ful name (path+name) from
Application.Dialogs(wdDialogToolsTemplates).Template
and the name alone from
ActiveDocument.BuiltinProperties(wdPropertyTemplate)
The first time I tried the first of those, it didn't seem to work, but I could have been checking the wrong document. You won't find the Template property documented under the Dialog object - it's documented here
If those do not work, the only other ways I can see to do it would be
a. to look inside the .docx while it is closed (for a .docx), or maybe (I haven't checked) use dsofile.dll for a .doc, or
b. insert the field { template \p }, execute it and retrieve its result text

I found the answer here on stack overflow (unfortunately only after posting my question) as an answer to "How to open a word document without resolving the attached template?":
strOldTemplate = Dialogs(wdDialogDocumentStatistics).Template

Related

Where best to store a version number in Word document?

I have a VSTO Add-In that fills a specific Word template document with data from a third party software. The trick here is that some changes in the script are connected to changes in the document. What I want to do now is to manually store some kind of version number in the document information so I can check in the script if the version of the document fits the version of the script.
I already took a look here but there are too many possibilities and I don't just want to pick any random object. I need to store it somewhere where it doesn't get changed.
Where would you recommend to store such a value?
edit: the version number has to be set manually because not every change of the document affects the script.
You have three options:
Document property - can be displayed in a document by using a DocProperty field but can be viewed and easily changed by the user.
Document variable - an old school method but still valid. Can be displayed in a document by using a DocVariable field but can only be added, or the value changed, by using code.
Custom XML Part - can be displayed in a document by mapping to a content control which can allow the value to be changed by the user. If not mapped can only be changed using code, but not as simply as a document variable.

word-vba: Unable to update linked image link

I have a document with two inlineshapes into a table on the document header. These inlineshapes are linked images. The folder containing these images has moved and I'm unable to change the image link by VBA code.
I'm working with Word 2016. The documents were created on Word 2003. The only way to access the images has been exploring the table cells and finding the inlineshapes into them
The inlineshapes have no field object defined (nothing)
I can update the link manually going into the File menu > Edit File Links...
I would like to be able to change the image links with vba and not having to do it manually
Well, at last I found what happened.
The images have to be accessed as inlineshapes, not as fields
the path to the image is stored in the property InlineShape.LinkFormat.SourceFullName
Changing this property value and then updating the inlineshape it worked
Sorry if I've bothered you but I think it can be helpful to other users

Search Contents of Word Documents without opening the Doc

Is there a way (in VB.net/VBA) to search for a string in a .doc file without physically opening it?
The question may sound silly but I have code that searches 100's of documents at a time while opening them all up one-by-one (using For Each, FileSystemObjectand oWord.Document.Open(Filename.doc) and it takes ages.
I noticed that when you search for a document using the search box in Windows Explorer, the results are returned having found your search term in the TITLE of the document as well as the the CONTENTS of the document within the search window. The results where the search term was found INSIDE the document appears as a sort of text preview of all the text inside that document.I could be wrong but I read somewhere that all word documents contain some sort of xml file embedded within that contains (among other things) the text from the document itself.
So my question is, if Windows Explorer can do it without having to open a word process, can the same be done in code (VBA or VB.NET)to find a particular word inside a document?
Hope that makes sense,
Thanks.

Word Automation Service breaks links in table of contents

I have written a code which utilizes Word Automation Service in order to convert the .DOCX file to the .PDF. I have noticed that in case the Word document contains a table of contents, its links are removed in the PDF. This is very bad for my business case.
On the other hand, manually opening MS Word and saving the same document as PDF preserves the links in the table of contents. This is the behavior I am looking for, but I want to keep my code independent form having MS Office Word installed on the machine running my code.
Has anyone had the similar issue and was anybody able to resolve it?
In my case, i found out that this is something related to Job Settings property. Try to comment or remove this line of code if you have one:
jobSettings.UpdateFields = true;

How to get the template path of a word 2003 document

When I create a new document based on a template (*.dot), I need to know on which template is the document based on.
Is there a way to find out with VBA which template was used for creating this new document? I need the complete path to the template.
ActiveDocument.AttachedTemplate.FullName
You may also use a built-in document property to access the template name: ActiveDocument.BuiltInDocumentProperties(wdPropertyTemplate)
I don't remember why theres has been sometimes differences between the two results; you must try it out. please be aware that accessing sometimes the built-in properties sets the document in a dirty state, so that it is useful to save the ActiveDocument.Saved state before and reset it after having accessed the property.
[UPDATE]
I've had again a look into the way you're creating the documents. If it's at your customer with 38.000 templates, I guess your problem are "fake" templates. I've just did a test with Office 2003:
Create a new document "TestTemplate.doc" with same content and save it as doc file. Close it.
Go to Windows Explorer and rename the document to "TestTemplate.dot". This provokes Windows Explorer to treat it as a template, not as a document. The default DDE command for templates is not "OPEN", but "NEW", what you can also see if you do a right-click on the file ("New" is bold, while with documents "Open" is bold).
Double-click the fake template: Word creates a copy of the document, so a new file named "Document2" or whatsoever.
Go to the VBA Editor, and type ?ActiveDocument.AttachedTemplate, and you'll see "Normal" as answer. Type ?activedocument.Type = wdTypeTemplate and you'll see "False" as answer.
Sp I guess the documents "Without template" are only copies of other documents, and not of templates. So you have no way to find the base template.