簡體   English   中英

如何將Excel中的VBA腳本中的參數作為參數傳遞給外部可執行文件(C#)?

[英]How do you pass parameters from a VBA script in excel to an external executable (C#) as arguments?

我在Excel工作表中有一個嵌入式按鈕,可以調用VBA腳本。 在此腳本中,我正在讀取當前目錄,進行解析,並使用該目錄生成值以將string [] args傳遞給外部C#可執行文件。 我已經經歷了許多迭代,並且確實調用了可執行文件,但是當C#.exe運行時,似乎傳遞的參數為null(空)。 我可以在其他程序中使用此C#.exe並傳遞參數,但是此VBA腳本無法正常工作。 我也知道它正在傳遞正確的參數#,因為我沒有從C#.exe中獲得超出范圍的異常。 我目前正在將所有這些參數轉換為字符串,以嘗試解決此問題,但這很可能不是必需的。 隨附的VBA代碼:

Sub ButtonSG1b7_Click()

Dim FileLocation
Dim ProgramName
Dim length

FileLocation = ActiveWorkbook.FullName
length = Len(FileLocation) - 5
ProgramName = Left(FileLocation, length)
ProgramName = Right(ProgramName, 10)
length = Len(FileLocation) - 15
FileLocation = Left(FileLocation, length)
length = Len(FileLocation) - 2
FileLocation = Right(FileLocation, length)

MsgBox "File Location : " & FileLocation & "    Program Name: " & ProgramName

Dim str0 As String
Dim str1 As String
Dim str2 As String
Dim str3 As String
Dim str4 As String
Dim str5 As String
Dim str6 As String

str0 = "H:\StageGate\Administration\Scripts\GetLatestFileOpen.exe "
str1 = "H:"
str2 = FileLocation
str3 = "Common"
str4 = "\Market Feasibility "
str5 = ProgramName
str6 = ".xlsx"


MsgBox str0 & str1 & str2 & str3 & str4 & str5 & str6
Shell (str0 & str1 & str2 & str3 & str4 & str5 & str6)

End Sub

編輯(添加涉及SolidWorks EPDM庫的預備C#代碼,該代碼接受參數並將其轉換為列表,並使用該列表提供字符串文件路徑以在EPDM庫中的文件夾上獲取最新信息,然后打開文件的更新的本地副本):

using System;
using System.Diagnostics;
using System.Windows.Forms;
using System.Collections.Generic;
using System.Linq;
using System.IO;
using EPDM.Interop.epdm;

class Program
{
static void Main(string[] args)
{

    IEdmFolder5 ppoRetParentFolder;
    List<string> filePath = new List<string>(args);

    if (filePath.Any())
    {
        filePath.RemoveAt(0);  //really need to just stop having the script call passing this argument.
    }


    if (Directory.Exists(@"C:\StageGate")) {
        filePath.Insert(0,"C:");
    }
    else if (Directory.Exists(@"H:\StageGate"))
    {
        filePath.Insert(0,"H:");
    }
    else
    {
        MessageBox.Show("StageGate not found.");
    }

    string newFilePath = string.Join("", filePath.ToArray());
    filePath.RemoveAt(5);
    filePath.RemoveAt(4);
    filePath.RemoveAt(3);
    string folderPathstr = string.Join("", filePath.ToArray());
    //MessageBox.Show(folderPathstr);

    //Have to create vault object to work with BatchGet
    EdmVault5 vault = new EdmVault5();
    //Replace My_Vault with your vault name
    vault.LoginAuto("StageGate", 0);
    //Set the 2 here equal to how many folders you want to get (not counting subfolders)
    EdmSelItem[] folderArray = new EdmSelItem[1];
    IEdmBatchGet bg = (IEdmBatchGet)vault.CreateUtility(EdmUtility.EdmUtil_BatchGet);

    //Create and array element for eachf older you want to get, replace folder locations with your folders
    folderArray[0].mlDocID = 0;
    folderArray[0].mlProjID = vault.GetFolderFromPath(folderPathstr).ID;
    //fa[1].mlDocID = 0;
    //fa[1].mlProjID = vault.GetFolderFromPath("C:\\My_Vault\\FolderPath").ID;

    bg.AddSelection(vault, folderArray);
    bg.CreateTree(0, (int)EdmGetCmdFlags.Egcf_IncludeAutoCacheFiles);  //Egcf_IncludeAutoCacheFiles will get latest version of file
    bg.GetFiles(0, null);

    if (File.Exists(newFilePath))
    {

        Process process = Process.Start(newFilePath); //can't just open the file.  Need to create new windows process to have the file open in the default application(process)
        //File.Open(newPath, FileMode.Open);
    }
    else
    {
        MessageBox.Show("File not found.");
    } 

閱讀您的C#代碼后,我看到您的代碼至少需要6個參數

此VBA代碼轉義了路徑中可能出現的所有空格,並將它們作為命令行參數傳遞:

Sub ButtonSG1b7_Click()

Dim FileLocation
Dim ProgramName
Dim length

FileLocation = ActiveWorkbook.FullName
length = Len(FileLocation) - 5
ProgramName = Left(FileLocation, length)
ProgramName = Right(ProgramName, 10)
length = Len(FileLocation) - 15
FileLocation = Left(FileLocation, length)
length = Len(FileLocation) - 2
FileLocation = Right(FileLocation, length)

MsgBox "File Location : " & FileLocation & "    Program Name: " & ProgramName

Dim args(0 To 6) As String
Dim cmdln As String, i as Integer

args(0) = "H:\StageGate\Administration\Scripts\GetLatestFileOpen.exe"
args(1) = "H:"
args(2) = FileLocation
args(3) = "Common"
args(4) = "\Market Feasibility "
args(5) = ProgramName
args(6) = ".xlsx"

cmdln=arg(0)
For i = 1 To 6
cmdln=cmdln & " """ & args(i) & """"
Next i
MsgBox "VBA Code Writes: " & cmdln
Shell (cmdln)

End Sub

現在,您的C#代碼應讀取以下參數:

class Program
{
static void Main(string[] args)
{
Console.WriteLine("C# Code Reads: "+String.Join(" ", args));
}
}

如果這兩個過程返回相同的命令行字符串,則您在以下行中可能遇到的任何異常都與VBA-C#通信無關

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM