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

Free Support Forum - aspose.com

Aspose.Word and document macro

If I have a Word .doc file that has macros in it (see below) that are digitally signed can those digitally signed macros persist and use data from the database during the Aspose merge process? For example, we’re trying to pull a path from the database based upon the currently logged-in username and then merge their scanned signature JPG from disk using INCLUDEPICTURE.
SigMergeTokens.JPG (8.0 KB)

Thanks,
Greg

Sub AutoOpen()
    
    Call GetSignature
    
End Sub

Sub GetSignature()

    'There are two bookmarks needed in the Word doc:
    '   The FIRST one called "bkFullSigPath" which holds the full signature path to the JPG as returned from the stored proc (e.g. \\\\SERVERNAME\\share\\Signatures\\GBROWN.jpg)
    '   Then a SECOND one called "bkIncludePicture" which uses the above bookmark to set the value in the INCLUDEPICTURE merge field which is already present in the Word doc.
    
    Dim strFullPathToSig As String 'This will hold the full path to the JPG signature file as returned from the stored proc and located using the 1st bookmark
    'Store the full sig path in the above variable:
    strFullPathToSig = ActiveDocument.Bookmarks("bkFullSigPath").Range.Text 'the FIRST bookmark
    
    Dim fld As Field 'this is going to be the reference to the {INCLUDEPICTURE} merge field that is already present in the Word doc (this field is found using the 2nd bookmark)
    'Set this reference using the 2nd bookmark
    Set fld = ActiveDocument.Bookmarks("bkIncludePicture").Range.Fields(1)
    'Inject the INCLUDEPICTURE merge field with dynamic path into the fld
    fld.Code.Text = "INCLUDEPICTURE " & Chr(34) & strFullPathToSig & Chr(34) & " \d "
    fld.Update
    
    'Clear out the returned path from the initial bookmark so that the user doesn't have to see it
    ActiveDocument.Bookmarks("bkFullSigPath").Range.Text = ""
End Sub

@gregbrownclerk There is no way to run macros using Aspose.Words. However, I think, you can implement the logic from macro using Aspose.Words API.
Could you please attach your sample template, data and expected output document? We will check and provide you more information.

We have a stored procedure that returns a field called SigPath like this with the double slashes and the beginning and ending double quotes,
“\\SERVERNAME\share\Signatures\GBROWN.JPG”

The attached SigMerge.docx (with macros removed due to file upload restrictions here) as you can see has two bookmarks that are used by the macros inside of the document but the macro code is pasted in my previous post above.

The end goal is to merge the scanned signature onto the document based upon the user that is currently logged into the app (see SampleOutput.JPG attached here) - in this case it will be GBROWN.JPG but this is the dynamic part depending upon the stored proc return - it could just as well be JSMITH.JPG.

BTW, I don’t have access to the Aspose.Word API as I’m using a 3rd party app that is using Aspose.Word so I’m only able to alter what the stored procedures return and what Word can do.

Thanks,
Greg
SigMerge.docx (21.3 KB)
SampleOutput.JPG (2.4 KB)
TokenAndBookmarkSamples.JPG (26.5 KB)

@gregbrownclerk In your case you do not need to use INCLUDEPICTURE field. You can use merge field with a special name Image:FieldName and used either image path or image url as a value of FieldName. For example see the following code and template: in.docx (12.1 KB)

string[] fieldNames = new string[] { "FromUrl", "FromPath" };
string[] fieldValues = new string[] {
        @"https://cms.admin.containerize.com/templates/aspose/App_Themes/V3/images/aspose-logo.png",
        @"C:\Temp\aspose-logo.png"
    };

Document doc = new Document(@"C:\Temp\in.docx");
doc.MailMerge.Execute(fieldNames, fieldValues);
doc.Save(@"C:\Temp\out.docx");

Wow! I didn’t know about Image:FieldName!
That makes it MUCH easier.
Thanks for quick response!

@gregbrownclerk Please feel free to ask in case of any issues. We are always glad t help.