.TXTファイルのテキストを使用するには、Open ... For Inputメソッドを使用できます。その.TXTファイル内の単語と行をループして、文書内の検索/置換を実行できます。
Sub DeleteWordsStoredInTXTFileFromThisDoc()
Dim filePath As String
' change the filePath to point to your file
' you can put the full path, for example "C:\foldername\filename.txt"
filePath = Environ("USERPROFILE") & "\Desktop\textfile.txt"
Open filePath For Input As 1
Dim LineFromFile As String
Do Until EOF(1)
Line Input #1, LineFromFile
Dim LineItems() As String
Dim LineItem As Variant
' depending on how you are listing your naughty words you need to pick from the following lines
' if your words are separated by commas
'LineItems = Split(LineFromFile, ",")
' if your words are separated onto different lines
LineItems = Split(LineFromFile, Chr(13))
For Each LineItem In LineItems
With Selection.Find
.Text = LineItem
.Replacement.Text = ""
.Execute MatchWholeWord:=True, Replace:=wdReplaceAll, Forward:=True, Wrap:=wdFindContinue
End With
Next LineItem
Loop
Close #1
End Sub
削除する単語のあなたの.TXTリストは、単語の前または後のスペースが含まれていないならば、あなたが残してしまいますので注意してください:ここでは
は、あなたが適応できるチョキコードですあなたのドキュメントの余分なスペースの後ろに。あなたはおそらくたいとき
"Big wolf"
が得られます「ビッグ悪い狼」から「悪い」という言葉を削除する「悪い」または「悪い」はこれを防ぐことができますようにあなたの単語リストにスペースを追加する
"Big wolf"
。あなたは明らかに文書を校正する必要があります。
ありがとうございました! VBAはチャンピオンのように働いた。私は助けに感謝します。 --dave – user1704475