Access to ContentTypeProperties unreliable when using VBA to save Excel files to Sharepoint library
Hi,
I am trying to port different Excel tools my organisation uses from shared drives to Sharepoint online. We are using MS Office 365. These tools will have to create files in different sharepoint libraries that I am conveniently an admin of (and therefore have complete access to). I am able to save the files to Sharepoint just fine, but automatically setting metadata with VBA has proven unreliable. It works fine with certain libraries, but not others. Here's what my (abridged) code looks like:
Dim SavePath As String
SavePath = "https://[ORG].sharepoint.com/sites/[SITE]/[LIBRARY]/"
Dim SPMetaData As Variant
Set SPMetaData = CreateObject("Scripting.Dictionary")
SPMetaData.Add "Column1", "Metadata1"
SPMetaData.Add "Column2", "Metadata2"
SPMetaData.Add "Column3", "Metadata3"
With NewBook
.SaveAs Filename:=SavePath & NewFileName
For Each Key In SPMetaData
.ContentTypeProperties(Key).Value = SPMetaData(Key)
Next Key
.SaveAs Filename:=SavePath & NewFileName
End With
The issue is, when saving through VBA in certain libraries, it's as if the file didn't sync' with Sharepoint and ContentTypeProperties is completely empty. I've tried the following, and while it returns the ContentTypeProperties names just fine in the libraries where it works, it returns error -2147216381 (80041403) on those where it doesn't:
For Each Prop In .ContentTypeProperties
Debug.Print Prop.Name
Next Prop
If I close the file and re-open it manually, it makes it sync with Sharepoint and will now allow VBA to access ContentTypeProperties. I've tried all of the following, to no avail:
- Closing & re-opening the file through VBA before attempting to access ContentTypeProperties
- Saving the file multiple times through VBA before attempting to access ContentTypeProperties
- Inserting a wait time in the code to give a change for things to sync' up
- Poring over both libraries' settings looking for any difference
I've been googling basically all day but am coming up empty. If it didn't work anywhere, I'd accept it's just impossible (although it's a HUGE hindrance). But it reliably working with some libraries and reliably not working with others when using the exact same code (except for the SavePath) is what is sending me for a spin.
Is there any way to force a newly saved file to sync with Sharepoint so it gets the ContenTypeProperties? Is there some SP library setting that causes this that I am overlooking?
Thank you very much!