If 语句结合 InputBox

If Statement combined with InputBox

这是我正在尝试做的事情:

文件应该根据用户的输入自动保存,即使根本没有输入。意思是,如果用户还没有进行任何输入,那么 IF 语句应该 return 第一部分(这就是为什么 UserName = "")。但是如果用户之前已经输入了一些东西,我希望它 return IF 语句的第二部分。

文件的原始名称是 20210910_Besprechungsnotizen_00_ 如果 Jack 是第一个打开该文件并创建协议的人,则名称应该是:20210910_Besprechungsnotizen_00_Jack 并且只要求杰克的名字。如果 Jack 想让 Steve 查看这个文档,那么 word 应该意识到,Jack 已经保存了这个文档(输入了他的首字母或他的名字),它会询问 Steve 这个文档当前使用的是哪个版本,并在 Jack 后面添加 Steve。意义: 20210910_Besprechungsnotizen_01_JackSteve 而史蒂夫只输入了他的名字和 1.

我是新手,非常感谢您的帮助! 这是我的代码:

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

使用 InputBox() 可能会有点慢且棘手。无论如何,尝试打印您输入的任何内容,看看您在代码中的位置。

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

这是调试的样子:

试试这个:

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