PuppySmurf
PuppySmurf

Reputation: 3

VBS / Making Word Document Visible

I have a simple script that I would like to run and it check to see if there's a word document open, and if it's open, then make it visible. It can be ANY word document, so I can't make it specifically to any file name.

Here's the code so far:

Dim Word

Set Word = GetObject(, "Word.Application")

If Word Is Nothing Then
    MsgBox "Is not running"
Else
    MsgBox "Is running"
    Word.Visible = True
End If

Everything works except for Word.Visible = True. The popup shows "Is running" but the word document isn't brought to the front, selected or made visible at all. What am I missing? Thanks!

Updated details:

I've even tried it like this...

Dim Word
Set Word = GetObject(, "Word.Application")
Word.Visible = True
Word.Selection.TypeText "Hello Word"

With this code...As long as Word is currently opened, it should make it visible, then write Hello Word. It does write Hello Word, but doesn't make it visible. One I run it, I can see Word flashing in my task bar because Hello Word was added, but still doesn't make it visible. Hope that helps!

Upvotes: 0

Views: 2919

Answers (2)

Ansgar Wiechers
Ansgar Wiechers

Reputation: 200273

It seems you're misunderstanding how the Visible property works. That property defines if the application is visible at all or not (as in "does or doesn't show up in the taskbar").

Apparently your application already is visible (otherwise you wouldn't be able to see it flashing in the taskbar), but what you actually want is to un-minimize it and bring it to the front. The WindowState property should do that for you:

Const wdWindowStateNormal   = 0
Const wdWindowStateMaximize = 1
Const wdWindowStateMinimize = 2

Set wd = GetObject(, "Word.Application")

If wd Is Nothing Then
  MsgBox "Is not running"
Else
  MsgBox "Is running"
  wd.WindowState = wdWindowStateNormal
End If

Upvotes: 1

Zam
Zam

Reputation: 2940

Set oWord = CreateObject ("Word.Application")

oWord.Visible = True

Upvotes: 1

Related Questions