Macros in word templates


#1

Hi guys,

I have word template that contains a macro, when i process it through the Apose word component the macro is lost. The template i use that contains the macro is used to generate in most cases more then one document and these documents are then combined together.

Also is there anyway to run a macro on a document after the merge fields are populated?

I have attached the template.


Thanks

Dave


#2

Hi Dave,

Thank you for your interest in Aspose.Word.

So is the macro missed in the combined document or directly in the attached document after executing mail merge and saving to disk? I’ve just tested it and no macros were lost after saving.

With respect to your second question, we do not allow to run macros programmatically using Aspose.Word due to security reasons.


#3

Hi Dmitri,

Thanks for te reply, the macro ‘SelectTable’ is lost on the final combined document.

cheers

dave


#4

It is expected since you move document content only, not common document data like macros. We wouldn’t want to deal with macros separately as I posted above. So a possible workaround would be as follows: use this document as the destination document and add other document sections to it; of course, clear its content to make it blank first using Document.RemoveAllChildren.


#5

Hi DmitryV,

I am using a clone of the document as a destination document. When generating 1 document with 1 set of xml it works fine, keeps the macro.

The problem arises when i append other documents to the first one. This is when the macro is lost. It is not stored on the final multi document consisting of more than one page.

I hope i made myself a little more clearer this time round

Thanks


#6

dmitry

This is the code i use for genrating multiple documents and combining them

foreach(Aspose.Word.Section sourceSection in docOutput.Sections)
{
Aspose.Word.Section newSection = (Aspose.Word.Section)docMulti.ImportNode(sourceSection, true);
newSection.PageSetup.SectionStart = SectionStart.NewPage;
docMulti.Sections.Add(newSection);
}

Is the macro being lost because im starting a new section?

Thanks


#7

No it doesn't seem to be the cause. However, something weird is happening with your document, I'm experiencing problems with adding sections to it when trying to reproduce the issue... I'm not sure what's going on at the moment, it definitely requires further investigation so I'm logging it in the meantime. Please check back later, probably next week.


#8

The following code works okay for us, the macro retains in the resulting document:

Document clonedDoc = TestUtil.Open(@“Other\TestDefect583.doc”);
Document docToAppend = TestUtil.Open(@“Other\TestMacro2.doc”);<?xml:namespace prefix = o ns = “urn:schemas-microsoft-com:office:office” /><o:p></o:p>

<o:p></o:p>

clonedDoc.RemoveAllChildren();<o:p></o:p>

<o:p></o:p>

foreach(Aspose.Word.Section sourceSection in docToAppend.Sections)
{
Aspose.Word.Section newSection = (Aspose.Word.Section) clonedDoc.ImportNode(sourceSection, true);
newSection.PageSetup.SectionStart = SectionStart.NewPage;
clonedDoc.Sections.Add(newSection);
}<o:p></o:p>

TestUtil.Save(clonedDoc, @“Other\TestDefect583 Out.doc”);

Please post more code, especially a fragment in which you create (or clone) the destination document. Also, attach other documents involved in the merge. Thanks.


#9

Hi DmitryV

Heres the code i use to clone

// Take clone of Word template and assign to output document
docOutput = docWordTemplate.Clone();

Heres the code i use if i know the Final document is going to have other docs appended to it.

protected override object PreProcess(XmlDocument xmlRequest, XmlTextWriter xmlOutput)
{
Document docMulti = null;
string strOutputDocumentFrequency = xmlRequest.SelectSingleNode(@"//Output/Frequency").InnerText;

// If all document items are to be output to a single document
if (strOutputDocumentFrequency == "Multi")
{
docMulti = new Document();
// Removes default blank section from new word document
docMulti.Sections.RemoveAt(0);
}
return docMulti;
}

protected override void PostProcess(object objMulti, string strOutputFile, XmlDocument xmlRequest, XmlTextWriter xmlOutput)
{
string strOutputDocumentFrequency = xmlRequest.SelectSingleNode(@"//Output/Frequency").InnerText;

// If all document items are to be output to a single document
if (strOutputDocumentFrequency == "Multi")
{
Document docMulti = (Document) objMulti;
docMulti.Save(strOutputFile + ".doc",SaveFormat.FormatDocument);
xmlOutput.WriteElementString("File", strOutputFile + ".doc");
}
}

I've also attached the input xml and my current output (without the macro)

Many Thanks have a good weekend

Dave




#10

Thanks for posting. Now I’ve got 2 questions:

1. Is docWordTemplate surely document that contains the macro?

2. Are you sure that you save docOutput (cloned document that should contain the macro), not docMulti that is created from scratch?