0 votes
in VBA by Beginner (2 points)
edited by

So i have used the tutorial https://wellsr.com/vba/2018/excel/read-append-or-write-to-text-files-with-vba-opentextfile/ and think it is great i have been using it for a few weeks now but i have come across a problem as i was passing the App over to be used in anger. 

The text files i am creating are saved into a sharepoint folder and require to be  checkedin and out of sharepoint how to i ammend the tutorial to allow this to happen?

1 Answer

0 votes
by Skilled (749 points)

I don't have a SharePoint server for testing, but this modified version of OpenTextFileAppend might work. It must be run as an Excel macro.

Sub OpenTextFileAppend()

    Dim fso As Scripting.FileSystemObject
    Dim tsTxtFile As Scripting.TextStream
    Dim sSharePointFilePath As String, sUNCFilePath As String
    sSharePointFilePath = "http://SharePointServer/Documents/Test.txt" ' update this
    sUNCFilePath = "\\SharePointServer\Documents\Test.txt" ' update this
    If Workbooks.CanCheckOut(sSharePointFilePath) = True Then
        Workbooks.CheckOut Filename:=sSharePointFilePath ' Open and Close
        MsgBox sSharePointFilePath & " cannot be checked out (or already is)."
        Exit Sub
    End If
    Set fso = New Scripting.FileSystemObject
    Set tsTxtFile = fso.OpenTextFile(sUNCFilePath, ForAppending, False, TristateMixed)
    With tsTxtFile
        .WriteBlankLines 1
        .Write "[1] Appended at the end of the TextStream WITHOUT a carriage return"
        .WriteLine "[2] Appended as a separate line WITH a carriage return"
    End With
    With Workbooks.Open(sSharePointFilePath)
        If .CanCheckIn Then
            .CheckIn SaveChanges:=True ' CheckIn and Close
            MsgBox sSharePointFilePath & " cannot be checked in."
        End If
    End With
End Sub

To determine the proper value for sUNCFilePath, open the folder on the SharePoint server, click the Actions menu, then pick Open in Windows File Explorer. Copy the path from File Explorer and append the filename for sUNCFilePath.

Welcome to wellsr Q&A
wellsr Q&A is the VBA and Python programming community that rewards you for learning how to code.

Getting Started
VBA Cheat Sheets (On Sale Now)

Earn free prizes for asking VBA and Python questions and for answering questions asked by others in our community.

Looking for something else? Hire our professional VBA Help, instead.

What makes us different?
Our points system rewards you with a chance for free gifts based on the quality of your questions and answers. All you have to do is post and you could get rewarded, like these members:

ParserMonster $25 Amazon Gift Card
Hightree $10 Amazon Gift Card
Thales1 $10 Amazon Gift Card
runfunke $10 Amazon Gift Card
coolag $10 Amazon Gift Card
Siew Hun $10 Amazon Gift Card

So, why don't you join us? It really is an encouraging way to motivate members in our VBA and Python community.


For more programming tips visit the VBA Tutorials Blog and the Python Tutorials Blog.