无法将Excel工作簿标记为最终

时间:2016-08-25 14:13:36

标签: vb.net excel

情况如下:

我们希望向远程计算机发布本地编辑的文件。此文件可以是Word,Excel,Powerpoint类型。显然,在发布到远程计算机之后,我们希望将本地文档标记为final,以防止用户再次编辑它(因为有意的工作流程首先从远程服务器下载,编辑下载的文档并将其发布回服务器。)

所以,有一堆像这样的代码:

Public Sub setDocFinal()
Select Case addin.HostType
    Case ADXOfficeHostApp.ohaWord
        Dim doc As Word.Document = Nothing
        Try
            doc = addin.WordApp.ActiveDocument
            doc.Final = True
        Catch ex As Exception
            Throw New Exception(Me.addin.getLabel("cannotSaveCopy", "Cannot Save the document."))
        Finally
            Marshal.ReleaseComObject(doc)
        End Try
    Case ADXOfficeHostApp.ohaExcel
        Dim doc As Excel.Workbook = Nothing
        Try
            doc = addin.ExcelApp.ActiveWorkbook

            doc.Final = True
            'doc.RefreshAll()
            'doc.CalculateUntilAsyncQueriesDone()
            'doc.Calculate()

        Catch ex As Exception
            Throw New Exception(Me.addin.getLabel("cannotSaveCopy", "Cannot Save the document."))
        Finally
            Marshal.ReleaseComObject(doc)
        End Try
    ' Powerpoint case intentionally skipped as is has same format/code
End Select
End Sub

上面的代码适用于Word案例,但是当涉及到Excel时,它会在弹出窗口上堆叠,告知用户对远程服务器的发布操作: enter image description here

修改
在该特定点,执行冻结(或者可能进入无限内部循环,因为调试模式中唯一可用的按钮是暂停和停止)在将文档设置为最终的行,它永远不会到达finally声明(我们释放对象的地方)。似乎执行尝试将控制权返回给excel文档,但仅此通知发生:

enter image description here

对于Excel的处理,上述代码中有什么问题?
评论中的行显示了我正在经历的一些试验,同时试图在网络上找到解决方案。

此外,这里还有弹出窗口的相关代码:

Private Sub doWork(ByVal action As String, ByVal e As System.ComponentModel.DoWorkEventArgs)
    Dim myDoc As MyDocument
    myDoc = DirectCast(e.Argument, MyDocument)
    System.Diagnostics.Debug.WriteLine("Post in Thread")

    Dim resultObject(3) As Object
    resultObject(0) = True
    resultObject(1) = myDoc

    Dim saveDocumentResult As SaveDocumentResult = Nothing
    Try
        Select Case action
            Case Constants.SAVE_DRAFT
                saveDocumentResult = myDoc.getRequestService().saveDraft(myDoc.getSaveFile(), myDoc.getDocName(), myDoc)
            Case Constants.PUBLISH
                saveDocumentResult = myDoc.getRequestService().publishDocument(myDoc.getSaveFile(), myDoc.getDocName(), myDoc)
                myDoc.setDocFinal() 'this line makes the call to the above code
        End Select

        myDoc.updateDocumentProperty(Constants.VERSION, saveDocumentResult.version)
        myDoc.updateDocumentProperty(Constants.HASH, saveDocumentResult.hash)

        myDoc.setSaved(True)
        System.Threading.Thread.Sleep(1500)
    Catch ex As Exception
        resultObject(0) = False
        resultObject(2) = ex.Message
    Finally
        e.Result = resultObject
    End Try

End Sub

0 个答案:

没有答案