支援拋出例外 (ThrowException=true) 或靜默失敗 (false) 兩種模式:
public object Execute(string methodName, object[] parameters, bool throwException) {
if (!CheckReady()) return null;
if (throwException) {
if (methodName == null) throw new ArgumentNullException("MethodName");
methodName = methodName.Trim();
if (methodName.Length == 0) throw new ArgumentException("MethodName");
} else {
if (methodName == null) return null;
methodName = methodName.Trim();
if (methodName.Length == 0) return null;
}
foreach (ScriptMethodInfo info in myScriptMethods) {
if (string.Compare(info.MethodName, methodName, true) == 0) {
try {
object result = info.MethodDelegate != null
? info.MethodDelegate.DynamicInvoke(parameters)
: info.MethodObject.Invoke(null, parameters);
return result;
} catch (Exception ex) {
if (throwException) throw;
Console.WriteLine($"VBA:{methodName}:{ex.Message}");
return null;
}
}
}
return null;
}
HasMethod(name):檢查方法是否存在ExecuteSub(name):安全執行無參數方法 (Execute(name, null, false))參考 JavaScript window 物件設計 XVBAWindowObject,核心成員分類:
ScreenWidth/Height、Title/Left/Top/Width/HeightSetTimeout/ClearTimeout/SetInterval/ClearIntervalAlert/AlertError/Confirm/Prompt/BrowseFile/BrowseFolder
protected bool bolUserInteractive = true;
public bool UserInteractive { get => bolUserInteractive; set => bolUserInteractive = value; }
protected string strSystemName = "應用程式";
public string SystemName { get => strSystemName; set => strSystemName = value; }
public XVBAEngine Engine { get => myEngine; }
public IWin32Window ParentWindow { get => myParentWindow; set => myParentWindow = value; }
public int ScreenWidth => bolUserInteractive ? Screen.PrimaryScreen.Bounds.Width : 0;
public int ScreenHeight => bolUserInteractive ? Screen.PrimaryScreen.Bounds.Height : 0;
ParentWindow 為宿主主視窗,WinForm 專案需指派以啟用 UI 功能。