79131718

Date: 2024-10-27 23:45:11
Score: 4
Natty:
Report link

Here is a condensed version of what I'm trying to accomplish:

Sub functionFa()
    Dim savePath As String, saveName As String, changeDate As String
    Dim countForms As Integer, i As Integer, j As Integer
    
    Dim serialNum As String, lastThree As String, LastThreePlusOneStr As String
    Dim LastThreeInt As Integer, LastThreePlusOne As Integer, k As Integer
    
    changeDate = dateFA.Value
    changeDate = Replace(changeDate, "/", ".")
        
        Set WordApp = CreateObject("Word.Application")
        Set WordDoc = WordApp.Documents.Add
        WordApp.Visible = False
        fileTemplate = Range("A1").Value
        Set WordDoc = WordApp.Documents.Open(fileTemplate)

        If startAt.Value <> "" Then     ‘Start at a number other than 1
            countForms = Val(startAt.Value) + (qtyFA.Value - 1)
            j = startAt.Value
        Else
            countForms = qtyFA.Value
            j = 1
        End If
                
    For i = j To countForms
        
        If incSerialCheck = True Then 'Checkbox on userform to increment entered serial number
            serialNum = serialNumberFAA.Value
            lastThree = Right(serialNum, 3)
            LastThreeInt = CInt(lastThree)
            LastThreePlusOne = LastThreeInt + 1
            LastThreePlusOneStr = CStr(LastThreePlusOne)
            NewSerialNumStr = Left(serialNum, Len(serialNum) - 3) & LastThreePlusOneStr
        Else
            NewSerialNumStr = SerialNumber.Value
        End If
            serialNum = NewSerialNumStr
            Debug.Print NewSerialNumStr  ' for debugging 
        End If
        
            WordDoc.FormFields(1).Result = formTrackingNumber.Value
            WordDoc.FormFields(3).Result = workOrder.Value      
            WordDoc.FormFields(17).Result = NewSerialNumStr
            WordDoc.FormFields(27).Result = dateFA.Value
  
                If ValidFileName(saveName) Then
                    With WordDoc
                        .SaveAs savePath & "\" & saveName & ".docx"
                    End If
            End If
Next i

    WordDoc.Close
    WordApp.Quit
    Set WordDoc = Nothing
    Set WordApp = Nothing
    
    Application.ScreenUpdating = True    
End Sub

And this is the section I'm struggling with. I'm trying to get the serial number to increment and inserted into "WordDoc.FormFields(17)" on EACH form created.

 If incSerialCheck = True Then 'Checkbox on userform to increment entered serial number
            serialNum = serialNumberFAA.Value
            lastThree = Right(serialNum, 3)
            LastThreeInt = CInt(lastThree)
            LastThreePlusOne = LastThreeInt + 1
            LastThreePlusOneStr = CStr(LastThreePlusOne)
            NewSerialNumStr = Left(serialNum, Len(serialNum) - 3) & LastThreePlusOneStr
        Else
            NewSerialNumStr = SerialNumber.Value
        End If
            serialNum = NewSerialNumStr
            Debug.Print NewSerialNumStr  ' for debugging 
        End If

I appreciate everyone's help on this, coding I'm very new to and have limited capacity when writing more complicated things. Please excuse my noobness. Please let me know if what I posted isn't clear

Reasons:
  • RegEx Blacklisted phrase (2.5): Please let me know
  • RegEx Blacklisted phrase (1.5): I'm very new
  • Long answer (-1):
  • Has code block (-0.5):
  • Self-answer (0.5):
  • Low reputation (1):
Posted by: Mike