Update Custom Properties in Word & Validate Correctness of Document | C# VB.NET VBA Code

I have many word/excel documents that has a custom property pointing to a hard coded path which is used by the macros. Using Aspose.Word we change the custom property of the documents and some hard coded paths with in the VBA code (macros).

I want to know if there is a way to write a code using aspose.word or cells to validate if the form is still good after changing the hard coded paths? We plan to change the paths and then validate the form by opening it is memory (supposedly run macro) in silent mode or in background and see if it throws any exceptions? If so, the form is invalid.

I want to make sure that updating the document using aspose we don’t corrupt the document. Users will still be able to open and use the documents.

@kuul13,

Please ZIP and upload your simplified Word & Excel files containing the custom property here for testing. Please also provide the source code that you are currently using to change the values of custom properties. We will then investigate the scenario on our end and provide you more information.

TestDocument.zip (59 Bytes)

The sample code:

using Microsoft.Vbe.Interop;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Word = Microsoft.Office.Interop.Word;
using VBA = Microsoft.Vbe.Interop;
using Aspose.Words;
using System.IO;

namespace WindowsFormsApp1
{
    public partial class Form1 : Form
    {
        private const string mPropertyKey = "XMLPath";
        private string mNewPath = Directory.GetCurrentDirectory();
        private const string mParameterFile = "FormsParameters.xml";
        private const string mFilename = @"TestDocument.docm";
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            // looks like the property name is not case sensitive here
            var doc = new Aspose.Words.Document(mFilename);

            if (doc != null && doc.CustomDocumentProperties != null)
            {
                doc.CustomDocumentProperties.Remove(mPropertyKey);
                doc.CustomDocumentProperties.Add(mPropertyKey, mNewPath);
                doc.Save(mFilename);
            }
            //---------------------------------------------------------------------------------------
            // put validation here. 
            // run the macros, and pull the paramters from the SRSFormsParameters.xml
            //---------------------------------------------------------------------------------------

                //doc.CustomDocumentProperties.Remove(mPropertyKey);
                //doc.CustomDocumentProperties.Add(mPropertyKey, mNewPath);
                //VbaProject project = doc.VbaProject;

                //for (int i = 0; i < project.Modules.Count(); i++)
                //{
                //    VbaModule module = project.Modules[i];
                //    string code = module.Name;

                //    if (code.Equals("clsParameters"))
                //    {
                //        // Replace the original message with the modified message
                //        string source = module.SourceCode;

                //        //int index = source.IndexOf("USER_FOLDERS_PATH");
                //        //if (index >= 0)
                //        //{
                //        var digit = doc.DigitalSignatures;
                //        source = source.Replace("msXMLPath = GetFormsXMLPath", "msXMLPath = \"\"");
                //        module.SourceCode = source;
                //        //project.Modules[i] = module;
                //        //doc.VbaProject = project;
                //        doc.Save(@"C:\SRSServer\test\MRI General Order Form1 .docm");
                //        var tt = doc;
                //        tt.Save(@"C:\SRSServer\test\1.docm");
                //        //}
                //    }
                //}

                //var app = new Word.Application();
                //var doc = app.Documents.Open(@"C:\SRSServer\test\MRI General Order Form .docm" , false, true, Type.Missing, Type.Missing, Type.Missing, 
                //    true, Type.Missing, Type.Missing, false, false, Type.Missing, false, true, Type.Missing);

                //var project = doc.VBProject;
                //foreach(var component in project.VBComponents)
                //{
                //    VBA.VBComponent vbComponent = component as VBComponent;
                //    if(vbComponent != null)
                //    {
                //        string componentName = vbComponent.Name;
                //        var componentCode = vbComponent.CodeModule;

                //    }
                //}

        }
    }
}

@kuul13,

We have logged your requirement in our issue tracking system. Your ticket number is WORDSNET-20031. We will further look into the details of this requirement and will keep you updated on the status of the linked issue. Sorry for the inconvenience.

@kuul13,

I am afraid, the TestDocument.zip file you attached in your previous post is empty; please ZIP and re-attach the source document again here for testing. We also need you to please elaborate your query a bit more:

Most of the attached code sample is commented. Is it expected or should it be un-commented to show us something?

What do you mean by Form and what this Form is all about? Can you please also provide us with the expected good Form? Or some detailed screenshots?

Do you mean that Users will still be able to open and use the documents with Aspose.Words?

Thanks for your cooperation.