How to read and write macros from/into document using .NET

Hello.

I have several thousands of word mail-merge documents that i’m not trying to merge using aspose words.

everything works as expected, except one thing.
each document contains the following macro.
what this macro does, is merge the source document into a new one, and the delete empty rows from a table.
(i have some other types of macros that are more complex than this one…).

the problem is, that the macro fails to execute while trying to merge itself into a new document, since it has already been merged by Aspose.

can anyone here think of a creative solution ?
(deleting empty rows using aspose won’t do the trick since thi is only one type of macro, there are a lot of more complex ones. and during the merge process i can’t tell which document has which macro in it)

Sub AutoOpen()
Selection.WholeStory
Application.Run MacroName:=“RTLRUN”
Selection.MoveUp Unit:=wdLine, Count:=1
With ActiveDocument.MailMerge
.Destination = wdSendToNewDocument
.SuppressBlankLines = True
With .DataSource
.FirstRecord = wdDefaultFirstRecord
.LastRecord = wdDefaultLastRecord
End With
.Execute Pause:=False
End With
Selection.WholeStory
Application.Run MacroName:=“RTLRUN”
Selection.MoveUp Unit:=wdLine, Count:=1
Dim TblCnt As Single
Dim MaxTblCnt As Single

TblCnt = 1
y = ActiveDocument.Range.Tables.Count
While TblCnt <= y
CurrTbl = ActiveDocument.Range.Tables(TblCnt)
CurrCell = ActiveDocument.Range.Tables(TblCnt).Cell(CurrTbl.Rows.Count, 1)
While (Len(CurrCell.Text) < 3)
CurrTbl.Rows(CurrTbl.Rows.Count).Delete
CurrTbl = ActiveDocument.Range.Tables(TblCnt)
CurrCell = ActiveDocument.Range.Tables(TblCnt).Cell(CurrTbl.Rows.Count, 1)
Wend
TblCnt = TblCnt + 1
Wend

End Sub

Hi Omer,


Thanks for your inquiry.

What I understand, you are simply doing open/save operations on your template documents using Aspose.Words for .NET and macros which work for template documents no longer work for Aspose.Words generated output documents. Could you please try latest version of Aspose.Words for .NET i.e. 14.3.0 from the following link and see if it resolves your problem?
http://www.aspose.com/community/files/51/.net-components/aspose.words-for-.net/default.aspx

Could you please also perform “Save As” operation using Microsoft Word 2013 on one of your template document and see if macros still work for Microsoft Word generated documents? In case the problem still remains, please attach the following resources here for testing:

  1. Your sample template Word document containing the macros you’re getting this problem with.
  2. Aspose.Words generated output Word document for which macros fail to run.
  3. Please create a standalone (runnable) simple console application that helps us reproduce your problem on our end and attach it here for testing.

As soon as you get these pieces of information ready, we’ll start investigation into your issue and provie you more information. Thanks for your cooperation.

Best regards,

Hi.


Actualy the problem i have is a bit different.
these documents are reports that we used to mail-merge manually.
so the macros always had two stages
1. merge into a new document
2. perform the required action (delete empty rows from tables, split tables, etc…)

the problem now, is that aspose merges the document, and outputs a merged document, while reserving the original macro.

so when i try to open the document that i merged with aspose, it does open successfully, but the tries to run the macros.
of course it always fails, because it tries to merge the document in to a new one.

i don’t think any source code will help here.
the code i use is very simple, and it works. the problem is more fundamental…

hope you can help

Omer

(again, just to be clear, the macros don’t “fail to run”, they do run, but then fail while trying to merge in to a new document, since the document is already merged)

Hi Omer,


Thanks for the additional information. I am afraid, Aspose.Words does not deal with Macros in document. I think, you should remove macros from Aspose.Words generated merged document using Document.RemoveMacros method and try achieving their functionalities using Aspose.Words itself. Generally, the operations, that you can perform using Microsoft Word, can also be performed on Word documents using Aspose.Words. Please refer to the documentation below:
http://www.aspose.com/docs/display/wordsnet/Home

Best regards,

@ohameiri

With the latest version of Aspose.Words for .NET 20.5, you can read and write macros from/to document. Please read the following article about working with VBA macros. .
Working with VBA Macros