vbaif-statementms-wordinputboxmsgbox

running macro twice leads to blank result and keep getting running error


I am having 2 issues with my code.

First issue: I cant get it to save as the oriTitle when I am running the macro for the second time and answer with no to the question if I wanted to change the title. It will just be blank.

Second issue: I can only save it for 2 times. After that, I'll get running error. I'd like to keep going for at least 10 times.

Could someone help me with both issues? I have no clue what to do. Thanks in advance!

Private Sub CommandButton3_Click()
   
    Const FilePath As String = "//SRVDC\Arbeitsordner\Intern\Meetings\Entwürfe\"
    Const OrigFileName As String = "20210910_Besprechungsnotizen_00_"
    Dim MyDate As String: MyDate = Format(Date, "YYYYMMDD")
    Dim Title As String
    Dim oriTitle As String: oriTitle = "Besprechungsnotizen"
    Dim newTitle As String
    Dim currentTitle As String
    Dim User As String
    Dim newUser As String
    Dim currentUser As String
    Dim Version As Integer
    Dim newVersion As Integer
    Dim currentVersion As Integer
    

    If Split(ActiveDocument.Name, ".")(0) = OrigFileName Then
        'file has not been resaved
    Else
        'file has been saved before so extract data from filename
        Dim nameElements As Variant
        nameElements = Split(Split(ActiveDocument.Name, ".")(0), "_")
        User = nameElements(UBound(nameElements))
        Version = nameElements(UBound(nameElements) - 1)
    End If
    If User = "" Then
    User = InputBox("Wer erstellt? (Name in Firmenkurzform)")
    newTitle = MsgBox("Anderer Titel?", vbQuestion + vbYesNo + vbDefaultButton2, "Titel")
        If newTitle = vbYes Then
            Title = InputBox("Wie soll der Titel sein?")
        Else
            Title = oriTitle
        End If
    Version = "0"
Else
    currentUser = InputBox("Wer bearbeitet? (Name in Firmenkurzform)")
        If currentUser = User Then
        
        Else
            User = User & "_" & currentUser
        End If
    newTitle = MsgBox("Neuer Titel?", vbQuestion + vbYesNo + vbDefaultButton2, "Titel")
        If newTitle = vbYes Then
            Title = InputBox("Wie soll der neue Titel sein?")
    Else
    End If
        Version = MsgBox("Neue Version?", vbQuestion + vbYesNo + vbDefaultButton2, "Version")
        If Version = vbYes Then
            newVersion = currentVersion + 1
            Version = newVersion
        Else
            Version = currentVersion
        End If
    End If
    
    ActiveDocument.SaveAs2 FilePath & MyDate & "_" & Title & "_i_0" & Version & "_" & User

End Sub

Solution

  • Thanks to another post and the main help of Timothy, I was able to finish my code. here's what I got in case somebody in the future is trying something similar. And I even addded the option to save as PDF and decide if this is supposed to be a new version or not. Here saving as word:

    Private Sub CommandButton3_Click()
    
    Const FilePath As String = "//SRVDC\Arbeitsordner\Intern\Meetings\Entwürfe\"
    Const OrigFileName As String = "20210910_Besprechungsnotizen_00_"
    Dim Title As String: Title = "Besprechungsnotizen"
    Dim newTitle As String
    Dim MyDate As String: MyDate = Format(Date, "YYYYMMDD")
    Dim User As String
    Dim Version As String
    
    If Split(ActiveDocument.Name, ".")(0) = OrigFileName Then
        'file has not been resaved
    Else
        'file has been saved before so extract data from filename
        Dim nameElements As Variant
        nameElements = Split(Split(ActiveDocument.Name, ".")(0), "_")
        User = nameElements(UBound(nameElements))
        Version = nameElements(UBound(nameElements) - 1)
        Title = nameElements(UBound(nameElements) - 3)
    End If
    If User = "" Then
        User = InputBox("Wer erstellt? (Name in Firmenkurzform)")
        newTitle = MsgBox("Anderer Titel?", vbQuestion + vbYesNo + vbDefaultButton2, "Titel")
            If newTitle = vbYes Then
                Title = InputBox("Wie soll der Titel sein?")
            Else
            End If
        Version = "0"
    Else
        Dim currentUser As String
        currentUser = InputBox("Wer bearbeitet? (Name in Firmenkurzform)")
        If currentUser = User Then
        Else
            User = User & currentUser
        End If
        Version = Format$(Version + 1, "0")
        
    End If
    
    ActiveDocument.SaveAs2 FilePath & MyDate & "_" & Title & "_i_0" & Version & "_" & User
    End Sub
    

    Here the PDF part

    Private Sub CommandButton1_Click()
    Const FilePath As String = "//SRVDC\Arbeitsordner\Intern\Meetings\Finale Versionen\"
    Const OrigFileName As String = "20210910_Besprechungsnotizen_00_"
    Dim Title As String: Title = "Besprechungsnotizen"
    Dim newTitle As String
    Dim MyDate As String: MyDate = Format(Date, "YYYYMMDD")
    Dim User As String
    Dim Version As String
    
    If Split(ActiveDocument.Name, ".")(0) = OrigFileName Then
        'file has not been resaved
    Else
        'file has been saved before so extract data from filename
        Dim nameElements As Variant
        nameElements = Split(Split(ActiveDocument.Name, ".")(0), "_")
        User = nameElements(UBound(nameElements))
        Version = nameElements(UBound(nameElements) - 1)
        Title = nameElements(UBound(nameElements) - 3)
    End If
    If User = "" Then
        User = InputBox("Wer erstellt? (Name in Firmenkurzform)")
        newTitle = MsgBox("Anderer Titel?", vbQuestion + vbYesNo + vbDefaultButton2, "Titel")
            If newTitle = vbYes Then
                Title = InputBox("Wie soll der Titel sein?")
            Else
            End If
        Version = "0"
    Else
        newVersion = MsgBox("Neue Version?", vbQuestion + vbYesNo + vbDefaultButton2, "Neue Version")
            If newVersion = vbYes Then
            Dim currentUser As String
            currentUser = InputBox("Wer bearbeitet? (Name in Firmenkurzform)")
                If currentUser = User Then
                Else
                    User = User & currentUser
                End If
            Version = Format$(Version + 1)
            Else
            Version = Format$(Version)
            End If
    End If
    
    
    ActiveDocument.ExportAsFixedFormat OutputFileName:=FilePath & _
                                    MyDate & "_" & Title & "_i_0" & Version & "_" & User & ".pdf", _
                                    ExportFormat:=wdExportFormatPDF, _
                                    OpenAfterExport:=False, _
                                    OptimizeFor:=wdExportOptimizeForPrint, _
                                    Range:=wdExportAllDocument, _
                                    IncludeDocProps:=True, _
                                    CreateBookmarks:=wdExportCreateWordBookmarks, _
                                    BitmapMissingFonts:=True
    End Sub