2016-09-10 28 views
0

私はExcelサーバーに接続されたExcelブックを持っています。私はデータをリフレッシュするためのリフレッシュボタンを配置し、シートの保護されていないステートメントを保持しています。excel vbaにDSNが存在するか確認してください

私の問題は、Excelファイルを開いてネットワークにないコンピュータで最新表示をクリックするとDSN作成ウィザードが表示され、ウィザードでキャンセルを押すとシートが保護されなくなります。

私は、DSNが利用可能かどうかをチェックし、そうでない場合はサブを終了する必要があるかどうかをチェックするIF条件を設定したいとします。

アイデア?

これは、エラーハンドラを持つ私のコードですが、私はまだDSNの作成ウィザードを取得し、メッセージボックスは、次のようにコードを変更することにより、シートが保護されていない

On Error GoTo handler 
Application.ScreenUpdating = False 
Sheets("DEC-2015").Unprotect Password:="password" 
ActiveWorkbook.Connections("Query from Sample").Refresh 
Sheets("DEC-2015").Protect _ 
Password:="password", _ 
UserInterfaceOnly:=True, _ 
AllowFiltering:=True, _ 
AllowSorting:=True, _ 
AllowUsingPivotTables:=True 
handler: 
MsgBox "Server Connection Lost...", vbOKOnly + vbCritical, "Warning" 
Exit Sub 
+0

ちょうどスタブで、 'Application.DisplayAlerts = False'でアラートをオフにしてみてください。 – Gareth

+0

それはトリックでしたが、それでも私のシートを保護しませんでした(これは私の最大の関心事です:() – Danny

答えて

0

ターンアラートを閉鎖された後:

Public Sub DoSomething() 

    On Error GoTo handler 

    With Application 
     .ScreenUpdating = False 
     .DisplayAlerts = False 
    End With 

    With ThisWorkbook 
     .Sheets("DEC-2015").Unprotect Password:="password" 
     .Connections("Query from Sample").Refresh 
     .Sheets("DEC-2015").Protect _ 
      Password:="password", _ 
      UserInterfaceOnly:=True, _ 
      AllowFiltering:=True, _ 
      AllowSorting:=True, _ 
      AllowUsingPivotTables:=True 
    End With 

    With Application 
     .ScreenUpdating = True 
     .DisplayAlerts = True 
    End With 

    Exit Sub 

    handler: 
     ThisWorkbookSheets("DEC-2015").Protect _ 
      Password:="password", _ 
      UserInterfaceOnly:=True, _ 
      AllowFiltering:=True, _ 
      AllowSorting:=True, _ 
      AllowUsingPivotTables:=True 

     With Application 
      .ScreenUpdating = True 
      .DisplayAlerts = True 
     End With 

     MsgBox "Server Connection Lost...", vbOKOnly + vbCritical, "Warning" 

End Sub 
関連する問題