How to select items in listbox field from VBA
- May 27, 2020
- 2 replies
- 4605 views
Good evening,
I’d be grateful if anyone could help in my problem.
I have to fill out the fields of a administrative pdf forms from Access (where I have my database), under VBA. I have no problems with the main part of the fields, except with listboxes having a multiple choice selection.
I put my code below, and the form for example is attached. Even if I send a sorted array of integers to set the currentValueIndices, I get the error code 13 : type incompatibility. The array type is 8194 (array 8192 + integer 2), and all my values are integer.
Could anyone help to understand how to pass the array to the pdf listbox field and set the selected items, please.
Many thanks
Bruno
from Adobe Acrobat SDK
«To set a multiple selection for a list box that allows it, pass an array as argument to this property, containing the indices (sorted in ascending order) of those strings in the options array. »
Dim apPDDoc, apVisuPDDoc, formAcrob, javscrobj As Object
Dim champTravail As Object
Dim nomFichOr, nomFichDest As String
Dim Cpt, nbLangagesSel As Integer
Dim valListBox() As Integer
Dim chLangage As String
chLangage = "PYT;JAV;CPP" ' Valeurs possibles PYT JAV PAS CPP VBA XML
Dim langagesList As Object
Set langagesList = CreateObject("Scripting.Dictionary")
langagesList.Add "CPP", 0
langagesList.Add "CPP_l", "C/C++"
langagesList.Add "JAV", 1
langagesList.Add "JAV_l", "Java"
langagesList.Add "PAS", 2
langagesList.Add "PAS_l", "Pascal"
langagesList.Add "PYT", 3
langagesList.Add "PYT_l", "Python"
langagesList.Add "VBA", 4
langagesList.Add "VBA_l", "VBA"
langagesList.Add "XML", 5
langagesList.Add "XML_l", "XML"
nomFichOr = "C:\Bruno\ManipPdfAccess\Form_Travail_remplLstbox.pdf"
nomFichDest = "C:\Bruno\ManipPdfAccess\Form_Travail_remplLstbox_rempli.pdf"
Set apPDDoc = CreateObject("AcroExch.PDDoc")
Set apVisuPDDoc = CreateObject("AcroExch.AVDoc")
If apVisuPDDoc.Open(nomFichOr, "") Then
Set formAcrobat = apVisuPDDoc.GetPDDoc()
Set javscrobj = formAcrobat.getJSObject
Set chTravail = javscrobj.getfield("chLangage")
' Extraction des langages sélectionnés
' Extraction of the selected values from chLangage
nbLangagesSel = (Len(chLangage) + 1) / 4
ReDim valListBox(0 To nbLangagesSel - 1)
For Cpt = 0 To nbLangagesSel - 1
' Va chercher dans le dictionnaire l'indice de liste associé au langage à sélectionner
' Fills the array with selected items, through the dictionnary
valListBox(Cpt) = CInt(langagesList.Item(Mid$(chLangage, 4 * Cpt + 1, 3)))
Next
' Tri du tableau, en principe par une fonction appelée (array sort, done by a function)
' Sorting the array, normaly made by function call
Dim i, temp As Integer
Dim finTri, pasDeModif As Boolean
finTri = False
pasDeModif = True
While Not finTri
For i = 0 To UBound(valListBox) - 1
If valListBox(i) > valListBox(i + 1) Then
temp = valListBox(i)
valListBox(i) = valListBox(i + 1)
valListBox(i + 1) = temp
pasDeModif = False
End If
Next i
If pasDeModif Then finTri = True Else pasDeModif = True
Wend
' Là, erreur 13, c'est pourtant tableau d'integer
' Here there is error code 13 type incompatibility
chTravail.currentValueIndices = valListBox()
formAcrobat.Save 1, nomFichDest
Else
MsgBox ("Pas de document de ce nom disponible")
End If
Set chTravail = Nothing
Set javscrobj = Nothing
Set formAcrobat = Nothing
Set apVisuPDDoc = Nothing
Set apPDDoc = Nothing