Home > OS >  If Statement combined with InputBox
If Statement combined with InputBox

Time:09-17

here is what I am trying to do:

The file is supposed to save itself according to the user's input and even if there is an input at all. Meaning, if a user has not made any inputs yet, the IF-Statement should return the first part (thats why UserName = ""). But if a user had previously already entered something, I want it to return the second part of the IF-Statement.

The original name of the file is 20210910_Besprechungsnotizen_00_ and if Jack is the first person to open that document and create a protocol, the name is supposed to be: 20210910_Besprechungsnotizen_00_Jack and only asking for Jack's Name. If Jack wants Steve to go over this document, then word should realize, Jack already saved this document (entered his initials or his name) and it will ask Steve which version this document is currently being on and add Steve behind Jack. Meaning: 20210910_Besprechungsnotizen_01_JackSteve while Steve only entered his name and 1.

I am a newbie & your help is much appreciated! Here is my code:

Private Sub CommandButton3_Click()

Dim FilePath As String
Dim Filename As String
Dim MyDate As String
Dim UserName As String

If UserName = "" Then

    FilePath = "//SRVDC\Arbeitsordner\Intern\Meetings\Entwürfe\"
    MyDate = Format(Date, "YYYYMMDD")
    Filename1 = "_Besprechungsnotizen_i_00_"
    UserName = InputBox("Wer erstellt? (Name in Firmenkurzform)")

    ActiveDocument.SaveAs2 FilePath & MyDate & Filename1 & UserName

Else

    FilePath = "//SRVDC\Arbeitsordner\Intern\Meetings\Entwürfe\"
    MyDate = Format(Date, "YYYYMMDD")
    Filename1 = "_Besprechungsnotizen_i_0"
    Filename2 = "_"
    UserName = InputBox("Wer bearbeitet? (Name in Firmenkurzform)")
    Version = InputBox("Welche Version? (in ganzen Zahlen)")

    ActiveDocument.SaveAs2 FilePath & MyDate & Filename1 & Version & Filename2 & UserName
End If

End Sub

CodePudding user response:

Working with InputBox() can be a bit slow and tricky. Anyway, try printing whatever you have entered and see where in the code you are.

Sub TestMe()

    Dim userName As String
    Dim usedVersion As String
    
    userName = InputBox("Enter username")
    If userName = "" Then
        Debug.Print "UserName is empty"
    Else
        Debug.Print "Username " & userName & " was entered."
        Debug.Print "Asking for version now:"
        usedVersion = InputBox("Welche Version? (in ganzen Zahlen)")
        Debug.Print "Version "; usedVersion; " was entered"
    End If

End Sub

This is how the debug looks like:

enter image description here

CodePudding user response:

Try this:

Private Sub CommandButton3_Click()

    Const FilePath As String = "//SRVDC\Arbeitsordner\Intern\Meetings\Entwürfe\"
    Const OrigFileName As String = "20210910_Besprechungsnotizen_00_"
    Dim Filename As String: Filename = "_Besprechungsnotizen_i_0"
    Dim MyDate As String: MyDate = Format(Date, "YYYYMMDD")
    Dim UserName 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), "_")
        UserName = nameElements(UBound(nameElements))
        Version = nameElements(UBound(nameElements) - 1)
    End If
    If UserName = "" Then
        UserName = InputBox("Wer erstellt? (Name in Firmenkurzform)")
        Version = "0_"
    Else
        Dim currentUser As String
        currentUser = InputBox("Wer bearbeitet? (Name in Firmenkurzform)")
        If currentUser = UserName Then
            'you need to figure out what you want to do in this case
        Else
            'you also need to figure out what you want to do when you already have two names
            UserName = UserName & currentUser
        End If
        'do you really need to prompt the user for the version number?
        'couldn't you just increment the existing number, e.g.
        'Version = Format$(Version   1, "00")
        Version = InputBox("Welche Version? (in ganzen Zahlen)") & "_"
    End If
    
    ActiveDocument.SaveAs2 FilePath & MyDate & Filename & Version & UserName

End Sub
  • Related