【问题标题】:Set command line Option for Access VBA为 Access VBA 设置命令行选项
【发布时间】:2018-01-17 09:00:50
【问题描述】:

当我从 C# 启动进程时,我使用以下代码:

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

可以在VBA代码中设置全局变量的值吗?

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

如果我尝试设置参数我有这个错误:

【问题讨论】:

  • 如果您想使用 C# 与 VBA 中的变量进行交互,您应该阅读Office Interop Objects。这将要求您重写问题中发布的所有代码,同时可能会修复您的错误

标签: c# ms-access vba


【解决方案1】:

您需要将您的 MS Access 路径与您的数据库作为参数结合起来。

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);

另外不要忘记在输入下一个参数之前附加/cmd


用于读取启动参数的附加 VBA 代码

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

【讨论】:

  • Access端如何获取参数?
  • @daniele3004 在VBA Code?
【解决方案2】:

最好的方法应该是找到与文件类型相关联的应用程序,然后使用您需要的参数(包括要打开的文件)启动它。

您可以了解如何确定默认应用程序here

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-08-28
    • 2020-03-26
    • 1970-01-01
    • 2013-11-15
    • 2020-03-24
    • 2022-01-21
    • 1970-01-01
    相关资源
    最近更新 更多