質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
VBA

VBAはオブジェクト指向プログラミング言語のひとつで、マクロを作成によりExcelなどのOffice業務を自動化することができます。

Q&A

解決済

2回答

2561閲覧

指定のURLをIE又はChromeで接続したい

ice930

総合スコア99

VBA

VBAはオブジェクト指向プログラミング言語のひとつで、マクロを作成によりExcelなどのOffice業務を自動化することができます。

0グッド

0クリップ

投稿2020/09/09 13:53

ユーザーフォームを作り、ボタンを押すとIE又はChromeで任意で指定のURLへ接続するものを作りたく、以下のようなものを作成しました。
現在Chromeのみ接続される状態で、IEボタンを押すと反応しません。

作成手順として、IEボタンを作り、その後Chromeボタンを作成しました。
IEボタン単体の状態では問題なく接続されたのですが、chromeボタンを追加すると反応しなくなりました。

「InternetExplorer.Application」と「WScript.Shell」の両方を動かすことは不可能なのでしょうか?(起動は別々だとは思いますが・・・。)

今後の為、何が起きたか知りたく質問させていただきました。
よろしくお願いします。
イメージ説明
以下は問題と思われる部分のコードです
ユーザーフォーム

Dim IELbtn As CommandButton Dim CHLbtn As CommandButton Set IELbtn = .Controls.Add("Forms.CommandButton.1", "IEL", True) Set CHLbtn = .Controls.Add("Forms.CommandButton.1", "CHL", True) Call ctrlURL.SetCtrl(IELbtn, Cells(row, 5)) '’’’’’’’’’’’’’’ Call ctrlURL.SetCtrl(CHLbtn, Cells(row, 5)) With IELbtn .Top = 64 .Left = 10 .Height = 20 .Width = 100 .Caption = "IEでアクセス" End With With CHLbtn .Top = 64 .Left = 120 .Height = 20 .Width = 100 .Caption = "Chromeでアクセス" End With

クラス

If tgtCtrl.Name = "IEL" Then 'Urlボタンの表示 intRow = tgtRange.row URL = Cells(intRow, 5).Copy MsgBox "IEでインターネットへ接続します。" Set ie = CreateObject("InternetExplorer.Application") ie.Navigate Cells(intRow, 5) ie.Visible = True Exit Sub End If If tgtCtrl.Name = "CHL" Then 'Urlボタンの表示 intRow = tgtRange.row URL = Cells(intRow, 5).Copy MsgBox "Chromeでインターネットへ接続します。" Set Ch = CreateObject("WScript.Shell") Ch.Run ("chrome.exe -url " & Cells(intRow, 5)) Set Ch = Nothing Exit Sub End If

以下は全てのコードです
こちらがユーザーフォームです

Private ctrlbtn(1 To 100) As New EventButtonClass Private ctrlURL As New EventButtonClass Sub UserForm_Initialize() Dim Myitem As Object Dim MyPass As Object Dim i As Long Dim Top As Long '項目入力開始位置 Dim row As Long Dim Col As Long Dim item As String row = ActiveCell.row '行番号取得 With UserForm3 Top = 50 '項目開始位置 With .Controls.Add("Forms.Label.1", "タイトル", True) 'タイトル .Top = 10 'Top位置(表示位置を移動する) .Left = 10 'Left位置 .Height = 20 '高さ .Width = 370 '幅 .BorderStyle = fmBorderStyleSingle '枠線 .BackColor = RGB(128, 128, 128) '背景色 .ForeColor = RGB(255, 255, 255) '文字色 .Font.Name = "メイリオ" 'テキストのスタイル .TextAlign = 2 'テキストの位置 .FontSize = 16 'テキストのサイズ .Caption = Cells(row, 3) End With If Cells(row, 5) <> "" Then 'URL有れば Dim IELbtn As CommandButton Dim CHLbtn As CommandButton Set IELbtn = .Controls.Add("Forms.CommandButton.1", "IEL", True) Set CHLbtn = .Controls.Add("Forms.CommandButton.1", "CHL", True) Call ctrlURL.SetCtrl(IELbtn, Cells(row, 5)) '’’’’’’’’’’’’’’ Call ctrlURL.SetCtrl(CHLbtn, Cells(row, 5)) With IELbtn .Top = 64 .Left = 10 .Height = 20 .Width = 100 .Caption = "IEでアクセス" End With With CHLbtn .Top = 64 .Left = 120 .Height = 20 .Width = 100 .Caption = "Chromeでアクセス" End With With .Controls.Add("Forms.Label.1", "url", True) .Top = 34 'Top位置(表示位置を移動する) .Left = 10 'Left位置 .Height = 20 '高さ .Width = 370 '幅 .BorderStyle = fmBorderStyleSingle '枠線 .BackColor = RGB(128, 128, 128) '背景色 .ForeColor = RGB(255, 255, 255) '文字色 .Font.Name = "メイリオ" 'テキストのスタイル .TextAlign = 2 'テキストの位置 .FontSize = 16 'テキストのサイズ .Caption = Cells(row, 5) End With Top = Top + 24 End If item = "初期値" Col = 5 i = 0 Do While item <> "" Col = Col + 1 item = Cells(row, Col) If item = "" Then Exit Do End If i = i + 1 Set Myitem = .Controls.Add("Forms.Label.1", "MyLabel" & i, True) Set MyPass = .Controls.Add("Forms.Label.1", "MyPass" & i, True) With Myitem 'ここは項目表示 .Top = Top + 24 * i 'Top位置(表示位置を移動する) .Left = 70 'Left位置 .Height = 20 '高さ .Width = 150 '幅 .BorderStyle = fmBorderStyleSingle '枠線 .BackColor = RGB(128, 128, 128) '背景色 .ForeColor = RGB(255, 255, 255) '文字色 .Font.Name = "メイリオ" 'テキストのスタイル .TextAlign = 2 'テキストの位置 .FontSize = 16 'テキストのサイズ .Caption = item End With Col = Col + 1 item = Cells(row, Col) With MyPass 'ここは内容表示 .Top = Top + 24 * i .Left = 230 'Left位置 .Height = 20 '高さ .Width = 150 '幅 .BorderStyle = fmBorderStyleSingle '枠線 .BackColor = RGB(128, 128, 128) '背景色 .ForeColor = RGB(255, 255, 255) '文字色 .Font.Name = "メイリオ" 'テキストのスタイル .TextAlign = 2 'テキストの位置 .FontSize = 16 'テキストのサイズ .Caption = item End With Dim Cpybtn As CommandButton Set Cpybtn = .Controls.Add("Forms.CommandButton.1", "copy" & i, True) Call ctrlbtn(i).SetCtrl(Cpybtn, Cells(row, i)) With Cpybtn .Top = Top + 24 * i .Left = 10 .Height = 20 .Width = 50 .Caption = "Copy" .Tag = i End With Loop End With End Sub

こちらがクラスです

Private WithEvents tgtCtrl As MSForms.CommandButton Private tgtRange Public Sub SetCtrl(new_ctrl As MSForms.CommandButton, new_Range As Range) Set tgtCtrl = new_ctrl Set tgtRange = new_Range End Sub Private Sub tgtCtrl_Click() Dim intRow As Long Dim URL As String Dim intcol As Long Dim i As Long Dim ie As InternetExplorer Dim Ch As Variant If tgtCtrl.Name = "IEL" Then 'Urlボタンの表示 intRow = tgtRange.row URL = Cells(intRow, 5).Copy MsgBox "IEでインターネットへ接続します。" Set ie = CreateObject("InternetExplorer.Application") ie.Navigate Cells(intRow, 5) ie.Visible = True Exit Sub End If If tgtCtrl.Name = "CHL" Then 'Urlボタンの表示 intRow = tgtRange.row URL = Cells(intRow, 5).Copy MsgBox "Chromeでインターネットへ接続します。" Set Ch = CreateObject("WScript.Shell") 'ie.Navigate """ & Cells(intRow, 5) & """ Ch.Run ("chrome.exe -url " & Cells(intRow, 5)) Set Ch = Nothing Exit Sub End If MsgBox "コピーしました" i = tgtCtrl.Tag intRow = tgtRange.row intcol = 5 + 2 * i Cells(intRow, intcol).Copy End Sub

よろしくお願いします。

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答2

0

meg_さんの回答にもありますが、上書きされてます。下記のように配列でクラスを2つにして、それぞれでSetCtrlすれば解決するでしょう。

vba

1Private ctrlbtn(1 To 100) As New EventButtonClass 2Private ctrlURL(1 to 2) As New EventButtonClass '修正 3 4Sub UserForm_Initialize() 5 6'中略 7 8 Call ctrlURL(1).SetCtrl(IELbtn, Cells(row, 5)) 9 Call ctrlURL(2).SetCtrl(CHLbtn, Cells(row, 5)) 10 11'後略

ただし、meg_さんの回答と重複しますが、この部分はクラス化する意味はないですね。
一つのクラスに異なる機能を詰め込んでますので、読みづらくなってます。

  • セル内容をコピーする
  • IEを開く
  • Chromeを開く

はまったく別の機能ですので一つに纏める必然性はないです。
「セル内容をコピーする」は同じ機能のボタンが多数あるのでクラス化するのは意味がありますか、他の2つはクラスにする必然性はないです。

クラスを使わない場合のコード例

vba

1Option Explicit 2Dim WithEvents IELbtn As CommandButton 3Dim WithEvents CHLbtn As CommandButton 4 5Private Sub IELbtn_Click() 6 Dim ie As InternetExplorer 7 8 MsgBox "IEでインターネットへ接続します。" 9 10 Set ie = CreateObject("InternetExplorer.Application") 11 ie.Navigate Cells(ActiveCell.Row, 5) 12 13 ie.Visible = True 14End Sub 15 16Private Sub CHLbtn_Click() 17 Dim Ch As Variant 18 19 MsgBox "Chromeでインターネットへ接続します。" 20 21 Set Ch = CreateObject("WScript.Shell") 22 Ch.Run "chrome.exe -url " & Cells(ActiveCell.Row, 5) 23 Set Ch = Nothing 24End Sub 25 26Private Sub UserForm_Initialize() 27 Set IELbtn = Me.Controls.Add("Forms.CommandButton.1", "IEL", True) 28 Set CHLbtn = Me.Controls.Add("Forms.CommandButton.1", "CHL", True) 29 30 With IELbtn 31 .Top = 64 32 .Left = 10 33 .Height = 20 34 .Width = 100 35 .Caption = "IEでアクセス" 36 End With 37 38 With CHLbtn 39 .Top = 64 40 .Left = 120 41 .Height = 20 42 .Width = 100 43 .Caption = "Chromeでアクセス" 44 End With 45End Sub

投稿2020/09/10 02:15

編集2020/09/10 07:28
hatena19

総合スコア33699

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

0

ベストアンサー

VBA

1Call ctrlURL.SetCtrl(IELbtn, Cells(row, 5)) 2Call ctrlURL.SetCtrl(CHLbtn, Cells(row, 5))

上記によりctrlURLtgtCtrlCHLbtnに上書きされていませんか?
そのためIf tgtCtrl.Name = "CHL" Thenの方が常に実行されていませんか?

ボタンが2つしかないのであれば、イベントプロシージャーの共通化をするのではなくボタン毎にクリックイベントを記述する方法の方が分かりやすいのではないでしょうか?

投稿2020/09/09 15:04

meg_

総合スコア10579

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問