1

We work with a program that embeds Excel in a larger program (SAP Xcelsius/Dashboard Designer). Is there a way, using VBA, to detect if the current instance of Excel is an Embedded instance or not?

The embedding occurs as a result of the command line invokation:

"C:\Program Files\Microsoft Office\Office12\EXCEL.EXE" -Embedding
4
  • Two wild guesses: Application.Hinstance and Application.Parent. Maybe you can infer from those whether Excel is embedded. Commented Feb 10, 2012 at 20:21
  • Thanks for the suggestion... Application.Hinstance returns 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.Parent returns Microsoft Excel in the immediate window. I am inspecting the object now to see if there is something useful there. Commented Feb 10, 2012 at 20:45
  • Have you been able to solve your problem? Commented Jun 29, 2015 at 13:03
  • 1
    @Sergey: See my new answer below... Commented Feb 28, 2017 at 15:53

1 Answer 1

1

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));
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.