We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

How to remove template reference from a Word?

Hi,

We are using the Aspose.Word in 15.9.0 with a supported license.

We have some Word document that contains Macro and missing template reference.
This produce an error in our own workflow for Word processing, so we try to remove this reference thanks to Aspose.word. But we could not manage to do it.

We see the reference thanks to the Document.getAttachedTemplate() method, which returns the Template path.
For example: C:\Users\foo\Temp\MyTemplate.dot

So we tried to use the Document.setAttachedTemplate("") method to remove it.
The value is removed (in another tool, we check the attachedTemplate value and it is the empty string), but the Template is still visible inside Word (and our workflow process still fail with the new file).

This is the whole code:
{code}
Document document = new Document(fileInputStream);
//it display the real attachedTemplate at the first execution, and empty string at the second execution
System.out.println("’"+document.getAttachedTemplate()+"’");
document.setAttachedTemplate("");
document.save(baos, SaveFormat.DOC);
{code}

Do you know a way to remove the Template and all its references in the document?

I give you in Attachment a zip file containing an word file that contains the problem (there is no content in the document), and screen of the missing template reference.

To see the problem in Word in the provided file:
Open it with Word, and Activate Macro/modification.
Go to Developer Tab and click on Visual Basic.
In the next Window, go to Tools>References, and you will see the term “MISSING: SpellcheckSample.dot” (as in the screenshot).

Regards.

Hi Mantovan,


Thanks for your inquiry. We have logged your requirement in our issue tracking system. The ID of this issue is WORDSNET-13678. Our product team will further look into the details of this problem and we will keep you updated on the status of this issue. We apologize for your inconvenience.

Best regards,

Hi Mantovan,


It is to update you that this problem actually requires us to implement a new complex feature in Aspose.Words and we regret to share with you that implementation of this issue has been postponed for now. However, the fix of this problem may definitely come onto the product roadmap in the future. Unfortunately, we cannot currently promise a resolution date. We apologize for your inconvenience and thank you for your understanding.

Actually, this is not a document template (Document.AttachedTemplate), but this is a reference to an Automation type library or VBA project.

Inside VBA file, this is represented as PROJECTREFERENCES record. The details can be found in spec [MS-OVBA] 2.3.4.2.2 PROJECTREFERENCES Record and 2.3.4.2.2.1 REFERENCE Record.

There is a way to remove a broken reference from VBA Project using VBA macros in document:
<span style=“font-family: Consolas, “Bitstream Vera Sans Mono”, “Courier New”, Courier, monospace !important;”>Public Sub RemoveBrokenReference()
 
<span style=“font-family: Consolas, “Bitstream Vera Sans Mono”, “Courier New”, Courier, monospace !important;”>    Dim project As VBIDE.VBProject
<span style=“font-family: Consolas, “Bitstream Vera Sans Mono”, “Courier New”, Courier, monospace !important;”>    Dim ref As VBIDE.Reference
<span style=“font-family: Consolas, “Bitstream Vera Sans Mono”, “Courier New”, Courier, monospace !important;”>    
<span style=“font-family: Consolas, “Bitstream Vera Sans Mono”, “Courier New”, Courier, monospace !important;”>    Set project = ActiveDocument.VBProject
<span style=“font-family: Consolas, “Bitstream Vera Sans Mono”, “Courier New”, Courier, monospace !important;”>    
<span style=“font-family: Consolas, “Bitstream Vera Sans Mono”, “Courier New”, Courier, monospace !important;”>    For Each ref In project.References
<span style=“font-family: Consolas, “Bitstream Vera Sans Mono”, “Courier New”, Courier, monospace !important;”>     If ref.IsBroken Then
<span style=“font-family: Consolas, “Bitstream Vera Sans Mono”, “Courier New”, Courier, monospace !important;”>       project.References.Remove ref
<span style=“font-family: Consolas, “Bitstream Vera Sans Mono”, “Courier New”, Courier, monospace !important;”>     End If
<span style=“font-family: Consolas, “Bitstream Vera Sans Mono”, “Courier New”, Courier, monospace !important;”>    Next
 
<span style=“font-family: Consolas, “Bitstream Vera Sans Mono”, “Courier New”, Courier, monospace !important;”>End Sub

Unfortunately, it requires to set manually “Trust Access To Visual Basic Project” in security settings and to add a reference to “Microsoft Visual Basic for Applications Extensibility” object.
The details can be found in the two following articles:
https://support.microsoft.com/en-us/kb/308340
http://stackoverflow.com/questions/9879825/how-to-add-a-reference-programmatically
Hence, to solve this problem we need to build VBA parser that will allow us to remove ‘REFERENCE Record’ from a binary VBA file.

Best regards,

The issues you have found earlier (filed as WORDSNET-13678) have been fixed in this Aspose.Words for .NET 20.9 update and this Aspose.Words for Java 20.9 update.