1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

[VBS] Excelファイルを開いて最前面に表示する

Posted at

背景

特定のExcelファイルを手軽に開きたくなりました。
実際は開いた後にいろいろやるんですが、うまく開くまでにえらい苦労したので、そのエッセンスを記録しておきます。
これのショートカットファイルをデスクトップに置いて、ショートカットキーを設定するとワンタッチで動かせます。

環境は次の通り。

  • Windows 11 Home 22H2
  • Excel 365 Desktop 2302(ビルド16130.20218)

コード

' SJIS
' 変数の宣言を強制
Option Explicit

Dim oWsh : Set oWsh = WScript.CreateObject("WScript.Shell")
Dim oFSO:Set oFSO = CreateObject("Scripting.FileSystemObject")
Dim fpath:fpath = oFSO.GetParentFolderName(WScript.ScriptFullName) & "\ワークシート.xlsx"

' GetObjectでファイルパスを渡すと、(なければ新たに開いてから)オブジェクトが得られる
Dim oWb:set oWb = GetObject(fpath)
Call oWb.Activate ' なくても動いた
oWb.Windows(1).Visible = True
Call oWsh.AppActivate(oWb.Application.Caption)
' ここでマクロとかいろいろ実行する

Applicationは、変数で受けると別インスタンス(ウィンドウ)ができてしまいました。

参考

【VBScript】使える時に使いたいVBScript(WSH)のコード - Qiita
GetObject(, "Excel~")だけで無く、GetObject(ファイル名)も活用しよう - Qiita
Bingチャット

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?