VBA Macro Repeat Action Until End of Document
Apprenti-InformatiChien -
Hello,
How can I repeat the following macro in a loop with the stop condition being reaching the end of the document? I don’t master the Loop Until function well. Here is my code :
Sub NotesdeFin_MiseEnForme()
'
' NotesdeFin_MiseEnForme Macro
' Add brackets around endnote number
'
Selection.TypeText Text:="["
Selection.MoveRight Unit:=wdWord, Count:=1
Selection.TypeText Text:="]"
Selection.MoveRight Unit:=wdCharacter, Count:=1
Selection.MoveRight Unit:=wdCharacter, Count:=1, Extend:=wdExtend
Selection.TypeText Text:=" "
Selection.MoveDown Unit:=wdParagraph, Count:=1
Loop Until
End Sub
Result, this formatting:
61. Voir note
is reformatted like this with brackets around the number :
[62]. Mozart, Briefe..., op. cit., lettre du 18 janvier 1775 : I, p. 517 (I, p. 368).
Thanks in advance for your help !
JulienDébutant
4 answers
-
Sub NotesdeFin_MiseEnForme() ' ' NotesdeFin_MiseEnForme Macro ' Ajout crochets autour numéro liste notes de fin ' Dim doc As Document Set doc = ActiveDocument ' Placer le curseur au début du document Selection.HomeKey Unit:=wdStory ' Boucle jusqu'à la fin du document Do Until Selection.End = doc.Content.End Selection.TypeText Text:="[" Selection.MoveRight Unit:=wdWord, Count:=1 Selection.TypeText Text:="]" Selection.MoveRight Unit:=wdCharacter, Count:=1 Selection.MoveRight Unit:=wdCharacter, Count:=1, Extend:=wdExtend Selection.TypeText Text:=" " Selection.MoveDown Unit:=wdParagraph, Count:=1 Loop End Sub -
Hello,
Thank you for your response. Unfortunately the proposed code is not suitable because the loop never stops, which then causes Word to crash. I don't yet know which instruction to indicate to stop the loop.
-
Sub NotesdeFin_MiseEnForme() ' NotesdeFin_MiseEnForme Macro ' Ajout crochets autour numéro liste notes de fin
Dim doc As Document
Dim rng As Range
Set doc = ActiveDocument
Set rng = doc.Content
' Désactiver le rafraîchissement de l'écran et les alertes pour améliorer les performances
Application.ScreenUpdating = False
Application.DisplayAlerts = False
' Boucle à travers chaque mot du document
For i = 1 To rng.Words.Count
If IsNumeric(rng.Words(i).Text) Then
' Ajouter les crochets autour du mot
rng.Words(i).Text = "[" & rng.Words(i).Text & "]"
End If
Next i
' Réactiver le rafraîchissement de l'écran et les alertes
Application.ScreenUpdating = True
Application.DisplayAlerts = True
End Sub -
Thank you very much for your reply.
The code proposed now actually creates two brackets around each number but also around any numbers present in the footnote:
[[1]]. This is true in [[1975]].
I think for now I will keep my first macro and create a shortcut to use it quickly.
I don't yet have the necessary skills to create code that suits me. I will keep studying.
Thanks again for your help!