【Word VBA】特定ファイルの段落書式を取り込むWordマクロ(その2)

2018年5月9日

以前紹介した「【コード】特定ファイルの段落書式を取り込むWordマクロ」の更新です。

前回の記事では、[段落]ダイアログボックスの[インデントと行間隔]タブと[体裁]タブのみを処理対象としていました。

今回の記事では、[改ページと改行]タブの設定を追加しました。

このマクロでできること

現在のファイルの段落書式を、特定のファイルの設定と同じようにします。

前回のマクロでの設定項目に加え、以下の6項目も設定できるようにしました。

マクロの解説

以前、「【アプリ紹介】ちゃうちゃう2.0 文字列比較の定番ソフト」でも紹介したとおり、ツールを使えば簡単にダイアログボックスのチェックボックスに該当するプロパティ名を調べられます。

今回も同じ方法で6つの項目を調べ、以前のマクロに追記しました(86行目~105行目)。

マクロ

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
Sub 段落書式をコピーするマクロ2()
 
 Dim myFilePath As String ' Wordファイルパス
 Dim myFD As FileDialog
 Dim myDoc As Document
 Dim myTempDoc As Document
 
 Set myFD = Application.FileDialog(msoFileDialogFilePicker)
 
 '------------------------------------------------
 'Wordファイルの選択
 '------------------------------------------------
 With myFD
 
  .AllowMultiSelect = False
  .Title = "Wordファイルを選択してください"
 
  With .Filters
   .Clear
   .Add "すべてのWordファイル", "*.doc; *.docx"
  End With
 
  If .Show = -1 Then
   myFilePath = .SelectedItems(1)
   .Filters.Clear
  Else
   .Filters.Clear
   Exit Sub
  End If
 
 End With
 
 '------------------------------------------------
 '段落書式をコピーする
 '------------------------------------------------
 If Dir(myFilePath, vbNormal) <> "" Then
 
  '現在の文書をmyDocオブジェクトに設定する
  Set myDoc = ActiveDocument
 
  'テンプレートファイルを開く
  '保護されたビューにならないように「読取り専用」で開く
  Set myTempDoc = Documents.Open(FileName:=myFilePath, ReadOnly:=True, Visible:=False)
 
  '-----------------------------------
  '段落書式設定
  '-----------------------------------
  With myDoc.Range.ParagraphFormat
 
   '-------------------------------------------
   '「インデントと行間隔」タブ
   '-------------------------------------------
   '「1ページの行数を指定時に文字を行グリッド線に合わせる」
   .DisableLineHeightGrid = myTempDoc.Range.ParagraphFormat.DisableLineHeightGrid
 
   '行間
   .LineSpacingRule = myTempDoc.Range.ParagraphFormat.LineSpacingRule
 
   '間隔
   .LineSpacing = myTempDoc.Range.ParagraphFormat.LineSpacing
 
   '段落後の間隔
   .SpaceAfter = myTempDoc.Range.ParagraphFormat.SpaceAfter
 
   '段落前の間隔
   .SpaceBefore = myTempDoc.Range.ParagraphFormat.SpaceBefore
 
   '-------------------------------------------
   '「体裁」タブ
   '-------------------------------------------
   '「禁則処理を行う」
   .FarEastLineBreakControl = myTempDoc.Range.ParagraphFormat.FarEastLineBreakControl
 
   '「英単語の途中で改行する」
   .WordWrap = myTempDoc.Range.ParagraphFormat.WordWrap
 
   '「句読点のぶら下げを行う」
   .HangingPunctuation = myTempDoc.Range.ParagraphFormat.HangingPunctuation
 
   '「日本語と英字の間隔を自動調整する」
   .AddSpaceBetweenFarEastAndAlpha = myTempDoc.Range.ParagraphFormat.AddSpaceBetweenFarEastAndAlpha
 
   '「日本語と数字の間隔を自動調整する」
   .AddSpaceBetweenFarEastAndDigit = myTempDoc.Range.ParagraphFormat.AddSpaceBetweenFarEastAndDigit
   
   '-------------------------------------------
   '「改ページと改行」タブ
   '-------------------------------------------
   '改ページ時1行残して段落を区切らない
   .WidowControl = myTempDoc.Range.ParagraphFormat.WidowControl
   
   '次の段落と分離しない
   .KeepWithNext = myTempDoc.Range.ParagraphFormat.KeepWithNext
   
   '段落を分割しない
   .KeepTogether = myTempDoc.Range.ParagraphFormat.KeepTogether
   
   '段落前で改ページする
   .PageBreakBefore = myTempDoc.Range.ParagraphFormat.PageBreakBefore
   
   '行番号を表示しない
   .NoLineNumber = myTempDoc.Range.ParagraphFormat.NoLineNumber
   
   'ハイフネーションなし
   .Hyphenation = myTempDoc.Range.ParagraphFormat.Hyphenation
   
  End With
  DoEvents
 
  myTempDoc.Close SaveChanges:=wdDoNotSaveChanges
  DoEvents
 
  Set myTempDoc = Nothing
  Set myDoc = Nothing
 
 End If
 
 Set myFD = Nothing
 
End Sub

-コード
-, ,

S