Pass variable from ThisDocument to Form

K

Kev

My apologies if this is an obvious question, but it's late at night and

my brain is descending into numbness.

I have a word 2003 macro coded into the ThisDocument module of the
template
so that it auto executes on opening the document.


Part of this macro defines a variable called docname which equals the
path name to a pdf file.


I then open up a form (Userform1). I need to put a button on this form

which will open the pdf file listed in the docname variable.


I've read a few post on public variables and tried to implement, but
cant get my head around the explanations.


Can someone please explain how I access the contents of the docname
variable from my form?


Cheers


Kevin
Western Australia
 
C

Cindy M.

Hi Kev,

You say you assign the name to a variable: a document variable
(Documents.Variables) or a variable in your code? I'm going to assume the
latter, but did have to ask...

At the top of the ThisDocument module type:

Public docname as String

Remove the declaration (Dim) of docname in the macro (Document_Open, I
assume).

Now the code behind the form should be able to "talk to" docname.
My apologies if this is an obvious question, but it's late at night and

my brain is descending into numbness.

I have a word 2003 macro coded into the ThisDocument module of the
template
so that it auto executes on opening the document.


Part of this macro defines a variable called docname which equals the
path name to a pdf file.


I then open up a form (Userform1). I need to put a button on this form

which will open the pdf file listed in the docname variable.


I've read a few post on public variables and tried to implement, but
cant get my head around the explanations.


Can someone please explain how I access the contents of the docname
variable from my form?

Cindy Meister
INTER-Solutions, Switzerland
http://homepage.swissonline.ch/cindymeister (last update Jun 17 2005)
http://www.word.mvps.org

This reply is posted in the Newsgroup; please post any follow question or
reply in the newsgroup and not by e-mail :)
 
K

Kev

Hi Cindy, thanks for your post.

Im still having issues and thought I would paste my code here.

This is the code from ThisDocument module of the Word doc:
===================
Public docname As String


Public Sub Document_Open()
docname = "C:\Documents and Settings\Administrator\My Documents\my.pdf"
UserForm1.Show

End Sub
====================

On Userform1 I have a single button. Here is the code:
====================
Public Sub CommandButton1_Click()
Call Shell("""C:\Program Files\Adobe\Acrobat 7.0\Reader\acrord32.exe""
" & docname, 1)

End Sub
====================

Acrobat Reader opens, but does not open my.pdf (as defined in the
docname variable).
If I hard code the pdf file path, it does open.
I also tried msgbox docname just before the call statement, and it
showed nothing in docname.

What is it that I'm missing (blonde hair tint on standby!)

Cheers

Kevin
 
K

Kodeworks

Kevin

There are a two things you need to fix in Command1_Click.

1. Correctly reference the 'docname' property that you've declared in
ThisDocument.
It should be 'ThisDocument.docname'. A public variable in declared in
an object must be referred to as a property of that object from outside
that object. (Sorry, I can't make it any more clear than that - it's
late for me too :) )

2. You need to surround 'docname' in the Shell command string with
double quotes as you have already done with the path to the acrobat
reader executable. Also, take note of the single space character to
separate the executable and file name strings.

Public Sub CommandButton1_Click()
Call Shell("""C:\Program Files\Adobe\Acrobat 7.0\Reader\acrord32.exe""
& _
" " & """" & ThisDocument.docname & """", 1)
End Sub


Sunil Jadwani
Kodeworks - Business Automation Solutions
www.kodeworks.com
 

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