Chuck
Wed May 11 05:42:01 CDT 2005
Well, one way is to pass the file name as a parameter:
Sub FirstSub()
Dim pfilename As String
Dim pdoc As Word.Document
pfilename = ActiveDocument.FullName
Set pdoc = ActiveDocument
SecondSub pfilename, pdoc
End Sub
Sub SecondSub( _
ByVal pfilename As String, _
ByVal pdoc As Word.Document)
MsgBox pdoc.Name
End Sub
"Larry" wrote:
>
> >Even in a
> > one-person situation this is fraught. And since there are simpler and
> > better ways of passing data between functions, better to use good
> > programming practices from the start.
>
>
> Jezebel,
>
> What are the better ways of doing it?
>
> Here's an example of the way I use publicly declared variables. I've
> declared these public variables at the top of the module.
>
> Public pFileName As String
> Public pDoc As Word.Document
>
> When a document is active that I'm about to leave, but I want to be able
> to return to that document instantly from any other document, I run a
> macro that contains this code:
>
> pFileName = ActiveDocument.FullName
> Set pDoc = ActiveDocument
>
> Then, when I'm in some other document and want to activate or open the
> first document, I run this (I'm not showing the whole macro, which is a
> lot bigger than this, just the key parts of it for the purpose of
> discussion).
>
> ' If pDoc is an open document it is activated. If it is a closed
> document it is opened.
> For Each pDoc In Documents
> If pDoc.FullName = pFileName Then
> myFlag1 = True
> Exit For
> End If
> Next
> If myFlag1 = True Then
> Documents(pFileName).Activate
> Else
> Documents.Open (pFileName)
>
> This works very well. What would be a better (or safer?) way of doing
> the same thing?
>
> Larry
>
>
>
>
> Jezebel wrote:
> > As Chuck says, they're not inherently bad -- just that if you rank the
> > causes of bugs in software, globals are right up there at or near the
> > top of the list. It's fine in a simple app with only a few functions
> > -- the problem comes as the app gets more complex. Even in a
> > one-person situation this is fraught. And since there are simpler and
> > better ways of passing data between functions, better to use good
> > programming practices from the start.
> >
> >
> >
> > "Larry" <larry328NOSPAM@att.net> wrote in message
> > news:%23TvxaOmUFHA.4000@TK2MSFTNGP14.phx.gbl...
> > > Well, to the extent I can follow his technical exposition, which
> > > is'nt very much, this would seem to be a potential problem in a
> > > shared environment. I don't see how it could be dangerous on an
> > > individual user's version of Word.
> > >
> > > I use these variables for a very simple purpose in just a few
> > > macros: to re-activate or re-open a previously active document, or
> > > to go back and forth instantly between any two documents without
> > > having to open the Window menu or use the "NextWindow" command.
> > >
> > >
> > >
> > > Chuck wrote:
> > > > Here's a link to an explanation of why global variables can cause
> > > > problems but also why they're not inherently a bad thing:
> > > >
> > > >
http://cpptips.hyperformix.com/cpptips/evil_globals
> > > >
> > > >
> > > >
> > > > "Larry" wrote:
> > > >
> > > > > I found the answer in VB help. Instead of
> > > > >
> > > > > Dim pFileName As String
> > > > >
> > > > > I write
> > > > >
> > > > > Public pFileName As String
> > > > >
> > > > >
> > > > >
> > > > > "Larry" <larry328NOSPAM@att.net> wrote in message
> > > > > news:u9IfLLdUFHA.228@TK2MSFTNGP12.phx.gbl...
> > > > > >
> > > > > > To clarify what I mean, here is the way the top of my Normal
> > > > > > template module now looks:
> > > > > >
> > > > > > Public Declare Sub Sleep Lib "kernel32" _
> > > > > > (ByVal dwMilliseconds As Long)
> > > > > >
> > > > > > ' This Public Declare Function works with MoveMouse macro
> > > > > > below. Public Declare Function SetCursorPos Lib "user32" _
> > > > > > (ByVal x As Long, ByVal Y As Long) As Long
> > > > > >
> > > > > > ' These four variables are used by SetDocAsDoc and
> > > > > > TwoDocsAltActivate. Dim pFileName As String
> > > > > > Dim pDoc As Word.Document
> > > > > > Dim pFileName2 As String
> > > > > > Dim pDoc2 As Word.Document
> > > > > > Dim sFullName As String
> > > > > > Dim myReadOnlyDoc As String
> > > > > >
> > > > > > If I remove the Public Declare statements, the declared
> > > > > > variables
> > > > > cease
> > > > > > to work. So if there is no Public Declare statement in a
> > > > > > module, how would I declare the module-wide variable?
> > > > > >
> > > > > >
> > > > > >
> > > > > >
> > > > > > "Larry" <larry328NOSPAM@att.net> wrote in message
> > > > > > news:uzuXlIdUFHA.2768@tk2msftngp13.phx.gbl...
> > > > > > >
> > > > > > > Sorry if I'm not using the terminology correctly.
> > > > > > >
> > > > > > > I already have several module-wide variables (created by Dim
> > > > > > statements)
> > > > > > > which enable information returned in one macro to be used in
> > > > > > > another macro--a neat capability. But these variables only
> > > > > > > seem to work
> > > > > > because
> > > > > > > there already happens to be some public declarations at the
> > > > > > > top of
> > > > > the
> > > > > > > module, like this:
> > > > > > >
> > > > > > > Public Declare Sub Sleep Lib "kernel32" _
> > > > > > > (ByVal dwMilliseconds As Long)
> > > > > > >
> > > > > > > These public declarations have nothing to do with the Dim
> > > > > statements,
> > > > > > > yet they enable them to work.
> > > > > > >
> > > > > > > I want to learn how to set up a module-wide variable if
> > > > > > > there is not already a Public declaration. In other words,
> > > > > > > if there is nothing
> > > > > at
> > > > > > > the top of the module, and I want to create a module-wide
> > > > > > > Dim
> > > > > > statement
> > > > > > > there, how do I do that?
> > > > > > >
> > > > > > > Thanks,
> > > > > > > Larry
>
>
>