@אלף-שין כתב בבקשת מידע | האם אפשר לבצע עשרות החלפות ביחד?:
אשמח.....
מצורף!
תשנה את הנתיב למה שתבחר ותיצור שם קובץ עם סיומת TXT
Sub החלפות_מרובות_מקובץ_טקסט()
Dim filePath As String
Dim fileNumber As Integer
Dim fileContent As String
Dim lines() As String
Dim line As Variant
Dim words As String
Dim replac() As String
Dim awords() As String
'כתוב כאן את הנתיב ושם הקובץ שבחרת
filePath = "Z:\נתוני תוכנות\Word\תבניות לתוספת\החלפות.txt"
fileNumber = FreeFile
Open filePath For Input As fileNumber
fileContent = Input$(LOF(fileNumber), fileNumber)
Close fileNumber
lines = Split(fileContent, vbCrLf)
If Application.CapsLock = True Then
words = InputBox("כתוב את המילים להחלפה הפרד בין המקור למחליף באמצעות ""-"" ללא רווח אם ברצונך להוסיף כמה ערכים הפסק באמצעות ""."" ביניהם", "הוספת ערכים להחלפה")
If words <> "" Then
Dim wordExists As Boolean
wordExists = False
awords = Split(words, ".")
For i = 0 To UBound(awords)
For Each line In lines
If InStr(1, line, awords(i), vbTextCompare) > 0 Then
wordExists = True
Exit For
End If
Next line
' בדוק אם המילים נמצאו
If wordExists Then
MsgBox "המילים '" & awords(i) & "' כבר קיימות בקובץ.", vbInformation
Else
' אם המילים אינם קיימות, הוסף אותם לקובץ
Open filePath For Append As fileNumber
Print #fileNumber, awords(i)
Close fileNumber
MsgBox "המילים '" & awords(i) & "' נוספו לקובץ.", vbInformation
End If
End If
Exit Sub
Else
Dim rng As Range
For Each line In lines
If Trim(line) <> "" Then
replac = Split(line, "-")
' החלפת המילים שבקובץ
Set rng = ActiveDocument.Content
With rng.Find
.Text = replac(0)
.Replacement.Text = replac(1)
.Forward = True
.Wrap = wdFindContinue
.Format = True
.MatchCase = False
.Execute Replace:=wdReplaceAll
.MatchWildcards = True
End With
End If
Next line
End If
End Sub
כתיבת הערכים בקובץ היא בכל שורה כך על ידי-ע""י (כדי שהוא יבין שאתה רוצה סימון של גרשיים צריך לעשות שניים רצופים)
לפעמים בפעמיים הראשונות הוא לא מצליח להוסיף לבד ולכן תעשה זאת ביצירת הקובץ, אח"כ ניתן להפעיל את המאקרו כש capslock דלוק ואז יקפוץ חלון להוספת ערכים
בהצלחה!