ドキュメントを特定の方法でフォーマットしてから、 を使用して保存するマクロがありますActiveDocument.Save
。
ただし、ドキュメントがまだ保存されていない場合もあれば、保存したくない場合もあります。残念ながら、[名前を付けて保存] ダイアログが表示されているときに [キャンセル] をクリックすると、実行時エラー (4198) が発生します -
コマンドが失敗しました
これを防ぐ方法を知っている人はいますか?ありがとう。
エラー処理手順を追加して、次のことを試してください。
On Error Resume Next 'to omit error when cancel is pressed
ActiveDocument.Save
If Err.Number <> 0 Then 'optional, to confirmed that is not saved
MsgBox "Not saved"
End If
On Error GoTo 0 'to return standard error operation
更新: 現在
1. ファイルが以前に保存されているかどうかをテストします
。 2. ファイルが保存されていない場合、制御されたプロセスを使用してSaveAs
ダイアログを表示し、ファイルを保存するか、Cancel
コード
Dim bSave As Boolean
If ActiveDocument.Path = vbNullString Then
bSave = Application.Dialogs(wdDialogFileSaveAs).Show
If Not bSave Then MsgBox "User cancelled", vbCritical
Else
ActiveDocument.Save
End If
vsto 開発者の方は、こちらをご覧ください
if (Globals.ThisAddIn.Application.ActiveDocument.Path == String.Empty)
{
Word.Dialog dlg;
Object timeout = 3000;
dlg = Globals.ThisAddIn.Application.Dialogs[
Word.WdWordDialog.wdDialogFileSaveAs];
int result = dlg.Display(ref timeout);
}
else
{
Globals.ThisAddIn.Application.ActiveDocument.Save();
}
結果は、どのボタンが押されたかを保存します (0 - キャンセル、1 - OK、2 - 閉じる)