Creating a Preview Window in a User Form (Word 2002)

M

Mickey F.

Ideally, what I'd like to do is create a UserForm that displays a list of
Word docs in a particular directory in a ComboBox. As the user selects
different documents in the ComboBox, another Control on the form displays the
text of the document, preferably in it's Rich Text Format - but even regular
text would be okay. Exactly like how the preview window woks in the AutoText
menu.

Here's the catch though, I'd like to be able to read from the documents
without actually opening them. Sort of like the .InsertFile method, but
instead of putting the text into a document, I would want to put that text
onto a Control. The text that needs to be previewed from each document WILL
be encapsulated in a uniquely named BookMark though.

I know how to put the File names into the ComboBox, the part I'm stuck on is
being able to read the BookMakrked text from another file WITHOUT opening it
(if that's even possible). I would also prefer not having to open a new
document or insert anything into the document the UserForm is attached too.

Word Version Info: Word 2002 (10.6818.6817) SP 3
 
J

Jonathan West

Hi Mickey,

Unfortunately, what you want to achieve can't be done. There isn't a preview
control for UserForms that gives you the same kind of preview as you see in
the File Open dialog.

The best you can do is actually *use* the File Open dialog.

Under normal circumstances, the FileDialog object would be the best approach
here, but unfortunately, when you set it to Preview mode, it doesn't display
the file previews you want.

The File Open dialog that is a member of the Dialogs collection does display
the preview, but the only way to persuade it to get into preview mode is to
use SendKeys, which is rather clunky and a method of last resort, but works
in the particular case

Try this code

With Dialogs(wdDialogFileOpen)
SendKeys "%l{LEFT}v"
If .Display = -1 Then
MsgBox .Name
Else
MsgBox "Cancel pressed"
End If
End With
 
M

Mickey F.

Thank you for the reply Jonathan! That's a great idea I never would have
come up with that on my own. The reason I didn't want to open each document
is because I was afraid of network lag time. The .InsertFile method never
seemed to suffer from that so I was hoping there was a method to capture the
text into a string variable or something rather than just put it in the
document. Seems a little unfair that the makers of Word would use preview
controls in their forms that aren't available to VBA programmers. :)

I'm not sure if I trust my end users with an open dialogue box. I know it
can be limited in scope and type of file to open, but I can't shake the fear
that they'd find SOME way to open up the wrong thing. And generating the
preview in the dialogue box seems to take up quite a bit of time too.

I think I'll try experimenting with opening a document in invisible mode and
see how long that takes and what can be read from an invisible document.
 
J

Jonathan West

Mickey F. said:
Thank you for the reply Jonathan! That's a great idea I never would have
come up with that on my own. The reason I didn't want to open each
document
is because I was afraid of network lag time. The .InsertFile method never
seemed to suffer from that so I was hoping there was a method to capture
the
text into a string variable or something rather than just put it in the
document. Seems a little unfair that the makers of Word would use preview
controls in their forms that aren't available to VBA programmers. :)

Its worse than that even! It *used* to be available to us in the custom
dialogs that were part of Wordbasic, which was superseded by VBA when Office
97 was introduced.
I'm not sure if I trust my end users with an open dialogue box. I know it
can be limited in scope and type of file to open, but I can't shake the
fear
that they'd find SOME way to open up the wrong thing.

Using the Display method rather than the Show method means that the file
isn't actually opened when the user selects the file. You can read the .Name
property and combine it with CurDir to get the full path, and perform a
validity check against the filename.
And generating the
preview in the dialogue box seems to take up quite a bit of time too.

Yes. Inevitable.
I think I'll try experimenting with opening a document in invisible mode
and
see how long that takes and what can be read from an invisible document.

I suspect this will be even longer, but it is always worth experimenting
with different approaches. Sometimes you get performance differences which
are the opposite of what you would intuitively expect.
 
P

perry

From Office XP on, you use dialog FileDialog to open in Preview mode, like
examplified in below code...
It's not entirely what y're after, but covers the "preview" need

Dim d As FileDialog
Set d = Application.FileDialog(msoFileDialogOpen)
d.InitialView = msoFileDialogViewPreview
d.Show

Works in Windows XP.
In Vista the above code will need some adjustments...
 

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