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

# Create Word Document & Update Author DOCPROPERTY Field using Java

Hi,
I would like to create a new word document (doc-file) based on a word template (dot-file), then to insert the contents of some other doc-files into the new doc-file using destination styles and in the end to save the target document.
In the footer of the dot-File is an “Author”-field included, where I would like to have the current user’s name replaced. I know that updating fields is not covered by Aspose, so I have added an AutoOpen Marcro in the dot-file that should update the fields every time the doc-file is opened.
For creating the doc-file and so on this I have in essence written the following code:

Document worddoc = **new** Document(dotTemplate);
...
InsertDocument using NodeImporter class with importFormatMode "USE_DESTINATION_STYLES"
...
worddoc.save(targetDoc);


dotTemplate and targetDoc are parameters for path+filename information.
Everything works fine excepting the filling of the “Author” field (other fields, e.g. for filename, etc. were filled correctly!)
The normal word behavior is that when creating a new document the “Author” field in the footer is first filled by the “name” that is inserted in the user information (Tools - Options) and is automatically copied to the Author field of the file properties.
Another fact is that the mentioned template in the file properties is normally the dot-file the doc-file was created from.
With my Aspose solution I have a different behavior:

1. the automatic transfer from the user information (name) to the file properties (author) seems not to work, because the author field in the file properties is empty and the author field in my footer the same
2. the template mentioned in the file properties is not the dot-file that I have basically used for creating the new document, but “Normal.dot”

I hope you can understand my problem!?
Is this problem known?
Best regards.
Ulrike

Hi

Thanks for your request. You can set value of Author property using setAuthor.

you are right, Aspose.Words cannot update AUTHOR field in the document. However, Aspose.Words can update DOCPROPERTY fields. So you can use it instead AUTHOR field. Here is field code:

{ DOCPROPERTY Author * MERGEFORMAT }

You can update such field using UpdateField method.

Hope this helps.
Best regards.

Hi,
I’ve set the author now using setAuthor and have replaced the {AUTHOR}-Field by the DOCPROPERTY-Field as you have proposed.
The update of the fields I’m still doing by Word-AutoOpen-Makros.
This works fine so far.
Only one thing is still strange. The template of the new document is still mentioned as “Normal.dot” instead of the template that I have used for creating.
Even adding a setTemplate command to my source explicitely setting the template to the “right” one has no effect.
This seems to be a display error since in fact the right template was used.
Best regards,
Ulrike

Hi

Best regards,

Great, this works!
Thanks and best regards,
Ulrike

Hi,
let me add a short notice.
I found out that when using this AttachedTemplate command Word is asking twice for activating macros when opening the generated word document: the first time for the doc-file itself and the second time for the attachtd dot-file.
Normally word doc file “inherits” the macros from the attached dot-file and it is only asked once for activating the macros in the doc file (when open it).
When adding an appropriate AttachedTemplate to the AutoOpen Macro within word (instead to my Aspose Code) it is only asked once. Unfortunately it is not necessarily known where the dot-file is stored to put the path fix into the AutoOpen Macro.
Have you any idea to avoid “double activation macro request” when using AttachedTemplate within the Aspose code?
Best regards,
Ulrike

Hi
Thanks for your request. I think you can just remove macros from your document. Please try using code like the following:

String docPath = "C:\\Temp\\in.doc";
String templatePaht = "C:\\Temp\\Template.dot";
// Open document
Document doc = new Document(docPath);
// Remove macros from the document
doc.removeMacros();
// Set attached template
doc.setAttachedTemplate(templatePaht);
// Doc something with document
// .................
// Save document
doc.save("C:\\Temp\\out.doc");


Hope this helps.
Best regards.

Hi,
I am not sure, whether this way is the right one for me or whether I understand your idea correctly.
Within my macros I have realized the update of fields in the doc-file since this was’nt possible with means of Aspose so far. In fact it is an AutoOpen-Macro that has the only task to update the fields in the whole document even in the header and footer.
As I wrote in my first request I’m using the following code for creating a new doc-file based on a dot-File (that contains the AutoOpen-Macro).

Document worddoc = new Document(dotTemplate);


If I understand your proposal correctly, you don’t create a new document in this same way, but rather takes any in.doc-File, open it and afterwards assign the attachedTemplate.
But what is the sense of removing the macros that I still need when open the document lateron in Word?
Best regards,
Ulrike

Hi
My idea it the following:

1. Create a template that contains AutoOpen macro, which will update fields inside the document. You can use this template to create Aspose.Words.Document it is not matter.
2. Remove macros from the document created by Aspose.Words.
3. Attach template that contains AutoOpen macro, which will update fields.
When you open the generated document, macro from the attached template will be run and update fields in the document. Here is code I used for testing:
String docPath = "C:\\Temp\\in.dot";
String templatePaht = "C:\\Temp\\in.dot";
// Open document
Document doc = new Document(docPath);
DocumentBuilder builder = new DocumentBuilder(doc);
// Remove macros from the document
doc.removeMacros();
// Set attached template
doc.setAttachedTemplate(templatePaht);
// Doc something with document
// .................
doc.getBuiltInDocumentProperties().setAuthor("Alexey.Noskov");
builder.insertField("DOCPROPERTY Author", "");
// Save document
doc.save("C:\\Temp\\out.doc");


Best regards.