0

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
7
  • Although your question explains the goal of your project it isn't clear what help you need. Please edit your question and explain clearly what it is that you need help with. It may be helpful to review How do I ask a good question? Commented Sep 10, 2021 at 10:15
  • The logic is not clear. You should ask for the UserName = InputBox before the If statement. Or use maybe the If ActiveDocument.Saved learn.microsoft.com/en-us/office/vba/api/word.document.save. Commented Sep 10, 2021 at 10:19
  • You need some place to store the user name if it has already been entered before. Your code does not seem to have any thing like that plus the variable named as "UserName" doesn't have any value assignment. So the logic will always be true.the else block will never be executed. Are you trying to do it in excel? Commented Sep 10, 2021 at 10:25
  • I edited to original post and hope its easier to understand what my goal is. And now I am trying to do it in word Commented Sep 10, 2021 at 11:52
  • @mishu36 can you elaborate? Cause this is exactly what seems to happen. Doesnt matter if somebody entered already or not, the logic is always true. please try to explain like you know youre explaining to a newbie Commented Sep 10, 2021 at 11:56

2 Answers 2

0

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
Sign up to request clarification or add additional context in comments.

18 Comments

you got my idea and its pretty damn close to what I am trying to do. its not quite working the way though. apparently, it goes straight to the line: Else UserName = InputBox("Wer bearbeitet? (Name in Firmenkurzform)") And when I change names again, it wont add the new name, it will just replace.
meaing it skips: If UserName = "" Then UserName = InputBox("Wer erstellt? (Name in Firmenkurzform)") Version = "0_". my original file is named 20210910_Besprechungsnotizen_00_. its just when the first person saves it, it should stay 00_(+name). and every other time, it should ask for the version and add the new name
@Jens1411 - When you ask a question here you need to provide all the information needed to provide an answer, e.g. the original file name. Keep in mind that we cannot see your document, or read your mind.
I've edited my original post one more time. Hope this is a much better explanation and I appreciate your help.
I am getting an error: If LBound(Split(ActiveDocument.Name, ".")) = OrigFileName Then
|
0

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

2 Comments

Thanks for your response, but it doesnt solve what I am trying to do. Perhaps I should elaborate on what I am actually trying to do: my goal is that the file is going to be saved by date_Besprechungsnotizen_Version_Name. But the Version should be automatically 0 when the first person enters his name and after that, word should realize at least a second person is putting something into the inputBox, so word will ask about which version and who is editing. Hope this makes more sense
I edited the original post to better explain my goals

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.