This document must contain content type properties - Run time error - 2147216381(80041403)

点点圈 提交于 2021-02-20 04:30:07

问题


I save my file to the SharePoint folder(onedrive sync) using the following VBA code. Off late for some reasons not known to me the SharePoint properties are not displayed

ActiveWorkbook.SaveAs fileName:=spath & e

wherein spath is the SharePoint path and e is the filename.

However, if a file is saved manually to the same folder then the properties appear.

any help is appreciated

For better understanding here is a screenshot of my query


回答1:


If you cannot save the file directly to SharePoint, then you can try a workaround - save the book on a local disk (for example, in the Temp directory) and copy this file along a path known to you, connecting it as a disk on your computer. After that, the temporary file can be deleted (or left for some time as a safety net) and the disk can be disconnected. Something like this:

Sub storeToSharePoint(ByVal sPath As String, sFolderName As String, sFilename As String)
Dim FSO As Object, NW As Object, sTempPath As String, ToPath As String, newFileName As String, sDrive As String
Rem First store ActiveWorkbook to Temp with name sFilename
    sTempPath = Environ("Temp")
    If Right(sTempPath, 1) <> Application.PathSeparator Then
        sTempPath = sTempPath + Application.PathSeparator
    End If
    ActiveWorkbook.SaveAs Filename:=sTempPath & sFilename
Rem And now - copy to SharePoint:
    sDrive = AvailableDriveLetter() ' Try get drive-letter
    If sDrive = vbNullString Then
        Debug.Print "No free letter to connect network location - file '" _
            & sFilename & "' will not be copied"
    Else
        Set NW = CreateObject("WScript.Network")
        Call NW.MapNetworkDrive(sDrive, sPath)
        Set FSO = CreateObject("scripting.filesystemobject")
        ToPath = sDrive & "\" & sFolderName & "\" & sFilename
        FSO.CopyFile Source:=sTempPath & sFilename, Destination:=ToPath
        Call NW.RemoveNetworkDrive(sDrive)
Rem If need Kill temp-file
Rem     FSO.DeleteFile sTempPath & sFilename
        Set FSO = Nothing
        Set NW = Nothing
    End If
End Sub

Here sPath is your spath - root folder of your SharePoint site; sFolderName - path to folder in your SharePoint (You don't want to save all files to the root folder, you put them a little deeper, right?) and sFilename is your "e".

You also need the AvailableDriveLetter function. It is very simple, it many times published in the Internet without any unchanges (Google gives out more than three hundred thousand links). I'll put it here:

Function AvailableDriveLetter() As String
Dim i As Integer
    AvailableDriveLetter = vbNullString
    With CreateObject("Scripting.FileSystemObject")
        For i = Asc("D") To Asc("Z")
            If Not .DriveExists(Chr(i)) Then
                AvailableDriveLetter = Chr(i) & ":"
                Exit For
            End If
        Next
    End With
End Function


来源:https://stackoverflow.com/questions/63645501/this-document-must-contain-content-type-properties-run-time-error-2147216381

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!