Opening xlsm using Microsoft.Office.Interop.Excel on windows forms disabling Excel options when running macro in Excel 2016

lcoimbatorer 1 Reputation point
2020-11-06T14:44:56.11+00:00

Hi,
I am trying to open xlsm (Excel macro enabled workbook) as a child window to windows forms in VB.NET using below code.

The xlsm is getting opened as a child to Forms, however on running macros, all options(Eg: File, Home ,insert) are greyed out in xlsm.

Code:

Imports System.Runtime.InteropServices
Imports Excel = Microsoft.Office.Interop.Excel
Public Class Form2
Public hwnd1 As IntPtr
Private Sub Form2_Load(sender As Object, e As EventArgs) Handles MyBase.Load

    Dim excApp As New Excel.Application()  
    excApp.Visible = True  
    Dim wb As Excel.Workbook = excApp.Workbooks.Open("C:\xxyy\SampleMacro.xlsm", [ReadOnly]:=False)  

    hwnd1 = CType(excApp.Hwnd, IntPtr)  
    SetParent(hwnd1, Me.Handle)  
    MoveWindow(hwnd1, 0, 0, Me.Width, Me.Height, True)  
End Sub  

<DllImport("user32.dll")>  
Public Shared Function SetParent(hWndChild As IntPtr, hWndNewParent As IntPtr) As IntPtr  
End Function  

<DllImport("user32.dll")>  
Public Shared Function MoveWindow(hwnd As IntPtr, x As Integer, y As Integer, nWidth As Integer, nheight As Integer, repaint As Boolean) As Boolean  
End Function  

End Class

Environment:
OS Name: Microsoft Windows 10 Enterprise
OS Version: 10.0.15063 N/A Build 15063

System Type: x64-based PC

Excel version :Microsoft excel for office 365 MSO (16.0.12527.21230) 32-bit

Tried with various Excel-->Trust center-->trust center settings -->Macro settings, same behavior.

Microsoft.Office.Interop.Excel - version: 1.9.0.0

.Net framework 4.838103-xlsmdisabled.png

Attached the output of the program that I am getting now using the specified code.

Please help in getting xlsm opening in Read- Write mode after running a macro.

Not Monitored
Not Monitored
Tag not monitored by Microsoft.
39,536 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Anonymous
    2020-11-06T15:14:43.087+00:00
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.