问题
I am developing word and excel macros, however to keep revision control and track changes I would like to use GitLab
So what I have done so far is to have a code which export out all vba code to a folder on my PC(Z:/Dokumentstyring/GIT), when I run my publish sub routine:
Private Sub publish()
ThisDocument.save
Call ExportVisualBasicCode
' Here I would like to automatically push the code to GIT
Call CloseAll
End Sub
Then I manually go into git shell and type:
git config --global user.name "xxxxx"
git config --global user.email "xxxx@xxxx.com"
cd Z:/Dokumentstyring/GIT
git init
git remote add origin xxxxxx/xxx/Dokumentstyring.git
git add .
git commit -m "Version Number - Date and a short comment"
git push -u origin master
Is there a way that I can get my publish routine todo this manually work for me?
回答1:
It took some time, but I finally manage to solve this issue: The stripped down code looks like this:
Private Sub publish()
Dim hFile As Long
Dim FileContents1 As String
Dim versionNumber As String
Dim strFile1 As String
Dim revTekst As String
Dim gitFileCommands As String
revTekst = "Pusher no filene i koden til Github automatisk!!" '& "<BR>" _
versionNumber = Left(wordMakroVersjon, 2) & "." & Right(wordMakroVersjon, Len(wordMakroVersjon) - 2)
Call ExportVisualBasicCode
Call pushToGit(versionNumber, replace(revTekst, "<BR>", vbNewLine))
gitFileCommands = replace(Settings.CMRDISK & "Dokumentstyring\gitCommands.sh", "\", "/")
Shell "C:\Users\kim\AppData\Local\Programs\Git\bin\sh.exe --login -i -c """ & gitFileCommands & """"
End Sub
Public Sub pushToGit(versionNumber As String, releaseNotes As String)
Dim FileContents1 As String
Dim strFile1 As String
strFile1 = Settings.CMRDISK & "Dokumentstyring\gitCommands.sh"
On Error Resume Next
Kill strFile1
On Error GoTo 0
FileContents1 = "#! /bin/bash" & vbNewLine _
& "cd Z:/Dokumentstyring/GIT" & vbNewLine _
& "git add ." & vbNewLine _
& "git commit -m """ & versionNumber & " - " & Format(Now(), "dd.mm.yyyy") & " " & releaseNotes & """" & vbNewLine _
& "git push -u origin master" & vbNewLine _
& "read -p ""Press enter to continue"" "
Open strFile1 For Binary As #1
Put #1, , FileContents1
Close #1
End Sub
来源:https://stackoverflow.com/questions/47695139/push-to-git-from-vba-on-windows