1つの文書内にあるすべての画像を全く同一の画像に差し替えようとしています。
以上のコメントを受けて、次のように変更しました(2023/06/27 10:53)。
vba
1 Sub ResetAllPicturesInActiveDocument ( )
2
3 Dim strPicturePath As String
4 Dim lngResetCount As Long
5
6 strPicturePath = "C:\FolderName\FileName.png"
7
8 Dim wrdShape As Word . Shape
9
10 For Each wrdShape In ActiveDocument . Shapes
11 Select Case wrdShape . Type
12 Case msoPicture , msoLinkedPicture
13 If ResetPicture ( wrdShape , strPicturePath ) Is Nothing Then
14 Exit Sub
15 End If
16 lngResetCount = lngResetCount + 1
17 Case Else
18 '何もしない
19 End Select
20 Next
21
22 If lngResetCount > 0 Then
23 MsgBox "この文書の描画レイヤーの図を " & lngResetCount & " 個差し替えました。" , _
24 vbInformation , _
25 "実行完了 (ResetAllPicturesInActiveDocument)"
26 Else
27 MsgBox "この文書の描画レイヤーに挿入されている図はありません。" , _
28 vbInformation , _
29 "実行完了 (ResetAllPicturesInActiveDocument)"
30 End If
31
32 End Sub
33
34 Function ResetPicture ( TargetShape As Word . Shape , NewPicturePath As String ) As Word . Shape
35 On Error GoTo Err_ResetPicture
36
37 If Dir ( NewPicturePath ) = "" Then
38 MsgBox "指定されたパス""" & NewPicturePath & """に該当するファイルが見つかりません。" , _
39 vbExclamation , _
40 "ファイル参照エラー (ResetPicture)"
41 Exit Function
42 End If
43
44 Dim strShapeName As String
45 Dim varLeftRelative As Variant
46 Dim varRelativeHorizontalPosition As Variant
47 Dim varLeft As Variant
48 Dim varTopRelative As Variant
49 Dim varRelativeVerticalPosition As Variant
50 Dim varTop As Variant
51
52 With TargetShape
53 Select Case . Type
54 Case msoPicture , msoLinkedPicture
55 '何もしない
56 Case Else
57 MsgBox "図形""" & . Name & """は図ではありません。" , _
58 vbExclamation , _
59 "オブジェクト参照エラー (ResetPicture)"
60 Exit Function
61 End Select
62
63 . Select
64 strShapeName = . Name
65 varLeftRelative = . LeftRelative
66 varRelativeHorizontalPosition = . RelativeHorizontalPosition
67 If . LeftRelative = wdShapePositionRelativeNone Then
68 varLeft = . Left
69 End If
70 varTopRelative = . TopRelative
71 varRelativeVerticalPosition = . RelativeVerticalPosition
72 If . TopRelative = wdShapePositionRelativeNone Then
73 varTop = . Top
74 End If
75 End With
76
77 Application . ScreenUpdating = False
78
79 Dim wrdNewShape As Word . Shape
80
81 Set wrdNewShape = ActiveDocument . Shapes . AddPicture ( FileName : = NewPicturePath , _
82 LinkToFile : = False )
83
84 With TargetShape
85 . LeftRelative = wdShapePositionRelativeNone
86 . RelativeHorizontalPosition = wdRelativeHorizontalPositionPage
87 . TopRelative = wdShapePositionRelativeNone
88 . RelativeVerticalPosition = wdRelativeVerticalPositionPage
89 End With
90
91 With wrdNewShape
92
93 . WrapFormat . Type = TargetShape . WrapFormat . Type
94
95 . LeftRelative = wdShapePositionRelativeNone
96 . RelativeHorizontalPosition = wdRelativeHorizontalPositionPage
97 . Left = TargetShape . Left
98
99 . TopRelative = wdShapePositionRelativeNone
100 . RelativeVerticalPosition = wdRelativeVerticalPositionPage
101 . Top = TargetShape . Top
102
103 . LeftRelative = varLeftRelative
104 . RelativeHorizontalPosition = varRelativeHorizontalPosition
105 If . LeftRelative = wdShapePositionRelativeNone Then
106 . Left = varLeft
107 End If
108
109 . TopRelative = varTopRelative
110 . RelativeVerticalPosition = varRelativeVerticalPosition
111 If . TopRelative = wdShapePositionRelativeNone Then
112 . Top = varTop
113 End If
114
115 Do While . ZOrderPosition > TargetShape . ZOrderPosition
116 . ZOrder msoSendBackward
117 Loop
118
119 End With
120
121 Exit_ResetPicture :
122 On Error Resume Next
123
124 If Not wrdNewShape Is Nothing Then
125 TargetShape . Delete
126 wrdNewShape . Name = strShapeName
127 Set ResetPicture = wrdNewShape
128 End If
129
130 Application . ScreenUpdating = True
131
132 Exit Function
133
134 Err_ResetPicture :
135
136 MsgBox Err . Number & ": " & Err . Description , _
137 vbCritical , _
138 "実行時エラー (ResetPicture)"
139
140 If Not wrdNewShape Is Nothing Then
141 wrdNewShape . Delete
142 Set wrdNewShape = Nothing
143 End If
144
145 Resume Exit_ResetPicture
146 End Function