Hiding Word: Killing the blob!

H

Howard Kaikow

Code such as the code below executes ugly.

The Documents.Add causes the blob to appear (an empty hidden Word window)
One has to then take action to remove the critter.
Quit causes the blob to briefly appear, then go away.

Makes for an ugly interface.

Is the only way around the issue to lock the desktop with an API?
Or could one just lock the Word window after making it invisible?

Dim appWord As Word.Application
Dim docWord As Word.Document

Set appWord = New Word.Application
With appWord
.ScreenUpdating = False
.Visible = False
.WindowState = wdWindowStateMinimize

' Blob appears
Set docWord = .Documents.Add(Visible:=False)
With docWord
' Makes blob disappear
.ActiveWindow.Visible = False
' Do stuff here
.Close
End With
' Blob appears, then goes away
.Quit
End With
Set docWord = Nothing
Set appWord = Nothing
 
H

Howard Kaikow

Alex Ivanov said:
Make the appWord.Visible=false right after its creation.

I'm already doing that.

I posted the following in the winapi newsgroup.
------------------------------------------
When I lock the Word app window with the code below, the lock does not hold
unless I put a breakpoint on, say, the With appWord statement following the
commented out Sleep statement.

Since it worked using a breakpoint, I ASSuMEd that I could use Sleep to
achieve the same effect. Well, I tried Sleep 20000 and that did no good.

What do I have to do to make sure the Lock works?
The code will evetually be part of a .exe or .dll, so cannot rely on manual
breakpoint.


Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal
lpClassName As String, _
ByVal lpWindowName As String) As Long
Private Declare Function LockWindowUpdate Lib "user32" (ByVal hwndLock As
Long) As Long

Private Declare Function GetWindowText Lib "user32" _
Alias "GetWindowTextA" _
(ByVal hWnd As Long, _
ByVal lpString As String, _
ByVal cch As Long) As Long

Private Declare Function GetActiveWindow Lib "user32" () As Long

Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)

Private Sub btnByeBye_Click()
Unload Me
End Sub

Private Sub btnNoTemplate_Click()
Dim appWord As Word.Application
Dim docWord As Word.Document
Dim lngHWnd As Long
Dim lngReturn As Long
Dim strCaption As String

Set appWord = New Word.Application
lngHWnd = FindWindow(vbNullString, "Microsoft Word")
If lngHWnd = 0 Then
Debug.Print "Not found"
appWord.Quit
Set appWord = Nothing
Unload Me
Exit Sub
Else
Debug.Print "Handle is" & Str(lngHWnd)
Debug.Print "Caption is: " & ActiveWindowCaption(lngHWnd)
lngReturn = LockWindowUpdate(lngHWnd)
If lngReturn = 0 Then
Debug.Print "Lock failed"
appWord.Quit
Set appWord = Nothing
Unload Me
Exit Sub
Else
Debug.Print "Lock success:" & Str(lngReturn) & Str(lngHWnd)
End If
End If

' Sleep 1000
With appWord
.ScreenUpdating = False
.Visible = False
.WindowState = wdWindowStateMinimize

' Blob appears
Set docWord = .Documents.Add(Visible:=False)
With docWord
' Makes blob disappear
.ActiveWindow.Visible = False
' Do stuff here
.Close
End With
' Blob appears, then goes away
.Quit
End With
Set docWord = Nothing
Set appWord = Nothing
End Sub

Private Function ActiveWindowCaption(Optional hWnd = -1) As String
' Return caption for active window.
Dim strCaption As String
Dim lngLen As Long

If hWnd = -1 Then
hWnd = GetActiveWindow()
End If

strCaption = String$(255, vbNullChar)
lngLen = Len(strCaption)

If (GetWindowText(hWnd, strCaption, lngLen) > 0) Then
ActiveWindowCaption = strCaption
Else
ActiveWindowCaption = ""
End If
End Function
 
A

Alex Ivanov

I have no problem running your code - it runs completely invisible on my PC.
Have you tried to run the same code on another machine? For me it looks like
you might have some corrupted Word registry entries.
You may try to delete the value
HKEY_CURRENT_USER\Software\Microsoft\Office\10.0\Word\Data\Settings or
entire Word key (Replace 10.0 with your version of Word and export the key
into a backup file first!) It should be recreated from HKEY_USERS\.Default
hive next time you start the Word. See if you still have the problem after
that.
 
H

Howard Kaikow

I'll try the code with other word versions.
I'm currently using Word 2003.

I've got other code that hides word.
The .exe is at http://www.standards.com/index.html?Sorting.
That .exe was compiled using Word 97. But I just ran from the VB 6 source
using Word 2003 and Word was entirely invisible.

I'll have to check what the problem code does with other word versions.
 
H

Howard Kaikow

Alex Ivanov said:
I have no problem running your code - it runs completely invisible on my PC.
Have you tried to run the same code on another machine?

I forgot to ask.

Did you run the code from VB 6?
If so what SP version?

If not, from which app did you run the code?
 
A

Alex Ivanov

I ran it from vb6 ide and from Word macro and it run fine in both cases.
However, when I added some stuff to the body of the with construct, it
chocked on the line .Range.text = "Hello, world!". It looks like a bug in
Word to me. Fortunately, workaround is easy, don't put the "visible"
parameter to documents.add method. Word is already hidden, you don't need it
there. This is my version of the procedure:
Private Sub Form_Load()
Dim appWord As Word.Application
Dim docWord As Word.Document

Set appWord = New Word.Application
With appWord
.ScreenUpdating = False
.Visible = False
.WindowState = wdWindowStateMinimize

' Blob appears
Set docWord = .Documents.Add '(Visible:=False)
With docWord
.Range.Text = "Hello, world!"
.SaveAs "c:\test.doc"
' Makes blob disappear
'.ActiveWindow.Visible = False
' Do stuff here
.Close wdDoNotSaveChanges
End With
' Blob appears, then goes away
.Quit
End With
Set docWord = Nothing
Set appWord = Nothing

End Sub
 
A

Alex Ivanov

I forgot to mention that it's better to use
appWord.Quit wdDoNotSaveChanges
It will ensure that all open docs are closed before quitting with no prompt
to save changes. You won't need to call docWord.Close explicitly in this
case.
 
H

Howard Kaikow

I ran into the same problem running from within Word 2003.
I also ran into the same problem using VB 6 and Word 2002.

I then tried VB 6 with Word 97, which forced me to remove the .Visible for
the doc as that is not supported for word 97.

So, I need to try removing .Visible in Word 2003.
 
H

Howard Kaikow

Alex Ivanov said:
I ran it from vb6 ide and from Word macro and it run fine in both cases.
However, when I added some stuff to the body of the with construct, it
chocked on the line .Range.text = "Hello, world!". It looks like a bug in
Word to me. Fortunately, workaround is easy, don't put the "visible"
parameter to documents.add method. Word is already hidden, you don't need it
there.


I tried eliminating the visibility stuff.
Same problem, unless I insert a break in the code.

The problem could be caused by 3rd party apps such as Norton Auntie Virus
2004 or OmniPage Office Pro or ... interfering with the OLE.

I have other code in which hiding Word does work in Word 2003 does work.
I'll have to try to find out what, if anything, that code does differently.
 
H

Howard Kaikow

Alex Ivanov said:
I forgot to mention that it's better to use
appWord.Quit wdDoNotSaveChanges
It will ensure that all open docs are closed before quitting with no prompt
to save changes. You won't need to call docWord.Close explicitly in this
case.

I never need to use wdDoNotSaveChanges as I always have my code do what is
needed and using that parameter would hide errors from me, inadvertently
losing changes that should be saved.

In any case, this particual app does not need that because the file has
already been saved prior to the close.
 
H

Howard Kaikow

FYI

The same problem occurs automating Word from VB .NET using Word 2003.

So I guess the problem is either with Word or 3rd party apps interfering
with the OLE.

Gotta go to the dentist, I'll try to construct a fresh example after I get
back.
 
P

Perry

I never have problems using Automation if using the GetObject() function
No Word window, no Document window.
A smooth messagebox with no screendisturbances, no sign of Word at all ...

Dim wd As Object
Set wd = GetObject("c:\MyPath\MyDoc.doc")

MsgBox wd.Paragraphs(1).Range

'Do your/any stuff to the doc here,
'not the Window related methods/props

wd.Parent.Quit
Set wd = Nothing

Krgrds,
Perry
 
H

Howard Kaikow

Wow, I Just discovered the problem is being caused by my own Normal.dot.
If I remove the Normal.dot, no more blobs. So either my Normal.dot is the
culprit, or some 3rd party app is not playing nice with my Normal.dot.

First, I'll reconstruct Normal.dot t make sure it is fresh.
If that doesn't do the deed, then I'll have to find out what specifically is
causing the problem.

Sometime last year, I had to change AutoClose to defend against something
being done by OmniPage Office Pro, so that's a likely candidate!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top