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