Home > OS >  running macro twice leads to blank result and keep getting running error
running macro twice leads to blank result and keep getting running error

Time:09-17

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

CodePudding user response:

The problem here is that you are simply not thinking anything through. You have also simply used the code given to you in a previous answer without first ensuring that you understand it.

NEVER use code you get from the internet without first working through it line by line and ensuring that you fully understand what each part of the code does. You can use the tools in the Visual Basic Editor to help with this. Place your cursor in a term you don't understand and press F2 to display the Object Browser, or F1 to access the online help.

I have added comments to the code to indicate your mistakes.

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)")
        'why are you using Title here when it is the variable used to save the document?
        'you should use newTitle for the MsgBox return value
        Title = MsgBox("Anderer Titel?", vbQuestion   vbYesNo   vbDefaultButton2, "Titel")
        If Title = vbYes Then
            'you should be using the Titlke variable here, not newTitle
            newTitle = InputBox("Wie soll der Titel sein?")
            'corect the prvious two lines and these next two can be deleted
            Title = newTitle
            'this sets newTitle to a blank string as you haven't assigned a value to currentTitle yet
            newTitle = currentTitle
        Else
            Title = oriTitle
            'this sets oriTitle to a blank string as you haven't assigned a value to currentTitle yet
            oriTitle = currentTitle
        End If
        Version = "0"
    Else
        currentUser = InputBox("Wer bearbeitet? (Name in Firmenkurzform)")
        If currentUser = User Then
            
        Else
            'if you use an underscore to separate the user names you will not be able to extract
            'the version number
            'you need to use a different character to separate the names and then use the Split
            'function to return those names as an array
            User = User & "_" & currentUser
        End If
        'see comments above
        Title = MsgBox("Neuer Titel?", vbQuestion   vbYesNo   vbDefaultButton2, "Titel")
        If Title = vbYes Then
            newTitle = InputBox("Wie soll der neue Titel sein?")
            Title = newTitle
            newTitle = currentTitle
        Else
            Title = currentTitle
        End If
        'comments above also apply here. You should have used the newVersion variable for the MsgBox
        Version = MsgBox("Neue Version?", vbQuestion   vbYesNo   vbDefaultButton2, "Version")
        If Version = vbYes Then
            newVersion = currentVersion   1
            Version = newVersion
        Else
            'you haven't assigned a value to currentVersion yet so it will set Version to zero
            Version = currentVersion
        End If
    End If
    
    ActiveDocument.SaveAs2 FilePath & MyDate & "_" & Title & "_i_0" & Version & "_" & User

End Sub

CodePudding user response:

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
  • Related