プロが教えるわが家の防犯対策術!

コマンドボタンからダイアログにいきキャンセルしたときに
上手く戻れずにエラーが返ってきてしまいます。

流れ
①MENUフォーム→②フォーム1→③ダイアログから画像選択
④間違えてキャンセル→⑤フォーム1に戻る
ようにしたいです。
エラーが返ってくるのでフォーム1をunloadし、キャンセルが返ってきたら再表示
になるようにしてみたもののエラーが返ってきます。実行時エラー5152
★の行がエラーとなる。
キャンセルしたときにendを選択するとフォームすべてが終了してしますうので
何かいい方法ありませんか?

Private Sub CommandButton2_Click()

Dim PIC As Shape
Dim shp As Word.Shape
Dim pg As Long
Dim i As Integer
Dim oDialog As Word.Dialog

' 図の挿入ダイアログで図を指定
Set oDialog = Dialogs(wdDialogInsertPicture)
With oDialog
.Display
Unload UserForm1
If Application.FileDialog(msoFileDialogFilePicker).SelectedItems.Count >= 1 Then
MsgBox Application.FileDialog(msoFileDialogFilePicker).SelectedItems(1)

Else
MsgBox "キャンセル"
UserForm1.Show vbModeless

End If
' 各ページごとの処理
pg = Selection.Information(wdNumberOfPagesInDocument)
For i = 1 To pg
Selection.GoTo What:=wdGoToPage, _
Which:=wdGoToAbsolute, _
Count:=i

' 画像処理
★ Set PIC = ActiveDocument.Shapes.AddPicture(filename:=.Name)
With PIC
'ファイル選択ダイアログでキャンセルした場合に分岐する

.RelativeHorizontalPosition = _
wdRelativeHorizontalPositionPage
.RelativeVerticalPosition = _
wdRelativeVerticalPositionPage
'.Left = wdShapeCenter
'.Top = wdShapeCenter
.Left = TextBox1.Value
.Top = TextBox2.Value
End With

Set PIC = Nothing
Next


End With

Set oDialog = Nothing

End Sub

A 回答 (2件)

#1です


あらためてコードを読んでみました。
#1の回答よりもっと前段で振り分けるべきのようですね。

Application.FileDialog(msoFileDialogFilePicker)などの使用意図がイマイチ分からないので#1を書き直してみました。

Private Sub CommandButton2_Click()

Dim PIC As Shape
Dim shp As Word.Shape
Dim pg As Long
Dim i As Integer
Dim oDialog As Word.Dialog

If Not IsNumeric(TextBox1.Value) And Not IsNumeric(TextBox1.Value) Then
MsgBox ("テキストボックスの値が不正です")
Exit Sub
End If

' 図の挿入ダイアログで図を指定
Set oDialog = Dialogs(wdDialogInsertPicture)
With oDialog
.Display
Unload UserForm1
If .Name <> "" Then
MsgBox .Name
Else 'ファイル選択ダイアログでキャンセルした場合に分岐する
MsgBox "キャンセル"
UserForm1.Show vbModeless
Exit Sub
End If

' 各ページごとの処理
pg = Selection.Information(wdNumberOfPagesInDocument)
For i = 1 To pg
Selection.GoTo What:=wdGoToPage, _
Which:=wdGoToAbsolute, _
Count:=i
' 画像処理
Set PIC = ActiveDocument.Shapes.AddPicture(FileName:=.Name)
With PIC
.RelativeHorizontalPosition = _
wdRelativeHorizontalPositionPage
.RelativeVerticalPosition = _
wdRelativeVerticalPositionPage
'.Left = wdShapeCenter
'.Top = wdShapeCenter
.Left = TextBox1.Value
.Top = TextBox2.Value
End With
Set PIC = Nothing
Next
End With

Set oDialog = Nothing

End Sub

MsgBox .Name についてはファイル名のみを表示したい場合は
文字列の右から\までを取得するような加工などが必要です
    • good
    • 0
この回答へのお礼

回答ありがとうございます。
Application.FileDialog(msoFileDialogFilePicker)についてはネットにてダイアログのキャンセルの時の分岐について記載があったので入れてみたりしていました。
おかげさまで考えていたとおりの動きを確認することができました。
ありがとうございました。

お礼日時:2021/12/14 23:42

こんにちは


word_VBAは詳しくないのですが、
oDialog.Nameがキャンセルによって取得できない為ではないかと思います。
キャンセルなのでその後の実行は必要ないかと、、ループ処理内なので
条件設定を加えて、その後の処理を行うか、飛ばすのか、処理分岐すれば良いように思います

iループ部分の提案
For i = 1 To pg
Selection.GoTo What:=wdGoToPage, _
Which:=wdGoToAbsolute, _
Count:=i
' 画像処理
If .Name <> "" Then 'ファイル選択ダイアログでキャンセルした場合に分岐する
Set PIC = ActiveDocument.Shapes.AddPicture(FileName:=.Name)
With PIC
.RelativeHorizontalPosition = _
wdRelativeHorizontalPositionPage
.RelativeVerticalPosition = _
wdRelativeVerticalPositionPage
'.Left = wdShapeCenter
'.Top = wdShapeCenter
.Left = TextBox1.Value
.Top = TextBox2.Value
End With
Set PIC = Nothing
End If
Next

*ほぼコピー、未検証なのでデバッグを行ってください。
    • good
    • 0

お探しのQ&Aが見つからない時は、教えて!gooで質問しましょう!