Может кто-нибудь помочь мне прикрепить шаблон к текстовому документу через Java-кодирование.
Попробуйте Apache POI , который обеспечивает кросс-платформенное решение на чистом Java для работы с документами MS Office.
с docx4j:
// Create settings part, and init content DocumentSettingsPart dsp = new DocumentSettingsPart(); CTSettings settings = Context.getWmlObjectFactory().createCTSettings(); dsp.setJaxbElement(settings); wordMLPackage.getMainDocumentPart().addTargetPart(dsp); // Create external rel RelationshipsPart rp = RelationshipsPart.createRelationshipsPartForPart(dsp); org.docx4j.relationships.Relationship rel = new org.docx4j.relationships.ObjectFactory().createRelationship(); rel.setType( "http://schemas.openxmlformats.org/officeDocument/2006/relationships/attachedTemplate" ); rel.setTarget("file:///C:\\Users\\jsmith\\AppData\\Roaming\\Microsoft\\Templates\\yours.dotm"); rel.setTargetMode("External"); rp.addRelationship(rel); // addRelationship sets the rel's @Id settings.setAttachedTemplate( (CTRel)XmlUtils.unmarshalString("<w:attachedTemplate xmlns:w=\"http://schemas.openxmlformats.org/wordprocessingml/2006/main\" xmlns:r=\"http://schemas.openxmlformats.org/officeDocument/2006/relationships\" r:id=\"" + rel.getId() + "\"/>", Context.jc, CTRel.class) );
Смотрите org.docx4j.samples.TemplateAttach в docx4j svn для полного примера.
Если вы знаете, как это сделать в VBA, вы можете использовать Com4J, сгенерировать JavaProxies для Word и затем вызывать те же функции из Java.