2

When I launch process from C# I use the following code:

ProcessStartInfo psi = new ProcessStartInfo(@"C:\MyProgram.exe");
psi.Arguments = "1";
Process p = Process.Start(psi);

It's possible to set the value of global variable in the VBA code?

ProcessStartInfo psiMDB = new ProcessStartInfo(@"C:\MyProgram.mdb");
psiMDB.Arguments = "1";
Process p = Process.Start(psiMDB);

If I try to set parameter I have this error:

enter image description here

Mathieu Guindon
  • 69,817
  • 8
  • 107
  • 235
daniele3004
  • 13,072
  • 12
  • 67
  • 75
  • If you want to interact with variables inside VBA by using C#, you should read into [Office Interop Objects](https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/interop/how-to-access-office-onterop-objects). This would require you to rewrite all code posted in the question, and would probably fix your error in the meantime – Erik A Aug 09 '17 at 13:05

2 Answers2

2

You need to combine your Path to MS Access with your DB as Parameter.

string dbPath = @"c:\MyProgram.mdb";
string pathToAccess = @"C:\Program Files (x86)\Microsoft Office\root\Office16\MSACCESS.EXE";

ProcessStartInfo psiMDB = new ProcessStartInfo(pathToAccess);
psiMDB.Arguments = dbPath + " /cmd 1";
Process p = Process.Start(psiMDB);

Also don't forget to attach /cmd before you put the next parameter in.


Additional VBA Code for reading Startup Arguments

Option Compare Database

Declare Function GetCommandLine Lib "kernel32" Alias "GetCommandLineW" () As Long
Declare Function lstrlenW Lib "kernel32" (ByVal lpString As Long) As Long
Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (MyDest As Any, MySource As Any, ByVal MySize As Long)

Sub PrintStartupArguments()
    MsgBox CmdLineToStr()
End Sub

Function CmdLineToStr() As String

Dim Buffer() As Byte
Dim StrLen As Long
Dim CmdPtr As Long

CmdPtr = GetCommandLine()
If CmdPtr > 0 Then
  StrLen = lstrlenW(CmdPtr) * 2
  If StrLen > 0 Then
    ReDim Buffer(0 To (StrLen - 1)) As Byte
    CopyMemory Buffer(0), ByVal CmdPtr, StrLen
    CmdLineToStr = Buffer
  End If
End If

End Function
0

The best way should be finding the associated application to the file type and then launching it with the arguments you need (including the file to be opened).

You can find out how to determine the default application here.

RicardoSBA
  • 785
  • 1
  • 6
  • 18