我们使用将Excel嵌入更大程序(SAP Xcelsius / Dashboard Designer)的程序。有没有办法,使用VBA,检测Excel的当前实例是否是Embedded
实例?
嵌入发生在命令行调用的结果:
"C:\Program Files\Microsoft Office\Office12\EXCEL.EXE" -Embedding
答案 0 :(得分:1)
在VBA中尝试:
Option Base 0
Option Explicit
Private Declare Function GetCommandLine Lib "kernel32" Alias "GetCommandLineW" () As Long
Private Declare Function lstrlenW Lib "kernel32" (ByVal lpString As Long) As Long
Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (MyDest As Any, MySource As Any, ByVal MySize As Long)
Function CmdToStr(Cmd As Long) As String
Dim Buffer() As Byte
Dim StrLen As Long
If Cmd Then
StrLen = lstrlenW(Cmd) * 2
If StrLen Then
ReDim Buffer(0 To (StrLen - 1)) As Byte
CopyMemory Buffer(0), ByVal Cmd, StrLen
CmdToStr = Buffer
End If
End If
End Function
Private Sub Workbook_Open()
Dim CmdRaw As Long
Dim CmdLine As String
Dim CmdArgs As Variant
Dim ValidationType As String
Dim SourcePath As String
Dim TargetPath As String
CmdRaw = GetCommandLine
CmdLine = CmdToStr(CmdRaw)
If Strings.InStr(1, CmdLine, "-Embedding", vbTextCompare) > 0 Then
Call MsgBox("IsEmbedded")
End If
End Sub
VBA-Base-Sourcecode取自:https://thebestworkers.wordpress.com/2012/04/10/passing-command-line-arguments-to-a-macro-enabled-office-object/
在C#(VSTO)中尝试:
var isEmbedded = new List<string>(Environment.GetCommandLineArgs()).Contains("-Embedding");
MessageBox.Show(string.Format("IsEmbedded={0}", isEmbedded));