Home > Blockchain >  What is the best way to pass a variable value from VBA to C# app?
What is the best way to pass a variable value from VBA to C# app?

Time:06-30

I need a way to pass a variable from VBA to C# app , The variable will be a string (a path) which will be used as a parameter in C# method (zip and unzip) .

Example : My VBA code in ms access :

Dim strSource,strDest,strPwd as string
strSource = CurrentProject.Path & "\AnyFolder"
strDest = CurrentProject.Path
strPwd = "BlaBla"

My C# Method would be like :

void UnZipMe()
           {

            string MyZip = VBA.strSource;
            string UnzipAt = VBA.strDest;
            string MyZipPass = VBA.Pwd;

            using (ZipFile archive = new ZipFile(MyZip))
            {
                archive.Password = MyZipPass;
                archive.Encryption = EncryptionAlgorithm.PkzipWeak; 
                archive.StatusMessageTextWriter = Console.Out;
                archive.ExtractAll(UnzipAt, ExtractExistingFileAction.Throw);
            }

        }

I could save VBA variable value in a table then get it from C# using OLEDB but i'm trying this way , Can it be done simply or just go for storing the values in access tables? thanks.

CodePudding user response:

I'd pass them as command line arguments.

(this is untested pseudo-code. you'll probably need to enclose those parameters in quotes and possibly escape special characters).

Shell("C:\your-program.EXE " & strSource  & " " & strDest & " " & strPwd , 1) 

Your C# program could be something like:

    internal class Program
    {
        static void Main(string[] args)
        {
            string MyZip = args[0];
            string UnzipAt = args[1];
            string MyZipPass = args[2];

            using (ZipFile archive = new ZipFile(MyZip))
            {
                archive.Password = MyZipPass;
                archive.Encryption = EncryptionAlgorithm.PkzipWeak;
                archive.StatusMessageTextWriter = Console.Out;
                archive.ExtractAll(UnzipAt, ExtractExistingFileAction.Throw);
            }

            Console.WriteLine("Hello, World!");
        }
    }
  • Related