How to check Sharepoint Document Properties using VBA - vba

I am looking to develop code to view the document properties of a file on SharePoint, then later build out this code to see if it matches the document properties of the ActiveWorkbook. Below is a sample of the code I have so far which is able to filter through to the correct document in the SharePoint Library. Does anyone know the function which must be added to objFile to access the SharePoint Document properties?
Sub CheckVersion()
Dim FSO As Object
Set FSO = CreateObject("Scripting.FileSystemObject")
Dim objFile As Object
Dim objDSO As Object
For Each objFile In FSO.GetFolder("\\SharePoint\Library\").Files
If objFile.Name = "FileName.zip" Then
MsgBox (objFile.Properties.Title)
End If
Next
End Sub

I am assuming SharePoint 2010 and higher version.
Use SharePoint REST Services to get proper data items with desired properties.
REST Services Reference for Sharepoint 2010
REST API Reference for SharePoint 2013
Get items from list using SharePoint 2010:
http://lab/_vti_bin/listdata.svc/List1()?$filter=startswith(Title,'foo')&$top=1&$select=Title,Column2,Column3
Get items from list using SharePoint 2013
http://lab/_api/web/lists/getbytitle('List1')/items?$filter=startswith(Title,'foo')&$top=1&$select=Title,Column2,Column3
Then you have to request this url with VBA (remeber to add references to Microsoft WinHTTP Services, version 5.1 and Microsoft XML, v6.0)
URL = "http://lab/_vti_bin/listdata.svc/List1()?$filter=startswith(Title,'foo')&$top=1&$select=Title,Column2,Column3"
Set req = CreateObject("WinHttp.WinHttpRequest.5.1")
req.Open "GET", URL, False
req.setRequestHeader "Content-Type", "application/xml"
req.SetCredentials "login", "pass", 0
req.Send
Set objXML = CreateObject("Msxml2.DomDocument.6.0")
If Not objXML.LoadXML(req.ResponseText) Then
Err.Raise objXML.parseError.ErrorCode, , objXML.parseError.reason
End If
XML parsing is for you ;)
/edit
To filter element with your filename, use:
http://lab/_vti_bin/listdata.svc/Library()?$filter=FileLeafRef eq 'FileName.zip'&$select=Title

Related

Retrieve Outlook Attachment Document Properties using VBA

I am attempting to retrieve some built-in (even custom?) properties from Word/Excel documents in attachments without saving temp files and opening them in the respective Application. I tried attaching them in a MailItem and a DocumentItem.
The properties I am interested in would be: Author, Title, LastSaveDtm, etc. Outlook seems to be able to get them, because the Author name appears at the top of the Preview Pane of a DocumentItem.
The only way I could find to get those properties is using the following methods as described here:
varProp = MailItem.PropertyAccessor.GetProperties(SchemaName)
varProp = DocumentItem.PropertyAccessor.GetProperties(SchemaName)
varProp = MailItem.Attachments(1).PropertyAccessor.GetProperties(SchemaName)
varProp = DocumentItem.Attachments(1).PropertyAccessor.GetProperties(SchemaName)
The SchemaName's are defined in here: [MS-OXPROPS]: Exchange Server Protocols Master Property List
Some interesting definitions from the specs:
named property: A property that is identified by both a GUID and either a string name or a 32-bit identifier
Document object: A Message object that represents a single file, such as a document generated by a word-processing application. The
Message object contains the file as an Attachment object and includes
additional properties to describe the file.
The properties I am trying to retrieve do not have a MAPI tag syntax (canonical name like PidTagPropName) to be used with the proptag namespace (which works from what I have tested*) nor a MAPI id syntax (canonical name like PidLidPropName) to be used with the id namespace, but only have a MAPI string syntax (canonical name like PidNamePropName) to be used with the string namespace.
Here is what I tried for the SchemaName:
"http://schemas.microsoft.com/mapi/string/{00020386-0000-0000-C000-000000000046}/Author"
"http://schemas.microsoft.com/mapi/string/{00020329-0000-0000-C000-000000000046}/Title"
"urn:schemas-microsoft-com:office:office#Author"
"urn:schemas-microsoft-com:office:office#Title"
None of them works.
This document says that "named properties are defined by clients and occasionally service providers"
I have also seen nomewhere that document properties should be "automatically published in MAPI".
So what I am doing wrong?
(*) SchemaName's that work (PidTagSubject):
"http://schemas.microsoft.com/mapi/proptag/0x0037001E"
"http://schemas.microsoft.com/exchange/subject-utf8"
The Outlook object model, nor Redemption, doesn't provide any property or method for that.
You need to save attachments on the disk and then retrieve document properties from files. The Attachment.SaveAsFile method saves the attachment to the specified path.
Sub SaveAttachment()
Dim myInspector As Outlook.Inspector
Dim myItem As Outlook.MailItem
Dim myAttachments As Outlook.Attachments
Set myInspector = Application.ActiveInspector
If Not TypeName(myInspector) = "Nothing" Then
If TypeName(myInspector.CurrentItem) = "MailItem" Then
Set myItem = myInspector.CurrentItem
Set myAttachments = myItem.Attachments
'Prompt the user for confirmation
Dim strPrompt As String
strPrompt = "Are you sure you want to save the first attachment in the current item to the Documents folder? If a file with the same name already exists in the destination folder, it will be overwritten with this copy of the file."
If MsgBox(strPrompt, vbYesNo + vbQuestion) = vbYes Then
myAttachments.Item(1).SaveAsFile Environ("HOMEPATH") & "\My Documents\" & myAttachments.Item(1).DisplayName
End If
Else
MsgBox "The item is of the wrong type."
End If
End If
End Sub
Note, you can automate Office applications where you can open the saved document and read the properties programmatically. See How to use a single VBA procedure to read or write both custom and built-in Document Properties for more information.

How to Edit a Read-Only Word Document (VBA)

I am periodically getting Word documents from various clients and sometimes they send them to me in 'Read-Only' mode. While it isn't a big deal to go to 'View > Edit Document' manually, I cannot seem to find how to do this within my VBA code.
Either opening a document as editable or toggling it as editable once it is open would be sufficient for my needs.
Note that I cannot open the document with 'readOnly = false' as it looks like it is set to 'readOnly recommended' (based on my reading of the MS man page on Document.Open).
IN CONTEXT:
I was also hitting a problem with turning off 'read-mode' which the documents were opening as by default. I have posted this question and answer here.
The code below will change the ReadOnly attribute of a closed file, setting its ReadOnly attribute to either True or False depending on the argument supplied to the procedure.
Private Sub SetReadOnlyProperty(Fn As String, _
ByVal ReadOnly As Boolean)
' 21 Nov 2017
Dim Fso As Object
Dim Doc As Object
Set Fso = CreateObject("Scripting.FileSystemObject")
Set Doc = Fso.GetFile(Fn)
If (Doc.Attributes And vbReadOnly) <> Abs(Int(ReadOnly)) Then
Doc.Attributes = Doc.Attributes Xor vbReadOnly
End If
End Sub
This procedure requires access to the MS Scripting Runtime DLL. Enable this access by checking the box against Miscrosoft Scripting Runtime from Tools >References in the VBE window. Below is an example of how to call the function. Note that an error will result if the supplied file doesn't exist.
Private Sub TestReadOnly()
SetReadOnlyProperty "H:\Test Folder\Test File.docx", False
End Sub

Big Commerce API Put call inside VBA

I have an excel sheet which is doing some matching from Quickbooks (hence the Excel).
I know I can pass "PUT" and "GET" calls using DOM and XMLHTTP but when I send the PUT I get the message back:
A content type for the input was not supplied.
Is this a syntax issue on my part? I've tried a few ways:
/api/v2/products/33288?"Content-Type":"application/json"&"condition":"Worn"
/api/v2/products/33288?"condition":"Worn"
/api/v2/products/33288,"condition":"Worn"
All with similar results.
If it doesn't show, I am not all that comfortable with API calls, but very comfortable with VBA
Full Code
Dim req As New XMLHTTP
Dim doc As New DOMDocument
Dim url As String
Dim nd As String
url = "https://USER:PASS#WEBSITE/api/v2/products/33288?""Content-Type"":""application/json""&""condition"":""Worn"""
req.Open "PUT", url, False
req.Send
doc.LoadXML req.ResponseText
doc.Save "C:\Users\Admin\Desktop\WebQueryResult2.xml"

lotus notes start session

I'm looking for a way to start Lotus Notes and control its utilisation from a vb.net project.
By controlling I mean litle things like getting the window position, closing active document and other stuff.
But the main objective is to start a session.
I'm confused because I tried to use the Lotus Notes Automation Classes dll and nothing worked well...
If anyone had some tips for me, I would apreciate a lot!
Thanks! (by the way, sorry english is not my main language)
In Notes speak, a session is a backend object, not a UI object. What you describe (changing window position, closing active window, etc) is UI functionality.
Notes supports COM, and you have full access to all backend classes. But you don't have access to the UI classes.
Why would you want to automate the actual Notes client? If you describe what you are ultimately wanting to do, perhaps we can help. I am sure that the correct way to solve what you are trying to do is to use the backend classes...
I found a way to start Notes, I needed to use process:
Private Sub StartNotes()
Dim p As Process = New Process()
p.StartInfo.FileName = "C:\Program Files\Notes\notes.exe"
p.StartInfo.Arguments = ""
p.Start()
End Sub
I automate it after using backend classes from lotus and domino dll
Sub Send_Email_via_Lotus_Notes()
Dim Maildb As Object
Dim MailDoc As Object
Dim Body As Object
Dim Session As Object
'Start a session of Lotus Notes
Set Session = CreateObject("Lotus.NotesSession")
'This line prompts for password of current ID noted in Notes.INI
Call Session.Initialize
'or use below to provide password of the current ID (to avoid Password prompt)
'Call Session.Initialize("<password>")
'Open the Mail Database of your Lotus Notes
Set Maildb = Session.GETDATABASE("", "D:\Notes\data\Mail\eXceLiTems.nsf")
If Not Maildb.IsOpen = True Then Call Maildb.Open
'Create the Mail Document
Set MailDoc = Maildb.CREATEDOCUMENT
Call MailDoc.REPLACEITEMVALUE("Form", "Memo")
'Set the Recipient of the mail
Call MailDoc.REPLACEITEMVALUE("SendTo", "Ashish Jain")
'Set subject of the mail
Call MailDoc.REPLACEITEMVALUE("Subject", "Subject Text")
'Create and set the Body content of the mail
Set Body = MailDoc.CREATERICHTEXTITEM("Body")
Call Body.APPENDTEXT("Body text here")
'Example to create an attachment (optional)
Call Body.ADDNEWLINE(2)
Call Body.EMBEDOBJECT(1454, "", "C:\dummy.txt", "Attachment")
'Example to save the message (optional) in Sent items
MailDoc.SAVEMESSAGEONSEND = True
'Send the document
'Gets the mail to appear in the Sent items folder
Call MailDoc.REPLACEITEMVALUE("PostedDate", Now())
Call MailDoc.SEND(False)
'Clean Up the Object variables - Recover memory
Set Maildb = Nothing
Set MailDoc = Nothing
Set Body = Nothing
Set Session = Nothing
End Sub

Sending HTTP requests with VBA from Word

I am trying to send data from a Word document to a web page. I have found some code, pasted it into a new module, and saved it. When I run it I get "compile error, user defined type not defined"
My code:
Sub http()
Dim MyRequest As New WinHttpRequest
MyRequest.Open "GET", _
"http://www.google.com"
' Send Request.
MyRequest.Send
'And we get this response
MsgBox MyRequest.ResponseText
End Sub
A potential alternative to avoid having to select the library is to use an object i.e.
Sub http()
Dim MyRequest As Object
Set MyRequest = CreateObject("WinHttp.WinHttpRequest.5.1")
MyRequest.Open "GET", _
"http://www.google.com"
' Send Request.
MyRequest.Send
'And we get this response
MsgBox MyRequest.ResponseText
End Sub
You need to set a reference to Microsoft WinHTTP Services in your VBA Project (Tools -> References).
Here's what it would look like:
Also, you can read more about the Microsoft WinHTTP Services, version 5.1 here.
You will need to change your references (Tools=>References in the code window). Look for Microsoft WinHTTP Services, version 5.1 (or newer) and tick the box. If you are using Vista and office 2007, you may also need to register it first. Open a command window as administrartor and paste:
>regsvr32.exe "c:\windows\system32\winhttp.dll"
It should say if it works.