Skip to content

Instantly share code, notes, and snippets.

@eugrus
Last active October 31, 2025 12:58
Show Gist options
  • Select an option

  • Save eugrus/f0de41d18f42e997050f8659d8fb03c0 to your computer and use it in GitHub Desktop.

Select an option

Save eugrus/f0de41d18f42e997050f8659d8fb03c0 to your computer and use it in GitHub Desktop.
function NormalizeNonBreakingSpaceInBeck {
try {
$word = [Runtime.InteropServices.Marshal]::GetActiveObject("Word.Application")
} catch {
throw "Keine aktive Word-Instanz gefunden."
}
$doc = $word.ActiveDocument
$wdFindContinue = 1
$wdReplaceAll = 2
function Replace-AllInStories {
param(
[__ComObject]$Document,
[string]$FindText,
[string]$ReplaceText
)
foreach ($storyStart in $Document.StoryRanges) {
$rng = $storyStart.Duplicate
while ($rng) {
$find = $rng.Find
$find.ClearFormatting()
$find.Replacement.ClearFormatting()
$null = $find.Execute(
$FindText, $false, $false, $false, $false, $false,
$true, $wdFindContinue, $false, $ReplaceText, $wdReplaceAll
)
$rng = $rng.NextStoryRange
}
}
}
# Step 1: Replace ONLY NBSP (^s and ^0160) with plain space
Replace-AllInStories -Document $doc -FindText "^s" -ReplaceText " "
Replace-AllInStories -Document $doc -FindText "^0160" -ReplaceText " "
# Step 2: Replace "§ " with "§^0160" (section sign followed by NBSP)
Replace-AllInStories -Document $doc -FindText "§ " -ReplaceText "§^0160"
}
NormalizeNonBreakingSpaceInBeck
Sub NormalizeNonBreakingSpaceInBeck()
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = "^s"
.Replacement.Text = " "
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute Replace:=wdReplaceAll
With Selection.Find
.Text = "§ "
.Replacement.Text = "§^s"
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute Replace:=wdReplaceAll
End Sub
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment