Try this in 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 taken from: https://thebestworkers.wordpress.com/2012/04/10/passing-command-line-arguments-to-a-macro-enabled-office-object/
Try this in C# (VSTO):
var isEmbedded = new List<string>(Environment.GetCommandLineArgs()).Contains("-Embedding");
MessageBox.Show(string.Format("IsEmbedded={0}", isEmbedded));
Application.HinstanceandApplication.Parent. Maybe you can infer from those whether Excel is embedded.Application.Hinstancereturns the instance number of Excel. I'm not exactly sure what that's referring to, but it looks like a memory location. In which case, that is not too useful... But the other,Application.ParentreturnsMicrosoft Excelin the immediate window. I am inspecting the object now to see if there is something useful there.