将新 Excel 文件添加到 SharePoint - 忽略请求签入

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

我正在向 SharePoint 库添加一个新的 excel 文件。图书馆需要签入或签出。 关闭 excel 工作簿时,后台会弹出一个对话框,询问用户是否签入。

是否可以防止弹出对话框?

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?

Excel 对象模型没有为此提供任何东西。您可以使用 Windows API 函数来查找对话框消息并关闭它(或以编程方式单击按钮)。 thread provides a possible solution. Also take a look at the 个帖子,例如:

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

该代码使用两个 Windows API 函数:FindWindowSendMessage

终于可以使用 user32.dll 解决问题了。以下示例将 window 置于前台。使用可以决定。

[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();
}

最后我在 Excel 对象模型中找到了一个签入。工作簿可以在 Excel 中打开时签入。这可以防止在关闭工作簿时出现 check.in 问题。

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