באג | איך למנוע LOOP אין סופי בVBA
- 
כשעושים מאקרו לדוגמא שמחפש סגנון בכל המסמך ועושה בו פעולה, ואז מחפש את הקבע הבא וכו' עד שעובר על כל המסמך, 
 איך עושה שלא יהיה לופ אין סופי,
 לדוגמא בזה:Do Selection.Find.ClearFormatting With Selection.Find .Text = "" .Style = singnon_actual .Forward = True .Format = True .Wrap = wdFindStop End With Selection.Find.Execute Selection.MoveLeft Unit:=wdCharacter, Count:=1 'בוחר מילה ראשונה ומחיל עליה סגנון Selection.MoveEndUntil Cset:=" ", Count:=wdForward Selection.Style = ActiveDocument.Styles(nombre_del_Nuevo_Estilo) Selection.MoveDown Unit:=wdParagraph, Count:=1 Loop Until Selection.Find.Found = Falseלא עוזר לי שאני כותב בחיפוש 
 .Wrap = wdFindStop
 כי אם בסוף המסמך יש גם אותו סגנון נתקע שם לעולםאיך הדרך הכי מקצועית למנוע מצב זה? 
- 
כשעושים מאקרו לדוגמא שמחפש סגנון בכל המסמך ועושה בו פעולה, ואז מחפש את הקבע הבא וכו' עד שעובר על כל המסמך, 
 איך עושה שלא יהיה לופ אין סופי,
 לדוגמא בזה:Do Selection.Find.ClearFormatting With Selection.Find .Text = "" .Style = singnon_actual .Forward = True .Format = True .Wrap = wdFindStop End With Selection.Find.Execute Selection.MoveLeft Unit:=wdCharacter, Count:=1 'בוחר מילה ראשונה ומחיל עליה סגנון Selection.MoveEndUntil Cset:=" ", Count:=wdForward Selection.Style = ActiveDocument.Styles(nombre_del_Nuevo_Estilo) Selection.MoveDown Unit:=wdParagraph, Count:=1 Loop Until Selection.Find.Found = Falseלא עוזר לי שאני כותב בחיפוש 
 .Wrap = wdFindStop
 כי אם בסוף המסמך יש גם אותו סגנון נתקע שם לעולםאיך הדרך הכי מקצועית למנוע מצב זה? זהו? אין תשובות לאף אחד? 
- 
זהו? אין תשובות לאף אחד? @menajemmendel 
 דבר ראשון אף לא חייב לך כלום אז נא להרגיע
 תבקש בצורה יפה יותרובנוגע לשאלה שלך 
 תכתוב ככהDo Selection.Find.ClearFormatting With Selection.Find .Text = "" .Style = singnon_actual .Forward = True .Format = True .Wrap = wdFindStop End With Selection.Find.Execute If Selection.EndOf(wdStory) Then Exit Do ' Exit the loop if the end of the document is reached End If Selection.MoveLeft Unit:=wdCharacter, Count:=1 'Selects a first word and applies a style to it Selection.MoveEndUntil Cset:=" ", Count:=wdForward Selection.Style = ActiveDocument.Styles(nombre_del_Nuevo_Estilo) Selection.MoveDown Unit:=wdParagraph, Count:=1 Loop Until Selection.Find.Found = Falseבקוד זה, ההצהרה "If Selection.EndOf(wdStory) Then" בודקת אם הבחירה הגיעה לסוף המסמך, ואם כן, היא יוצאת מהלולאה באמצעות המשפט "Exit Do" 
- 
@menajemmendel 
 דבר ראשון אף לא חייב לך כלום אז נא להרגיע
 תבקש בצורה יפה יותרובנוגע לשאלה שלך 
 תכתוב ככהDo Selection.Find.ClearFormatting With Selection.Find .Text = "" .Style = singnon_actual .Forward = True .Format = True .Wrap = wdFindStop End With Selection.Find.Execute If Selection.EndOf(wdStory) Then Exit Do ' Exit the loop if the end of the document is reached End If Selection.MoveLeft Unit:=wdCharacter, Count:=1 'Selects a first word and applies a style to it Selection.MoveEndUntil Cset:=" ", Count:=wdForward Selection.Style = ActiveDocument.Styles(nombre_del_Nuevo_Estilo) Selection.MoveDown Unit:=wdParagraph, Count:=1 Loop Until Selection.Find.Found = Falseבקוד זה, ההצהרה "If Selection.EndOf(wdStory) Then" בודקת אם הבחירה הגיעה לסוף המסמך, ואם כן, היא יוצאת מהלולאה באמצעות המשפט "Exit Do" @הדובדבן-שבקצפת תודה רבה לכל העונים, אבל לא עובד, באופן שאתה כתבת עושה את הלולאה רק פעם אחת למרות שנשאר עוד מלא דפים עד הסוף 
- 
@הדובדבן-שבקצפת תודה רבה לכל העונים, אבל לא עובד, באופן שאתה כתבת עושה את הלולאה רק פעם אחת למרות שנשאר עוד מלא דפים עד הסוף @menajemmendel 
 אז תעשה ככהDo While Selection.Find.Execute If Selection.Range.End < ActiveDocument.Range.End Then Selection.MoveLeft Unit:=wdCharacter, Count:=1 'Selects a first word and applies a style to it Selection.MoveEndUntil Cset:=" ", Count:=wdForward Selection.Style = ActiveDocument.Styles(nombre_del_Nuevo_Estilo) Selection.MoveDown Unit:=wdParagraph, Count:=1 Else Exit Do End If Loopאם קיימת אפשרות שהסגנון שאתה מחפש עשוי להופיע מספר פעמים בסוף המסמך, תוכל לשנות את הקוד כדי לבדוק אם סוף טווח הבחירה נמצא במרחק מסוים מסוף המסמך. לדוגמה, ניתן לבדוק אם סוף טווח הבחירה נמצא בטווח של 10 תווים מסוף המסמך. הנה דוגמה: Do While Selection.Find.Execute If ActiveDocument.Range.End - Selection.Range.End > 10 Then Selection.MoveLeft Unit:=wdCharacter, Count:=1 'Selects a first word and applies a style to it Selection.MoveEndUntil Cset:=" ", Count:=wdForward Selection.Style = ActiveDocument.Styles(nombre_del_Nuevo_Estilo) Selection.MoveDown Unit:=wdParagraph, Count:=1 Else Exit Do End If Loopבדוגמה זו, ההצהרה If בודקת אם ההבדל בין סוף המסמך לסוף טווח הבחירה גדול מ-10 תווים. אם כן, הלולאה ממשיכה. אם לא, הלולאה יוצאת. בדרך זו, הלולאה תצא אם היא תגיע לסוף המסמך או אם היא נתקעת בסוף המסמך עקב מופעים מרובים של הסגנון. 
 
