1

有没有办法确定当前文档(无论是 Word 文档、Excel 工作簿还是 PowerPoint 演示文稿)是否是从 SharePoint 服务器打开的?

这是您认为在 ActiveDocument / ActiveWorkbook / ActivePresentation 上有属性的东西,但如果有这样的属性,我找不到它。

我可以查看 FullName 属性,看看它是否以 http:// 开头,我想这将是一种合理的启发式方法,但我确信必须有一种更清洁的方式。

如果它有所作为,让我们假设 Office 2007 或更高版本(以及 SharePoint 2007 或更高版本)。

4

1 回答 1

1

前段时间我遇到了同样的问题,但还没有找到明确的方法来确定这一点。我使用的肮脏方式是分析文档的原始路径并根据它确定来源。它仍然有一两个陷阱,但应该处理非恶意的情况/用户。

Private Sub Document_Open()
'if default drafts location is not set in registry then exit
If IsNull(GetDefaultDrafts()) Then Exit Sub

'if document path includes 'http://' then it comes from SharePoint
If InStr(ActiveDocument.Path, "http://") = 1 Then
    'MsgBox ("Opened From SP")
    Exit Sub
    Else
    'if it does not
        If IsNull(GetCustomDrafts()) Then
            'if there is no custom location for drafts in registry
            'check if file path contains default location for drafts
            'if it does then it most likely comes from SharePoint
            If InStr(ActiveDocument.Path, GetDefaultDrafts()) = 1 Then
                'MsgBox ("Opened From SP")
                Exit Sub
            Else
                MsgBox WarningMessage(), vbCritical
                Exit Sub
            End If
        Else
            'there is custom location for drafts
            If InStr(ActiveDocument.Path, GetCustomDrafts()) = 1 Then
                'MsgBox ("Opened From SP")
                Exit Sub
            Else
                MsgBox WarningMessage(), vbCritical
                Exit Sub
            End If
        End If
End If
End Sub

Function GetDefaultDrafts()
Const HKEY_LOCAL_MACHINE = &H80000001

strComputer = "."
Set objRegistry = GetObject("winmgmts:\\" & _
    strComputer & "\root\default:StdRegProv")

strKeyPath = "Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders"
strValueName = "Personal"
objRegistry.GetStringValue HKEY_LOCAL_MACHINE, strKeyPath, strValueName, strValue
If IsNull(strValue) Then
    GetDefaultDrafts = Null
Else
    GetDefaultDrafts = strValue + "\SharePoint Drafts"
End If

End Function

Function GetCustomDrafts()
Const HKEY_LOCAL_MACHINE = &H80000001

strComputer = "."
Set objRegistry = GetObject("winmgmts:\\" & _
    strComputer & "\root\default:StdRegProv")

strKeyPath = "Software\Microsoft\Office\Common\Offline\Options"
strValueName = "Location"
objRegistry.GetStringValue HKEY_LOCAL_MACHINE, strKeyPath, strValueName, strValue
If IsNull(strValue) Then
    GetCustomDrafts = Null
Else
    GetCustomDrafts = strValue
End If

End Function

Function WarningMessage()
WarningMessage = "It seems that this document has not been opened from SharePoint library but from local copy instead. Local copies must not be used to preserve system functionality."
End Function
于 2012-09-14T11:52:28.277 回答