-1

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.

Csharp Newbie
  • 303
  • 1
  • 10
  • How are you planning on running this app? From the command line? If Yes then you could pass the arguments via that command line call... – Tim Williams Jun 29 '22 at 17:47
  • I will use this `Shell "MyCSharpApp.exe", vbnormalfocus` – Csharp Newbie Jun 29 '22 at 17:48
  • 1
    https://stackoverflow.com/questions/20917355/how-do-you-run-a-exe-with-parameters-using-vbas-shell – Tim Williams Jun 29 '22 at 17:50
  • 1
    This is really opinion-based. For some situations, command-line args are fine. For more complicated params (where passing binary/typed data is important) and more deep integration, you have COM. For larger datasets/async operation, pipes can be used. And that's far from exhaustive, there are a lot more options. – Erik A Jun 29 '22 at 18:16

1 Answers1

0

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!");
        }
    }
Ben Osborne
  • 1,412
  • 13
  • 20