0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Word VBA 1行づつ余計な改行が入っている場合に1行だけ改行させるマクロ

Last updated at Posted at 2019-12-10

Word :空白行を一括削除するには
http://office-qa.com/Word/wd394.htm

というのをいつも使っていつも忘れるのでVBA化しました。
なんのことだかわからない人は紹介したサイトを見てください。
しかし

※空白行は前の段落から数えて段落記号が2個以上あるからです。
※13で置換するとレイアウトが崩れるため^pを指定します。

このあたり本当にどこに書いてあるんだ。。。
しかも改行と自分が言っているのは本当は段落記号らしい。
ちなみに元サイトは2つ以上の連続ですが、こちらは1つだけにしています。

Sub WordTwoLinesOnelineJpn()
' For Word 2007 Later
' 日本語用 For Japanese
With Selection
.EndKey Unit:=wdStory 'いったん文末に飛ばし、文頭に飛ばす
.HomeKey Unit:=wdStory
.Find.ClearFormatting '段落書式指定をクリア
.Find.Replacement.ClearFormatting 'Removes text and paragraph formatting from the text specified in a replace operation.
.Find.ClearAllFuzzyOptions 'Clears all nonspecific search options associated with Japanese text.
.Find.ClearHitHighlight
With .Find
.Text = "^13^13"
.Replacement.Text = "^p"
.Forward = True
.LanguageIDFarEast = wdJapanese '日本語以外では設定を変えるか省略する
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchByte = False
.MatchAllWordForms = False
.MatchSoundsLike = False
.MatchFuzzy = False
.MatchWildcards = True
'.Execute2007 'Selection.Find.Execute2007(FindText, MatchCase, MatchWholeWord, MatchWildcards, MatchSoundsLike, MatchAllWordForms, Forward, Wrap, Format, ReplaceWith, Replace, MatchKashida, MatchDiacritics, MatchAlefHamza, MatchControl, MatchPrefix, MatchSuffix, MatchPhrase, IgnoreSpace, IgnorePunct)
.Execute Replace:=wdReplaceAll 'ここを外さない 'Selection.Find.Execute (FindText, MatchCase, MatchWholeWord, MatchWildcards, MatchSoundsLike, MatchAllWordForms, Forward, Wrap, Format, ReplaceWith, Replace, MatchKashida, MatchDiacritics, MatchAlefHamza, MatchControl)
End With
End With
End Sub
0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?