'Adding new Excel file to SharePoint - irgnore ask for check-in

I am adding a new excel file to a SharePoint Library. The Library requires check-in or check-out. When closing the excel workbook, a dialog pops up in the background asking the user for check-in or not.

Is it possible to prevent the dialog popping up?

Excel.Application oExcel = new Excel.Application();
Excel.Workbook workbook = oExcel.Workbooks.Add();
string xlsxFullName = "https://company.sharepoint.com/sites/collection/list/newfile.xlsx";
workbook.SaveAs(Filename: xlsxFullName, FileFormat: XlFileFormat.xlOpenXMLWorkbook, AddToMru: false );
oExcel.Workbooks.Close();
// Excel asks now for Check-In -> can this be prevented or accepted in program?


Solution 1:[1]

Finally I found a Checkin in the Excel Object Model itself. The workbook can be checked in while open in Excel. This prevents the check.in question when clsoing the workbook.

if (workbook.CanCheckIn() == true)
{
    workbook.CheckIn(SaveChanges: true, Comments: "tbd", MakePublic: false);
}
oExcel.Workbooks.Close();

Solution 2:[2]

The Excel object model doesn't provide anything for that. You can use Windows API functions to find the dialog message and close it (or click the button programmatically). The Close a modal opened by vba macro with c# thread provides a possible solution. Also take a look at the Programmatically dismiss a MsgBox posts, for example:

Private Declare PtrSafe Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Private Declare PtrSafe Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long

Public Sub WaitAndKillWindow()
    On Error Resume Next
    Dim h As Long: h = FindWindow(vbNullString, "Microsoft Excel")
    If h <> 0 Then SendMessage h, 16, 0, 0 ' <-- WM_Close
    Application.OnTime Now + TimeSerial(0, 0, 1), "WaitAndKillWindow"
End Sub

The code uses two Windows API functions: FindWindow and SendMessage.

Solution 3:[3]

Finally I could solve the problem using the user32.dll. The following example brings the window to the foreground. The use can decide.

[DllImport("user32.dll", SetLastError = true)]
static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
[DllImport("user32.dll")]
static extern bool SetForegroundWindow(IntPtr hWnd);

public void killMbox(Object windowTitle)
{
    bool endless = true;
    do
    {
        Thread.Sleep(250);
        IntPtr hdl = FindWindow(null, windowTitle.ToString());
        if (hdl != null)
        {
            SetForegroundWindow(hdl);
            endless = false;
        }
    } while (endless == true);
}

public void main()
{
    Thread mboxKiller = new Thread(killMbox);
    mboxKiller.Start("Microsoft Excel");

    // closing the workbook requires a check-in decision by the user
    // the dialog pops up anywhere in the background
    oExcel.Workbooks.Close();
    
    mboxKiller.Abort();
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Dgo
Solution 2 Eugene Astafiev
Solution 3 Dgo