Error 5854 string parameter is too long
j'utilise une variable a remplacer dans un fichier world mais malheureusement il n'accepte pas vue que le texte a remplacer ets plus de 255 caracteres ,
y't il une solution pour ca ??
Hello,
J'ai eu ce problème.
J'ai recherché sur des forums et depuis j'utilise ce code très pratique
Mon champ à remplir s'appelle Champ1 dans mon word
Sub remplace_word_255()
'Ne pas oublier d'ajouter Microsoft Word Object Library XX.0 aux références du projet
'Variables application de word
Dim WdApp As Word.Application
Dim WdDoc As Word.Document
'Ouverture de word
Set WdApp = New Word.Application
'ouverture du document Word avec le chemin complet
Set WdDoc = WdApp.Documents.Open("C:\xxxxxx.docx")
With WdApp
.ActiveDocument.FormFields("Champ1").Result = "****"
.Selection.Goto what:=wdGoToBookmark, Name:="Champ1"
.Selection.Collapse
.Selection.MoveRight wdCharacter, 1
.Selection.TypeText MaVariableDePlusDe255Caracteres
.Selection.Goto what:=wdGoToBookmark, Name:="Champ1"
With .Selection.Find
.Execute findtext:="*", replacewith:="", Replace:=wdReplaceAll
End With
End With
WdApp.Visible = True
End Sub
Je peux te rechercher l'explication de chaque ligne si tu veux mais je l'ai plus en tête.
merci Beacoup
voici mon code :
est ce que je dois refaire toute la structure :
Public Sub Edit_Word_Document()
Dim i As Integer
Dim wordDocument As Word.Document
Dim experience As String
Dim wordApp As Word.Application
Set wordApp = CreateObject("word.Application")
For i = 2 To ActiveSheet.Cells(ActiveSheet.Rows.Count, 1).End(xlUp).Row
If LCase(ActiveSheet.Cells(i, 12)) = "dnm" And ActiveSheet.Cells(i, 1) <> "" Then
wordApp.Documents.Open "C:\Users\riadh.said@cic.gc.ca\Desktop\Condidats\Template.docx"
Set wordDocument = wordApp.ActiveDocument
bodymessage = ""
bodymessage1 = ""
bodymessage2 = ""
bodymessage3 = ""
bodymessage4 = ""
bodymessage5 = ""
If LCase(Cells(i, "D").Value) = "dnm" Then bodymessage = vbNewLine & Sheets("SOMC-Legend").Range("B22").Value
If LCase(Cells(i, "E").Value) = "dnm" Then bodymessage1 = vbNewLine & Sheets("SOMC-Legend").Range("B23").Value
If LCase(Cells(i, "F").Value) = "dnm" Then bodymessage2 = vbNewLine & Sheets("SOMC-Legend").Range("B24").Value
If LCase(Cells(i, "G").Value) = "dnm" Then bodymessage3 = vbNewLine & Sheets("SOMC-Legend").Range("B25").Value
If LCase(Cells(i, "F").Value) = "dnm" Then bodymessage2 = vbNewLine & Sheets("SOMC-Legend").Range("B26").Value
If LCase(Cells(i, "G").Value) = "dnm" Then bodymessage3 = vbNewLine & Sheets("SOMC-Legend").Range("B27").Value
experience = bodymessage & bodymessage1 & bodymessage2 & bodymessage3 & bodymessage4 & bodymessage5
wordDocument.Content.Find.Execute FindText:="VariableToReplace", ReplaceWith:=experience, Replace:=wdReplaceAll
' wordDocument.Content.Find.Execute FindText:="VariableGroupe", ReplaceWith:=ActiveSheet.Cells(i, i + 1).Text, Replace:=wdReplaceAll
wordDocument.SaveAs "H:\PDF FILES\" & ActiveSheet.Cells(i, 1).Value & ", " & ActiveSheet.Cells(i, 2).Value & ".pdf", 17
'set placeholder variable back
wordDocument.Content.Find.Execute FindText:=experience, ReplaceWith:="VariableToReplace", Replace:=wdReplaceAll
End If
Re. Adapté à ton code tu peux remplir ta variable au fur et à mesure:
Public Sub Edit_Word_Document()
Dim i As Integer
Dim wordDocument As Word.Document
Dim experience As String
Dim wordApp As Word.Application
Set wordApp = CreateObject("word.Application")
For i = 2 To ActiveSheet.Cells(ActiveSheet.Rows.Count, 1).End(xlUp).Row
If LCase(ActiveSheet.Cells(i, 12)) = "dnm" And ActiveSheet.Cells(i, 1) <> "" Then
wordApp.Documents.Open "C:\Users\riadh.said@cic.gc.ca\Desktop\Condidats\Template.docx"
Set wordDocument = wordApp.ActiveDocument
bodymessage = ""
bodymessage1 = ""
bodymessage2 = ""
bodymessage3 = ""
bodymessage4 = ""
bodymessage5 = ""
If LCase(Cells(i, "D").Value) = "dnm" Then bodymessage = vbNewLine & Sheets("SOMC-Legend").Range("B22").Value
If LCase(Cells(i, "E").Value) = "dnm" Then bodymessage1 = vbNewLine & Sheets("SOMC-Legend").Range("B23").Value
If LCase(Cells(i, "F").Value) = "dnm" Then bodymessage2 = vbNewLine & Sheets("SOMC-Legend").Range("B24").Value
If LCase(Cells(i, "G").Value) = "dnm" Then bodymessage3 = vbNewLine & Sheets("SOMC-Legend").Range("B25").Value
If LCase(Cells(i, "F").Value) = "dnm" Then bodymessage2 = vbNewLine & Sheets("SOMC-Legend").Range("B26").Value
If LCase(Cells(i, "G").Value) = "dnm" Then bodymessage3 = vbNewLine & Sheets("SOMC-Legend").Range("B27").Value
experience = bodymessage & bodymessage1 & bodymessage2 & bodymessage3 & bodymessage4 & bodymessage5
If Len(experience) > 200 Then
Do While Len(experience) >= 200
experience_part = Left(experience, 200) & "VariableToReplace"
experience = Right(experience, Len(experience) - 200)
wordDocument.Content.Find.Execute findtext:="VariableToReplace", replacewith:=experience_part, Replace:=wdReplaceAll
Loop
wordDocument.Content.Find.Execute findtext:="VariableToReplace", replacewith:=experience, Replace:=wdReplaceAll
Else
wordDocument.Content.Find.Execute findtext:="VariableToReplace", replacewith:=experience, Replace:=wdReplaceAll
End If
' wordDocument.Content.Find.Execute FindText:="VariableGroupe", ReplaceWith:=ActiveSheet.Cells(i, i + 1).Text, Replace:=wdReplaceAll
wordDocument.SaveAs "H:\PDF FILES\" & ActiveSheet.Cells(i, 1).Value & ", " & ActiveSheet.Cells(i, 2).Value & ".pdf", 17
'set placeholder variable back
wordDocument.Content.Find.Execute FindText:=experience, ReplaceWith:="VariableToReplace", Replace:=wdReplaceAll
End If
Par contre par sûr que la dernière ligne fonctionne
wordDocument.Content.Find.Execute FindText:=experience, ReplaceWith:="VariableToReplace", Replace:=wdReplaceAll
HEY , je l'ai essayé ca fonctionne pas :/
Salut,
Est-ce que tu peux éventuellement mettre tes fichiers excel et word en pièce jointe pour pouvoir tester sur les fichiers réels?
Hey je sais pas trop comment faire un upload ?
En dessous du champ de réponse tu as "Ajouter des fichiers joints" qui est affiché.