VBScriptでローカルエリア接続の有効・無効を自動化する方法

このQ&Aのポイント
  • コントロールパネルからネットワーク接続を開けず、マイネットワークの右クリックからプロパティを開く方法でローカルエリア接続を有効・無効にするVBScriptを作成したが問題が発生している。
  • マイネットワークのプロパティが前面に出るだけで、ネットワーク接続の画面にあるfolderitemが取得できなくなっている。
  • アクティブなウィンドウのfolderitemを取得する方法や、ncpl.cplでfolderitemを取得する方法を試したが成功していない。何か他の方法はあるだろうか?
回答を見る
  • ベストアンサー

VBScript等でローカルエリア接続の有効・無効をしたい。

コントロールパネルからネットワーク接続を開かせ、右クリックメニューにある「有効にする」or「無効にする」を選択するという自動化したものがあったのですが、 仕事で利用しているPCなのでコントロールパネルが開かず、マイネットワークの右クリックからプロパティか、ncpa.cplで開くしか手段がなく、利用できませんでした。 devconなどの他ツールを使わずにローカルエリア接続を有効にしたいのですが、何か方法はありませんでしょうか? ちなみに、弄った後のものになりますが、下記方法だとマイネットワークのプロパティが前面に出るだけで、ネットワーク接続の画面にあるfolderitemが取得できなくなっています。 アクティブなウィンドウのfolderitemを取得するとか、右クリックのプロパティをDo itじゃなく、画面開いたときと同じような処理(スクリプト上で自動認識?)できるのであれば問題もなさそうなのですが・・・ それかncpl.cplでfolderitemを取得して・・・という方法もあるのかもしれません(調べましたが見つかりませんでしたorz) Const ssfDESKTOP = 0 Const sConPaneName = "マイ ネットワーク" Const sConnectionName = "ローカル エリア接続" Const sConAAA = "プロパティ(&R)" Const sDisableVerb = "無効にする(&B)" Const sEnableVerb = "有効にする(&A)" set shellApp = createobject("shell.application") set oControlPanel = shellApp.Namespace(ssfDESKTOP) set oNetConnections = nothing set oNetC2onnections = nothing for each folderitem in oControlPanel.items if lcase(folderitem.name) = lcase(sConPaneName) then set oNetConnections = folderitem': exit for end if 'if folderitem.name = sConPaneName then ' set oNetC2onnections = folderitem.getfolder: exit for ' end if next for each verb in oNetConnections.verbs if verb.name = sConAAA then verb.Doit WScript.Sleep 2000 end if next if oNetConnections is nothing then wscript.quit end if Set WshShell = CreateObject("Wscript.Shell") WshShell.AppActivate("ネットワーク接続") set oNetC2onnections = folderitem.getfolder set oLanConnection = nothing for each folderitem in oNetC2onnections.items msgbox folderitem.name if lcase(folderitem.name) = lcase(sConnectionName) then set oLanConnection = folderitem: exit for WScript.Echo "aaaaaaa" end if next if oLanConnection is nothing then wscript.quit end if for each verb in oLanConnection.verbs if verb.name = sDisableVerb then verb.Doit WScript.Sleep 2000 end if next お分かりになられる方いらっしゃいましたら、ご教授願います。

質問者が選んだベストアンサー

  • ベストアンサー
  • nda23
  • ベストアンサー率54% (777/1415)
回答No.3

No.2です。もう少し洗練された方法に変えました。呼び出し方は以下の通りです。 EnableLAN("ローカル エリア接続",True) EnableLAN("ローカル エリア接続",False) '*************************************************************** '* ネットワーク接続の接続/無効を制御する '* 引 数:NetworkName  処理対象のネットワーク接続名 '*     :Enable      True = 接続 / False = 無効 '* 戻り値:結果       0 = 正常終了 '*                1 = ネットワーク接続が見つからない '*                2 = 処理対象のネットワーク接続名が見つからない '*                3 = タイムアウト '*************************************************************** Function EnableLAN(ByVal NetworkName,ByVal Enable)   Const EnableVerb   = "有効にする(&A)" '接続する場合のコマンド   Const DisableVerb  = "無効にする(&B)" '切断する場合のコマンド   Const StatusPosition = 1            '状態を示す文字列の位置   Const StatusLength  = 2            '状態を示す文字列の長さ   Const EnableWord   = "接続"        '接続時の状態   Const DisableWord  = "無効"        '無効時の状態   Const MaxWaitCount  = 2000         '状態遷移の最大検査回数   Dim oShell      'シェルオブジェクト   Dim oNetwork    'ネットワーク接続   Dim oConnect    '処理対象の接続   Dim iIndex      'ネットワーク接続内のインデックス   Dim iWork      '戻り値/カウンタ   Dim sExpect     '期待する状態   Dim sStatus     '取得した状態(全文)   Set oShell = CreateObject("Shell.Application")   Set oNetwork = oShell.NameSpace("Shell:ConnectionsFolder")   If oNetwork Is Nothing Then     EnableLAN = 1     Exit Function   End If   iWork = oNetwork.Items.Count - 1   For iIndex = 0 To iWork     Set oConnect = oNetwork.Items.Item(iIndex)     If oConnect.Name = NetworkName Then Exit For   Next   If iIndex > iWork Then     EnableLAN = 2     Exit Function   End If   If Enable Then     oConnect.InvokeVerb EnableVerb     sExpect = EnableWord   Else     oConnect.InvokeVerb DisableVerb     sExpect = DisableWord   End If   For iWork = 1 To MaxWaitCount     Set oConnect = oNetwork.Items.Item(iIndex)     sStatus = oNetwork.GetDetailsOf(oConnect,2)     If Mid(sStatus,StatusPosition,StatusLength) = sExpect Then       EnableLAN = 0       Exit Function     End If     WScript.Sleep 1   Next   EnableLAN = 3 End Function ※たぶん、Windows2000でも変更なしで実行できると思います。

sc_staff
質問者

お礼

お礼遅れました。 実際に動作確認等終わり、運用でも利用の了解得ることができそうです。 ただ使えるだけでなく、参考になりました。 ありがとうございました。

その他の回答 (2)

  • nda23
  • ベストアンサー率54% (777/1415)
回答No.2

大分、遅くなりましたが以下のScriptで実行できました。 環境は WindowsXP Professional SP3 です。 '************************************************** '* "ローカル エリア接続"の接続/無効を切り替える '* 引数  Enable True=接続 / False=無効 '* 戻り値 0=正常 / 1=ネットワークなし / 2=ローカル エリア接続なし / 3=タイムアウト '************************************************** Function EnableLAN(ByVal Enable)   Const ssfCONTROLS = 3   Const sColumnStatus = 2   Const sMaxCount = 2000   Const sConPaneName = "ネットワーク接続"   Const sConnectionName = "ローカル エリア接続"   Const sDisableVerb = "無効にする(&B)"   Const sEnableVerb = "有効にする(&A)"   Const sConnect = "接続"   Const sDisconnect = "無効"   Set ShellApp = CreateObject("Shell.Application")   Set oControlPanel = ShellApp.NameSpace(ssfCONTROLS)   Set oConPane = Nothing   Set oNetConnections = Nothing   For Each FolderItem In oControlPanel.Items     If FolderItem.Name = sConPaneName Then       Set oConPane = FolderItem       For Each Element In FolderItem.GetFolder.Items         If Element.Name = sConnectionName Then           Set oNetConnections = Element           Exit For         End If       Next       Exit For     End If   Next   If oConPane Is Nothing Then     EnableLAN = 1     Exit Function   End If   If oNetConnections Is Nothing Then     EnableLAN = 2     Exit Function   End If   Expected = ""   If Enable Then     oNetConnections.InvokeVerb sEnableVerb     Expected = sConnect   Else     oNetConnections.InvokeVerb sDisableVerb     Expected = sDisconnect   End If   TimeCount = 0   Do     Status = ""     For Each FolderItem In oControlPanel.Items       If FolderItem.Name = sConPaneName Then         For Each Element In FolderItem.GetFolder.Items           If Element.Name = sConnectionName Then             Status = FolderItem.GetFolder.GetDetailsOf _                 (Element, sColumnStatus)             Exit For           End If         Next         Exit For       End If     Next     If Left(Status, 2) = Expected Then       EnableLAN = 0       Exit Do     End If     If TimeCount >= sMaxCount Then       EnableLAN = 3       Exit Do     End If     WScript.Sleep 1     TimeCount = TimeCount + 1   Loop End Function

  • nda23
  • ベストアンサー率54% (777/1415)
回答No.1

何でdevconはダメなんですか?

sc_staff
質問者

補足

実行形式を配布することができない環境であることと、devconだとデバイスIDで制御するかと思いますが、数千台のPCで、メーカーやOSも統一されているわけではないので、対応しきれないのが現状です。 ローカルエリア接続という点と、Win2kかXPの人だけ対応するので・・・ devconを使わなくてもマイネットワークの右クリックからプロパティか、ncpl.cplで開くことはできますので、コントロールパネルから判別するこの(↑)のやり方を流用できないかと考えてはいるのですが、判別させることは不可能なのでしょうか?

関連するQ&A

  • VBScriptでファイル検索

    こんにちは。 Dドライブのどこかにある「あいうえお.xls」ファイルのパス をメッセージ表示するようなスクリプトを作りましたが、上手 く表示されません。どこに不具合があるのか、ご教授ください。 お願いいたします。 Option Explicit Dim FSO,File,SubFolder Set FSO = CreateObject("Scripting.FileSystemObject") ShowSubfolders FSO.GetFolder("D:\") Sub ShowSubFolders(Folder) For Each File in Folder.Files If File.Name = "あいうえお.xls" then      Wscript.Echo File.path End If Next For Each Subfolder in Folder.SubFolders ShowSubFolders Subfolder Next Set FSO = Nothing End Sub

  • [VBScript]csrss.exeメモリリーク

    vbscriptでファイル名にある文字列を含むファイルを検索し、作成日の最も古いファイルをコピーする処理をしています。 下記環境で動かしているのですが、csrss.exeの使用メモリが増加し続けます。 原因または調査の仕方など教えていただけると幸いです。 システムの都合上、別言語への変更はできないのでどうにか解決したいです。 よろしくお願いいたします。 動作環境: windows2003 std sp1 プログラム: Const ForReading = 1 'read only Const ForWriting = 2 'write(overwrite mode) Const ForAppending = 8 'write(add mode) kekka =0 Set objFS = CreateObject("Scripting.FileSystemObject") Set objShell = WScript.CreateObject("WScript.Shell") Set colEnv = objShell.Environment("Process") strNow = Now FILE_NAME = "test_J999" LIST_FILE = "D:\common\" & FILE_NAME & ".log"'use log Set objText = objFS.OpenTextFile(LIST_FILE, ForAppending, True, 0) objText.WriteLine strNow & " copy.vbs start" strSndpath = "D:\common\data\" strRcvpath = "D:\common\" strFileName = search() If objFS.FileExists(strCopyFrom) = False Then Kekka = 0 WScript.Echo kekka objText.WriteLine strNow & " copy.vbs end" & " error: " & err.Number & " file: nothing" objText.Close Set objShell = Nothing Set objFS = Nothing WScript.Quit End If objFS.CopyFile strCopyFrom,strCopyTo IF err.number = 0 Then kekka = 0 Else kekka = err.number End IF WScript.Echo kekka objText.WriteLine strNow & " copy.vbs end" & " error: " & err.Number & " file: " & strFileName objText.Close Set objShell = Nothing Set objFS = Nothing Function search() On Error Resume Next 'error ignore Set objApl = Wscript.CreateObject("Shell.Application") Set folder = objFS.GetFolder(strSndpath) Set folinfo = objApl.NameSpace(strSndpath) Mostoldtime = strnow Totalcnt = folinfo.Items().count+5 Redim File(Totalcnt) i=0 For each t in File If Instr(t , FILE_NAME) > 0 then strfilepath = strSndpath & "\" & t Set fileinfo = objFS.GetFile(strfilepath) 'serch most old file If fileinfo.DateCreated < Mostoldtime then Mostoldtime = fileinfo.DateCreated Filename = t End If End If Next Erase File Set objApl = Nothing search = (Filename) End function

  • VBSエラー"オブジェクト型の変数は定義されていません"について(2)

    こんばんは。よろしくお願いします。 CreatePages1は、あるルートフォルダ(rtFolder)にトップページ"index.html"をつくり(または上書きし)、そのルートフォルダの中にひとつだけあるフォルダ(sbFolder)以下のすべての階層のすべてのフォルダの中に存在する、拡張子がabc(仮称)のファイルと同じ名前のhtmlファイルを、abcファイルと同じフォルダに同じ数だけつくり(または上書きし)、トップページ"index.html"にその作成したすべてのhtmlファイルへのリンクを表示させる、という構想で作成中のVBSなのですが、これを実行すると「●(マル)」と書いた20行目のところでエラー「オブジェクト型の変数は定義されていません」となってしまいます。その前のEchoの結果は望みどおりになっていると思います。このエラーをどのように対処すればよいかを教えていただければと思います。 注)rtFolderにabcファイルはありません。 私はExcelのVBAは多少の経験がありますが、VBScriptを書いたのはこれが初めてで、HTMLも未経験です。 aSearchPatternに"*.abc"を代入したのに、▲(さんかく)と書いた行で拡張子"abc"を指定してしまっているのは、こうしないと全てのファイルについてhtmlファイルが作成されそうだったからです。よろしければ、このことも含めてご回答よろしくお願いします。 Option Explicit Public fso, CurFolder, indexPageTS, otherPageTS, rtFolder, sbFolder, sbFolder2, _ dataFolder, aSearchPattern, aFiles, FileItem, currentFolder, Ext Set fso = WScript.CreateObject("Scripting.FileSystemObject") Set CurFolder = fso.GetFolder(".") Call CreatePages1(CurFolder, "*.abc", aFiles) Set CurFolder = Nothing Set fso = Nothing 'サブフォルダへの処理 Public Sub SearchSubFolder1(ByVal sbFolder) Set fso = WScript.CreateObject("Scripting.FileSystemObject") For Each sbFolder2 In sbFolder.SubFolders WScript.Echo sbFolder WScript.Echo sbFolder2 ●(マル) If sbFolder2 <> "" Then Call CreatePages1(sbfolder2, aSearchPattern, aFiles) '再帰呼び出し If sbFolder2 <> "" Then Call SearchSubFolder1(sbFolder2) End If End If Next 'オブジェクトの開放。 Set sbFolder2 = Nothing Set fso = Nothing End Sub 'htmlファイル作成 Public Sub CreatePages1(ByVal rtFolder, ByVal aSearchPattern, ByRef aFiles) Set fso = WScript.CreateObject("Scripting.FileSystemObject") 'トップページを途中まで作成する。 Set indexPageTS = fso.CreateTextFile(rtFolder.Path & "\index.html", True) indexPageTS.WriteLine "<HTML>" (中略) 'データフォルダに各htmlファイルを作成する。 For Each dataFolder In rtFolder.SubFolders If dataFolder <> "" Then For Each FileItem In dataFolder.Files If FileItem <> "" Then Ext = fso.GetExtensionName(FileItem.Name) ▲(さんかく) If LCase(Ext) = "abc" Then Set otherPageTS = fso.CreateTextFile(dataFolder.Path & "\" & Left(FileItem.Name, Len(FileItem.Name) - Len(Ext)-1) & ".html", True) otherPageTS.WriteLine "<HTML>" (中略) otherPageTS.WriteLine "</HTML>" otherPageTS.Close() 'トップページに各リンクを作成する。 Ext = fso.GetExtensionName(FileItem.Name) If LCase(Ext) = "html" Then indexPageTS.WriteLine "<a href=""" & rtFolder.Path & "\" & FileItem.Name & """>" & Left(FileItem.Name, Len(FileItem.Name) - Len(Ext)-1) & "</a><br>" End If End If End If Next End If Next 'トップページの続きを記述する。 indexPageTS.WriteLine "</BODY>" indexPageTS.WriteLine "</HTML>" indexPageTS.Close() 'データフォルダのサブフォルダへの処理。 For Each dataFolder In rtFolder.SubFolders If dataFolder <> "" Then Set currentFolder = fso.GetFolder(dataFolder.Path) If currentFolder <> "" Then Call SearchSubFolder1(currentFolder) End If End If Next 'オブジェクトの開放。 Set currentFolder = Nothing Set dataFolder = Nothing Set indexPageTS = Nothing Set otherPageTS = Nothing Set fso = Nothing End Sub

  • VBScriptでファイルからgoogle検索

    カレントディレクトリの.txtファイルを1行づつ読み込み、google検索したいのですが、ウィンドウが複数立ち上がってしまい1ウィンドウでタブで表示したいのですが上手くいきません。。と、テキストファイル名もタイトルタグで表示したいです。どなたかご存知でしたらご教授願います。以下、書きかけのソースになります。 ---------------------------------------------------------------------------------- use_ie Sub use_ie() 'Option Explicit 'On Error Resume Next Dim objFSO ' FileSystemObject Dim objFile ' ファイル読み込み用 Dim objWshShell ' WshShell オブジェクト If Err.Number = 0 Then Set objWshShell = WScript.CreateObject("WScript.Shell") Set objFSO = WScript.CreateObject("Scripting.FileSystemObject") If Err.Number = 0 Then For Each FLO In objFSO.GetFolder(objWshShell.CurrentDirectory).Files path = FLO.Path If Right(path, 4) = ".txt" Then Set objFile = objFSO.OpenTextFile(path, 1) Do While objFile.AtEndOfStream <> True Set ie = CreateObject("InternetExplorer.Application") ie.Visible = True 'ie.Navigate "about:blank" 'ie.Document.Write "<title>path</title>" ie.Navigate "http://www.google.co.jp/" ',2048 '新規タブで開く waitIE ie ie.Document.getElementById("q").Value = objFile.ReadLine WScript.Sleep 100 ' 検索ボタンクリック ie.Document.all("btnG").Click waitIE ie strURL = "https://www.google.co.jp/" If ie.LocationURL = strURL Then ie.Quit Set ie = Nothing End If Loop objFile.Close End If Next Else WScript.Echo "ファイルオープンエラー: " & Err.Description End If Else WScript.Echo "エラー: " & Err.Description End If Set objFile = Nothing Set objFSO = Nothing Set objWshShell = Nothing Set ie = Nothing End Sub ' IEがビジー状態の間待ちます Sub waitIE(ie) Do While ie.Busy = True Or ie.readystate <> 4 WScript.Sleep 100 Loop WScript.Sleep 1000 End Sub

  • 【VBScript】プログラム改良

    VBScriptのプログラムについて、 回答頂きたく投稿しました。 以下を実行すると、 65行目で「'End'がありません。」とエラー表示されてしまいます。 End Ifは入れているはずですが、どこが問題なのでしょうか? またIfステートメントを少し減らしたいのですが、 どうすればシンプルな形になりますでしょうか? 恐れ入りますが、アドバイス頂ければ幸いです。 Option Explicit Dim intCount, strFile, strArg, strX, lonMsgBox, objFSO, objOpen, strText, strNewFile, objTS intCount = 0 If WScript.Arguments.Count = 0 Then WScript.Echo "引数が指定されていません。" WScript.Quit End If For Each strArg In WScript.Arguments intCount = intCount + 1 strFile = strArg Next Set objFSO = CreateObject("Scripting.FileSystemObject") If objFSO.GetExtensionName(strFile) <> "txt" Then If intCount > 1 Then MsgBox "2つ以上のファイルが指定されています。" & vbCr _ & "ファイルを指定し直してください。", 48, "Error" WScript.Quit Else MsgBox "テキストファイル以外が指定されています。" & vbCr _ & "ファイルを指定し直してください。", 48, "Error" WScript.Quit End If Else strX = InputBox("抽出したい文字列を入力してください。", "変換処理") If strX <> "" Then lonmsgbox = MsgBox (strX & "を抽出します。" & vbCr _ & "変換しますか?", 4 + 32 + 0, "確認") If lonmsgbox = 6 Then strNewFile = objFSO.BuildPath( _ objFSO.GetParentFolderName(strFile), _ objFSO.GetBaseName(strFile) & "_New." & objFSO.GetExtensionName(strFile)) Set objTS = objFSO.OpenTextFile(strNewFile, 2, True) Set objOpen = objFSO.OpenTextFile(strFile, 1) Do Until objOpen.AtEndOfStream = True strText = objOpen.ReadLine If InStr(strText, strX) > 0 Then objTS.WriteLine strText End If Loop objTS.Close Set objTS = Nothing objOpen.Close Set objFSO = Nothing WScript.Sleep 1000 MsgBox ("文字列の抽出が完了しました。") Else MsgBox ("処理を中断します。") End If End If ElseIf IsEmpty(strX) then MsgBox ("キャンセルされました。") WScript.Quit Else MsgBox "文字列が入力されていません。" & vbCr _ & "入力し直してください。", 0, "Error" WScript.Quit End If

  • VBScriptで検索結果をタブ表示したいです。

    カレントディレクトリの.txtファイルを1行づつgoogle検索したいのですが、ウィンドウが複数立ち上がってしまい1ウィンドウでタブで表示したいのですが上手くいきません。。と、テキストファイル名もタイトルタグで表示したいです。どなたかご存知でしたらご教授願います。以下、書きかけのソースになります。 ---------------------------------------------------------------------------------- use_ie Sub use_ie() 'Option Explicit 'On Error Resume Next Dim objFSO ' FileSystemObject Dim objFile ' ファイル読み込み用 Dim objWshShell ' WshShell オブジェクト If Err.Number = 0 Then Set objWshShell = WScript.CreateObject("WScript.Shell") Set objFSO = WScript.CreateObject("Scripting.FileSystemObject") If Err.Number = 0 Then For Each FLO In objFSO.GetFolder(objWshShell.CurrentDirectory).Files path = FLO.Path If Right(path, 4) = ".txt" Then Set objFile = objFSO.OpenTextFile(path, 1) Do While objFile.AtEndOfStream <> True Set ie = CreateObject("InternetExplorer.Application") ie.Visible = True 'ie.Navigate "about:blank" 'ie.Document.Write "<title>path</title>" ie.Navigate "http://www.google.co.jp/" ',2048 '新規タブで開く waitIE ie ie.Document.getElementById("q").Value = objFile.ReadLine WScript.Sleep 100 ' 検索ボタンクリック ie.Document.all("btnG").Click waitIE ie strURL = "https://www.google.co.jp/" If ie.LocationURL = strURL Then ie.Quit Set ie = Nothing End If Loop objFile.Close End If Next Else WScript.Echo "ファイルオープンエラー: " & Err.Description End If Else WScript.Echo "エラー: " & Err.Description End If Set objFile = Nothing Set objFSO = Nothing Set objWshShell = Nothing Set ie = Nothing End Sub ' IEがビジー状態の間待ちます Sub waitIE(ie) Do While ie.Busy = True Or ie.readystate <> 4 WScript.Sleep 100 Loop WScript.Sleep 1000 End Sub

  • VBScript ワードunicodeテキスト保存

    大量のワードのファイルをテキスト保存する仕事が入り、フォルダ内にあるワードファイルをテキスト保存し、テキストボックスやオートシェープの中のテキストも抜き出すプログラムを「VBScript」で作りました。 テキスト保存する際、Unicode形式で保存しなければならず、調べると、「SaveAs …, 7」がUnicodeによるテキスト保存だと分かったのですが、実際にやってみると、拡張子が「.rtf」ファイルだけが、Unicodeによるテキスト保存され、「.doc」や「.docx」は、「シフトJIS」保存されてしまいました。 私、何か間違っているのでしょうか? 以下、一応、プログラムをコピーしておきます。 問題の部分は「行13」です。 なお、ワードは「Word2010」です。 01 Option Explicit 02 Public a, b, c, d, e, f, g, h, t, u, v, w, x, y, z 03 Set w = CreateObject("Word.Application") 04 w.Application.DisplayAlerts = False 05 w.Visible = False 06 Set x = CreateObject("Scripting.FileSystemObject") 07 Set y = x.GetFolder(".") 08 For Each a In y.Files 09 b = LCase(x.GetExtensionName(a.Name)) 10 If b = "doc" or b = "docx" or b = "rtf" Then 11 h = x.GetBaseName(a.Name) 12 Set z = w.Documents.Open(y & "\" & a.Name) 13 z.SaveAs y & "\" & h & ".txt", 7 14 z.Close 15 Set z = Nothing 16 End If 17 If b = "docx" Then 18 Set z = w.Documents.Open(y & "\" & a.Name) 19 z.SaveAs y & "\qkza934irs2801wuptc56ynv7bm.doc", 0 20 z.Close 21 Set z = Nothing 22 Set z = w.Documents.Open(y & "\qkza934irs2801wuptc56ynv7bm.doc") 23 Call o 24 z.Close 25 Set z = Nothing 26 x.DeleteFile(y & "\qkza934irs2801wuptc56ynv7bm.doc") 27 ElseIf b = "doc" or b = "rtf" Then 28 Set z = w.Documents.Open(y & "\" & a.Name) 29 Call o 30 z.Close 31 Set z = Nothing 32 End If 33 If f > "" Then 34 Set t = x.CreateTextFile(y & "\TB_" & h & ".txt", True, True) 35 t.Write f 36 t.Close 37 End If 38 Next 39 w.Quit 40 Set z = Nothing 41 Set y = Nothing 42 Set x = Nothing 43 Set w = Nothing 44 MsgBox("Finished!") 45 Sub o 46 f = "" 47 For Each c In w.ActiveDocument.Shapes 48 If c.Type = 1 or c.Type = 17 then 49 Set v = c.TextFrame 50 e = v.TextRange 51 f = f & e 52 Set d = Nothing 53 ElseIf c.Type = 6 Then 54 For Each g In c.GroupItems 55 Set u = g.TextFrame 56 e = u.TextRange 57 f = f & e 58 Set u = Nothing 59 Next 60 End if 61 Next 62 End Sub よろしく、お願いします。

  • VBScript について 初心者です

    会社内でmsgファイルから添付ファイルを抽出するVBScriptを以下のコードで利用させてもらっています。ただ、同じファイル名の場合は取出すことができません。アンケート.xlsx、アンケート(2).xlsx、などという形でもれなく取出せるようにできないでしょうか?よろしくお願いいたします。 Option Explicit Dim args Dim olApp Dim i Const SaveFolderPath = "C:\Test" '添付ファイルの保存先フォルダ(※要変更) Set args = WScript.Arguments If args.Count < 1 Then MsgBox "msgファイルを当スクリプトファイルにドラッグ&ドロップしてください。", vbExclamation + vbSystemModal WScript.Quit End If With CreateObject("Scripting.FileSystemObject") If .FolderExists(SaveFolderPath) = False Then MsgBox "添付ファイルの保存先フォルダが見つかりませんでした。" & vbCrLf & _ "処理を中止します。", vbCritical + vbSystemModal WScript.Quit End If Set olApp = CreateObject("Outlook.Application") For i = 0 To args.Count - 1 If .FileExists(args(i)) = True Then Select Case LCase(.GetExtensionName(args(i))) Case "msg" 'msgファイルのみ処理 SaveMsgAttachments olApp, args(i), AddPathSeparator(SaveFolderPath) End Select End If Next olApp.Quit End With MsgBox "処理が終了しました。", vbInformation + vbSystemModal Private Sub SaveMsgAttachments(ByVal OutlookApp, ByVal MsgFilePath, ByVal SaveFolderPath) Dim itm 'Outlook.MailItem Dim atc 'Outlook.Attachment Dim fn With OutlookApp.GetNamespace("MAPI") Set itm = .OpenSharedItem(MsgFilePath) Select Case LCase(TypeName(itm)) Case "mailitem" If itm.Attachments.Count < 1 Then MsgBox "添付ファイルがありません。" & vbCrLf & _ "(ファイル名:" & MsgFilePath & ")", vbExclamation + vbSystemModal Exit Sub Else With CreateObject("Scripting.FileSystemObject") For Each atc In itm.Attachments fn = SaveFolderPath & atc.FileName If .FileExists(fn) = True Then .DeleteFile fn, True '同名のファイルがあったら事前に削除 End If atc.SaveAsFile fn Next End With End If End Select End With End Sub Private Function AddPathSeparator(ByVal s) If Right(s, 1) <> ChrW(92) Then s = s & ChrW(92) AddPathSeparator = s End Function

  • Windows7でVBScriptによるネットワークアダプタの有効/無

    Windows7でVBScriptによるネットワークアダプタの有効/無効を取得 こんにちは。 ネットで情報収集しているのですが、どうも答えがわからずにいます。 どなたかアドバイスをお願い致します。 Windows7でVBScriptを使ってネットワークアダプタの有効/無効を取得し、 テキストファイルに保存したいです。 WindowsXPでは正常に動くのですがWindows7だとエラーが返ってきます。 エラーの原因は、 「if strFolderItem.name = "ネットワーク接続" then」 の所で「strFolderItem.name = "ネットワーク接続"」に該当する ものが一つも無い事が原因のようです。 Xpでは"ネットワーク接続"だったのが7では名称が変わったのか。 それとも、そもそもこのobjApp.Namespace(3)では取得できないのか。 答えがわかりません。 よろしくお願いいたします。 以下、コードです。 '//----------------------------------------- ' ファイル名:test.vbs ' このVBScriptと同じフォルダ内に ' 「NetAdapterCheck.log」という空のファイルを準備した上で実行する。 '変数の宣言を明示的にする Option Explicit '変数の宣言 Dim objApp Dim objCtrPanel Dim objConnection Dim objAdapter Dim strEnable Dim strDisable Dim strFolderName Dim strFolderItem Dim strVerb Dim intCnt Dim objFileSys Dim objOutFile set objApp = createobject("shell.application") set objCtrPanel = objApp.Namespace(3) strEnable = "有効にする(&A)" strDisable = "無効にする(&B)" for each strFolderItem in objCtrPanel.items if strFolderItem.name = "ネットワーク接続" then set objConnection = strFolderItem.getfolder: exit for end if next strFolderName = "" intCnt = 0 for each strFolderItem in objConnection.items set objAdapter = strFolderItem for each strVerb in objAdapter.verbs if strVerb.name = strEnable then intCnt = intCnt + 1 strFolderName = strFolderName & intCnt & "<>" & "無効<>" & strFolderItem.name & vbCrLf elseif strVerb.name = strDisable then intCnt = intCnt + 1 strFolderName = strFolderName & intCnt & "<>" & "有効<>" & strFolderItem.name & vbCrLf end if next next 'ファイルに書き込み Set objFileSys = WScript.CreateObject("Scripting.FileSystemObject") Set objOutFile = objFileSys.OpenTextFile("NetAdapterCheck.log",2) '1=読込、2=上書き、3=追記 objOutFile.WriteLine strFolderName 'テキストファイルのクローズ objOutFile.Close 'オブジェクト破棄 Set objFileSys = Nothing Set objOutFile = Nothing 'オブジェクトの開放 set objApp = nothing set objCtrPanel = nothing set objConnection = nothing set objAdapter = nothing '//-----------------------------

  • VBScriptのIEでhttpにアクセスできない

    VBScriptにてIEを操作しています。 先日PCを新しくwin7のIE11にしたところおかしくなりました。 httpのサイトにアクセスするとDocumentCompleteが発生しません。 というか最初のnavigateでは発生はしているようなのですが 次のURLにnavigateした時発生せずreadystateは1のままです。 2度目のnavigateの挙動がおかしい気がします。 ちなみにhttpsのサイトは問題ありませんでした。 試しに以下のようなスクリプトを作成してみたところ Msgboxにて"http://www.yahoo.co.jp/"、"http://weather.yahoo.co.jp/weather/" の二つが表示されてしまいました、、、。 画面上にはお天気ページのIEのみが起動(見えて)していてIEはQuitできていません。 かなり困っています。 Option Explicit Dim ie Dim isReady Set ie = WScript.CreateObject("InternetExplorer.Application", "ie_") ie.Visible = True ie.Navigate "http://www.yahoo.co.jp/" Call wait ie.Navigate "http://weather.yahoo.co.jp/weather/" Call wait ie.Quit Set ie = Nothing msgbox "ok" Sub wait() Dim count: count = 0 isReady = False Do While isReady = False WScript.Sleep 100 count = count + 1 If count > 100 Then Call chkIe ie.Quit Set ie = Nothing WScript.Quit End If Loop End Sub Sub ie_DocumentComplete(ByVal pDisp, URL) If pDisp Is ie Then isReady = True End Sub Sub chkIe Dim fso Dim browse Dim pn Set fso = CreateObject("Scripting.FileSystemObject") For Each browse In CreateObject("Shell.Application").Windows pn = fso.GetFileName(browse.FullName) If LCase(pn) = "iexplore.exe" Then msgbox browse.LocationURL Next Set fso = Nothing End Sub ほかのWIN7、IE11のPCでは問題なく操作できています。 この新しいPCのIEもデフォルトのままで特別な設定はしていません。 どなたか何か心当たりがありましたらよろしくお願い致します。