Reply To: Visual Basic coding, Cells to Form Fields export

  • Isaac Harned

    Member
    January 20, 2023 at 9:52 am
    Points: 8,900
    Rank: UC2 Brainery Purple Belt III UC2 Brainery Purple Belt III

    ok I am much closer now than ever before thanks to you guys and that amazing tool. Still having a couple issues:

    1. I am seeing duplicated field names when formatted to fdf, can’t quite see why this is happening. Some also appear not to add the “J” variable to the name, and there are several duplications of that.

    Also having issue with actually saving the file as FDF, as it is trying to save as xml and just adding the extension for fdf. File attached (could not upload “fdf” file, extension not supported. will have to run macro to see), here’s my code so far:

    Sub fdfExpTest()

    On Error GoTo ErrorHandler

    Application.ScreenUpdating = False ‘disabling screen updating

    Dim ws As Worksheet

    Set ws = Worksheets(“Sheet3”)

    Dim lrow As ListRow

    Dim table As ListObject

    Set table = ws.ListObjects(“Table3”)

    Dim sFileFields, formField As String

    Dim currentCell As String

    Dim fileName As String

    sFileFields = “”

    Dim i As Integer

    For i = 2 To table.ListRows.Count

    Set lrow = table.ListRows(i)

    currentCell = lrow.Range(1, 1).Address

    Dim j As Integer

    For j = 1 To 12

    formField = “<</V(” & Range(currentCell).Value & “)/T(” & Replace(Range(currentCell).Value, “-“, j & “-“) & “)>>”

    sFileFields = sFileFields & formField

    Next j

    Next i

    fileName = InputBox(“Please enter the file name:”, “File Name”)

    If fileName = “” Then Exit Sub ‘if user clicked cancel or didn’t type a name

    Dim dialog As FileDialog

    Set dialog = Application.FileDialog(msoFileDialogSaveAs)

    With dialog

    .Title = “Select a location to save the FDF file”

    .InitialFileName = fileName & “.fdf”

    .Show

    If .SelectedItems.Count = 0 Then

    MsgBox “No location selected, file will not be saved”

    Exit Sub

    Else

    fileName = .SelectedItems(1)

    End If

    End With

    If Dir(fileName & “.fdf”) <> “” Then ‘checking if the path exists

    If MsgBox(“A file with the same name already exists, do you want to replace it?”, vbYesNo) = vbNo Then Exit Sub

    End If

    Dim fso As New FileSystemObject ‘Create a new FileSystemObject

    Dim f As TextStream ‘Create a new TextStream

    Set f = fso.CreateTextFile(fileName & “.fdf”, True) ‘Create a new .fdf file and open it for writing

    f.WriteLine “%FDF-1.2”

    f.WriteLine “%âãÏÓ”

    f.WriteLine “1 0 obj<</Version 1.5/FDF<</F(12 TU – Portrait.pdf)/ID[<48dd2d6619f25a804876d8592bbf3bec><78c1721c7ccd2e9289e7fbfd72376444>]/Fields[” & sFileFields & “]”

    f.WriteLine “]>>endobj”

    f.WriteLine “trailer”

    f.WriteLine “<</Root 1 0 R>>”

    f.WriteLine “%%EOF”

    f.Close ‘closing the file

    MsgBox “FDF file has been created successfully and saved in ” & fileName

    Application.ScreenUpdating = True ‘enabling screen updating

    Exit Sub

    ErrorHandler:

    MsgBox “Error: ” & Err.Description

    Application.ScreenUpdating = True ‘enabling screen updating

    End Sub