- PR -

VB.NETでShellExecuteExを使用する

投稿者投稿内容
KeroKero
常連さん
会議室デビュー日: 2003/11/06
投稿数: 26
投稿日時: 2003-11-11 12:59
引用:

----------source----------
Private Sub PrintDoc(ByVal APrintDocName As String)
  Dim myProcess As New Process()
  Try
    '''成功
    myProcess.StartInfo.FileName = APrintDocName
    myProcess.StartInfo.Verb = "Print"
    myProcess.StartInfo.CreateNoWindow = True
    myProcess.Start()
    myProcess.WaitForExit(5000)

    '''失敗(関連付けられているプロセスはありません)
    'Dim startInfo As New ProcessStartInfo("AcroRd32.exe")
    'With startInfo
    '  .FileName = APrintDocName
    '  .Verb = "print"
    '  .CreateNoWindow = True
    'End With
    'myProcess.Start(startInfo)
    'myProcess.WaitForExit(5000)
  Catch e As Exception
    MsgBox(e.Message)
  Finally
    myProcess.Kill()
  End Try
End Sub



余談ですが,ちょっと変えれば最初のコードでも動くと思います。

Private Sub PrintDoc(ByVal APrintDocName As String)
  Dim myProcess As Process
  Try
    Dim startInfo As New ProcessStartInfo("AcroRd32.exe")
    With startInfo
      .FileName = APrintDocName
      .Verb = "print"
      .CreateNoWindow = True
    End With
    myProcess = Process.Start(startInfo)
    myProcess.WaitForExit(5000)
  Catch e As Exception
    MsgBox(e.Message)
  Finally
    myProcess.Kill()
  End Try
End Sub

このやり方の場合,myProcessがnothingかどうか判定できます。
実験してみましたが,IEを使用する場合は,すでにIEが起動しているとそのIEを再利用するため,myProcessはnothingになるようです。同様に,myProcess.Start()を使用しても,myProcessに関連付かないようです。
#URLを指定すると,先にIEが起動していなくてもnothingでした。失礼しました。

プロセスを取得するにはGetProcessesByNameを使う方法があるようですが。
http://support.microsoft.com/default.aspx?scid=kb;JA;305602


[ メッセージ編集済み 編集者: KeroKero 編集日時 2003-11-11 13:21 ]
空条Q太郎
会議室デビュー日: 2003/11/06
投稿数: 19
投稿日時: 2003-11-12 21:05
返事が遅くなりすみませんでした。

"static"についてですが

----------問題の部分----------
>>引数有りのStartメソッドはstaticなんですね。
>staticでないと思うのですが。

>毎度申し訳ございませんが、宜しくお願い致します。

>Private Sub PdfPrint(ByVal Url As String)
>  Dim myProcess As New Process()
>  Try
>    myProcess.StartInfo.FileName = Url
>    myProcess.StartInfo.Verb = "Open"
>    myProcess.StartInfo.CreateNoWindow = False
>    myProcess.Start()
>    myProcess.WaitForExit(5000)'''エラー箇所
>  Catch e As Exception
>    MsgBox(e.Message)
>  Finally
>    myProcess.Kill()''''''''''''''エラー箇所
>  End Try
>End Sub
>'※エラー箇所の内容は"このオブジェクトに関連付けられているプロセスはありません。"です。
-------------------------------

でしたが、真意は
"下記コード(今は上記コード)は静的なStartメソッドを使用している訳ではないのに、
なぜエラーになるのでしょうか?"
とういうことだったのです。決して、
"引数有りのStartメソッドがstaticではない"
とういうことではありません。

フォローして頂いたKeroKeroさん、なちゃさん、よねKENさん、
そして、なにより直接ご迷惑をお掛けすることになりましたJubeiさん
私の落ち度が誤解を招くことになり本当にすみませんでした。
空条Q太郎
会議室デビュー日: 2003/11/06
投稿数: 19
投稿日時: 2003-11-12 21:09
返事が遅くなりすみませんでした。

"IEのプロセスが落ちない"についてですが
KeroKeroさんからの

>プロセスを取得するにはGetProcessesByNameを使う方法があるようですが。

で上手くIEは終了しましたが、起動しているIE全て(閲覧中など)を終了するのは
少しまずいので今後に生かしたいと思います。

同じくKeroKeroさんからの

>実験してみましたが,IEを使用する場合は,すでにIEが起動しているとそのIEを再利用するため,
>myProcessはnothingになるようです。同様に,myProcess.Start()を使用しても,
>myProcessに関連付かないようです。#URLを指定すると,先にIEが起動していなくてもnothingでした。

からIEをエクスプローラーと同じと考えるとURL指定の場合は常に再利用するため
nothingになりエラーになるのではないでしょうか?。

そこで
"iexplore.exe"をfileNameにUrlをArgumentsにセットし
IEを新規起動しUrl指定するとういう下記コードで無事IEのプロセスを終了することができました。
長くフォローをして頂きました皆さん本当にありがとうございました。
今後も宜しくお願い致します。

----------source----------
Public Shared Sub PdfPrint(ByVal Url As String)
  Dim myProcess As New Process()
  '''1
  Try
    Dim startInfo As New ProcessStartInfo("iexplore.exe", Url)-------○
'Dim startInfo As New ProcessStartInfo(Url)----------------------×
    With startInfo
      .Verb = "open"
      .WindowStyle = ProcessWindowStyle.Hidden
    End With
    myProcess = Process.Start(startInfo)
    myProcess.WaitForExit(5000)
  Catch e As Exception
    MsgBox(e.Message)
  Finally
    myProcess.Kill()
  End Try

  '''2
  Try
    myProcess.StartInfo.FileName = "iexplore.exe"--------------------○
    myProcess.StartInfo.Arguments = Url------------------------------○
myProcess.StartInfo.FileName = Url-------------------------------×
    myProcess.StartInfo.Verb = "open"
    myProcess.StartInfo.WindowStyle = ProcessWindowStyle.Hidden
    myProcess.Start()
    myProcess.WaitForExit(5000)
  Catch e As Exception
    MsgBox(e.Message)
  Finally
    myProcess.Kill()
  End Try
End Sub
--------------------------

スキルアップ/キャリアアップ(JOB@IT)