Home |
Search |
Today's Posts |
#1
|
|||
|
|||
How to make doc's FullName into an AutoText entry without first inserting it into document
This macro works fine. It temporarily Inserts Fullname of the active document into the document and makes that string an AutoText entry called MyDocName, which is used later by another macro to reactivate this document from another document. This enables me to return instantly to this document without having to open the Window menu or scroll through the open documents. But I wonder if there is a more efficient way of doing it, so that the active document's FullName doesn't actually have to be inserted into the document as a range, but rather the FullName becomes an AutoText entry in one step. It seems that would make the macro a little faster. Any ideas? Thanks. Larry .. Sub DocFullNameStore() Application.ScreenUpdating = False Dim X As Long, Y As Long Dim r As Range X = ActiveDocument.Range.End - 1 ActiveDocument.Range.InsertAfter ActiveDocument.FullName Y = ActiveDocument.Range.End - 1 Set r = ActiveDocument.Range(Start:=X, End:=Y) NormalTemplate.AutoTextEntries.Add Name:="MyDocFullName", Range:=r r.Delete End Sub |
#2
|
|||
|
|||
Storing the name as a DocumentProperty or DocVariable would be simpler; but
if the name needs to be retained only for the current session, why not simply put it into a static variable within your VBA module? "Larry" wrote in message ... This macro works fine. It temporarily Inserts Fullname of the active document into the document and makes that string an AutoText entry called MyDocName, which is used later by another macro to reactivate this document from another document. This enables me to return instantly to this document without having to open the Window menu or scroll through the open documents. But I wonder if there is a more efficient way of doing it, so that the active document's FullName doesn't actually have to be inserted into the document as a range, but rather the FullName becomes an AutoText entry in one step. It seems that would make the macro a little faster. Any ideas? Thanks. Larry . Sub DocFullNameStore() Application.ScreenUpdating = False Dim X As Long, Y As Long Dim r As Range X = ActiveDocument.Range.End - 1 ActiveDocument.Range.InsertAfter ActiveDocument.FullName Y = ActiveDocument.Range.End - 1 Set r = ActiveDocument.Range(Start:=X, End:=Y) NormalTemplate.AutoTextEntries.Add Name:="MyDocFullName", Range:=r r.Delete End Sub |
#3
|
|||
|
|||
The purpose of this is to be able to activate this document by a single command from another document. How would putting a static variable within the module enable me to do that? So that you can better understand what I'm doing here, here is the second macro, by which I activate the first document (the one whose name has been made into an AutoText entry by the first macro) from another document. What would be the equivalent of this using a variable instead of an AutoText entry? Sub PrevDocActivate() ' Open (or activate) the document whose name (or fullname) has been made into an ' AutoText entry. ' Uses Activate method if document is open, and Open method is document is closed. 'Application.ScreenUpdating = False Dim sDoc As Document Dim PrevDocName As String PrevDocName = NormalTemplate.AutoTextEntries("MyDocFullName").Va lue 'Instead of opening in all cases, do a For Each statement. For Each sDoc In Documents If sDoc.FullName = PrevDocName Then myFlag1 = True Exit For End If Next If myFlag1 = True Then 'DocFullNameStore Documents(PrevDocName).Activate Else Documents.Open (PrevDocName) End If End Sub Jezebel wrote: Storing the name as a DocumentProperty or DocVariable would be simpler; but if the name needs to be retained only for the current session, why not simply put it into a static variable within your VBA module? "Larry" wrote in message ... This macro works fine. It temporarily Inserts Fullname of the active document into the document and makes that string an AutoText entry called MyDocName, which is used later by another macro to reactivate this document from another document. This enables me to return instantly to this document without having to open the Window menu or scroll through the open documents. But I wonder if there is a more efficient way of doing it, so that the active document's FullName doesn't actually have to be inserted into the document as a range, but rather the FullName becomes an AutoText entry in one step. It seems that would make the macro a little faster. Any ideas? Thanks. Larry . Sub DocFullNameStore() Application.ScreenUpdating = False Dim X As Long, Y As Long Dim r As Range X = ActiveDocument.Range.End - 1 ActiveDocument.Range.InsertAfter ActiveDocument.FullName Y = ActiveDocument.Range.End - 1 Set r = ActiveDocument.Range(Start:=X, End:=Y) NormalTemplate.AutoTextEntries.Add Name:="MyDocFullName", Range:=r r.Delete End Sub |
#4
|
|||
|
|||
I guess my real question is, how do I put a variable in a function, so
that it can then be accessed later by a macro? Then it would work like this. Macro 1 defines the FullName of the active document as a variable, MyDocFullName. This variable is then placed in a function. Then, after I've activated a different document, I run Macro 2, which accesses the variable MyDocFullName from the function and sticks it into a line of code: Documents(MyDocFullName).Activate Larry Larry wrote: The purpose of this is to be able to activate this document by a single command from another document. How would putting a static variable within the module enable me to do that? So that you can better understand what I'm doing here, here is the second macro, by which I activate the first document (the one whose name has been made into an AutoText entry by the first macro) from another document. What would be the equivalent of this using a variable instead of an AutoText entry? Sub PrevDocActivate() ' Open (or activate) the document whose name (or fullname) has been made into an ' AutoText entry. ' Uses Activate method if document is open, and Open method is document is closed. 'Application.ScreenUpdating = False Dim sDoc As Document Dim PrevDocName As String PrevDocName = NormalTemplate.AutoTextEntries("MyDocFullName").Va lue 'Instead of opening in all cases, do a For Each statement. For Each sDoc In Documents If sDoc.FullName = PrevDocName Then myFlag1 = True Exit For End If Next If myFlag1 = True Then 'DocFullNameStore Documents(PrevDocName).Activate Else Documents.Open (PrevDocName) End If End Sub Jezebel wrote: Storing the name as a DocumentProperty or DocVariable would be simpler; but if the name needs to be retained only for the current session, why not simply put it into a static variable within your VBA module? "Larry" wrote in message ... This macro works fine. It temporarily Inserts Fullname of the active document into the document and makes that string an AutoText entry called MyDocName, which is used later by another macro to reactivate this document from another document. This enables me to return instantly to this document without having to open the Window menu or scroll through the open documents. But I wonder if there is a more efficient way of doing it, so that the active document's FullName doesn't actually have to be inserted into the document as a range, but rather the FullName becomes an AutoText entry in one step. It seems that would make the macro a little faster. Any ideas? Thanks. Larry . Sub DocFullNameStore() Application.ScreenUpdating = False Dim X As Long, Y As Long Dim r As Range X = ActiveDocument.Range.End - 1 ActiveDocument.Range.InsertAfter ActiveDocument.FullName Y = ActiveDocument.Range.End - 1 Set r = ActiveDocument.Range(Start:=X, End:=Y) NormalTemplate.AutoTextEntries.Add Name:="MyDocFullName", Range:=r r.Delete End Sub |
#5
|
|||
|
|||
Module level variable --
Dim mDoc as Word.Document Sub Macro1() : set mDoc = ActiveDocument : End Sub Sub PrevDocActivate mDoc.Activate End Sub Bear in mind that while you're writing or debugging your code, mDoc will get cleared whenever you make any changes to any code. "Larry" wrote in message ... I guess my real question is, how do I put a variable in a function, so that it can then be accessed later by a macro? Then it would work like this. Macro 1 defines the FullName of the active document as a variable, MyDocFullName. This variable is then placed in a function. Then, after I've activated a different document, I run Macro 2, which accesses the variable MyDocFullName from the function and sticks it into a line of code: Documents(MyDocFullName).Activate Larry Larry wrote: The purpose of this is to be able to activate this document by a single command from another document. How would putting a static variable within the module enable me to do that? So that you can better understand what I'm doing here, here is the second macro, by which I activate the first document (the one whose name has been made into an AutoText entry by the first macro) from another document. What would be the equivalent of this using a variable instead of an AutoText entry? Sub PrevDocActivate() ' Open (or activate) the document whose name (or fullname) has been made into an ' AutoText entry. ' Uses Activate method if document is open, and Open method is document is closed. 'Application.ScreenUpdating = False Dim sDoc As Document Dim PrevDocName As String PrevDocName = NormalTemplate.AutoTextEntries("MyDocFullName").Va lue 'Instead of opening in all cases, do a For Each statement. For Each sDoc In Documents If sDoc.FullName = PrevDocName Then myFlag1 = True Exit For End If Next If myFlag1 = True Then 'DocFullNameStore Documents(PrevDocName).Activate Else Documents.Open (PrevDocName) End If End Sub Jezebel wrote: Storing the name as a DocumentProperty or DocVariable would be simpler; but if the name needs to be retained only for the current session, why not simply put it into a static variable within your VBA module? "Larry" wrote in message ... This macro works fine. It temporarily Inserts Fullname of the active document into the document and makes that string an AutoText entry called MyDocName, which is used later by another macro to reactivate this document from another document. This enables me to return instantly to this document without having to open the Window menu or scroll through the open documents. But I wonder if there is a more efficient way of doing it, so that the active document's FullName doesn't actually have to be inserted into the document as a range, but rather the FullName becomes an AutoText entry in one step. It seems that would make the macro a little faster. Any ideas? Thanks. Larry . Sub DocFullNameStore() Application.ScreenUpdating = False Dim X As Long, Y As Long Dim r As Range X = ActiveDocument.Range.End - 1 ActiveDocument.Range.InsertAfter ActiveDocument.FullName Y = ActiveDocument.Range.End - 1 Set r = ActiveDocument.Range(Start:=X, End:=Y) NormalTemplate.AutoTextEntries.Add Name:="MyDocFullName", Range:=r r.Delete End Sub |
#6
|
|||
|
|||
Fantastic, so simple.
There's one drawback however. This would work only with open documents. My older macro, which uses the actual FullName of the document as an AutoText entry, can also open a closed document. However, this would still be great with an alternative: a macro that will alternatively activate two documents. Let's say I have seven documents open and I just want to go back and forth between two of them for a while. Instead of having to deal with the Window menu or using NextWindow to find the destination document each time, a simply macro will do it. Larry Jezebel wrote: Module level variable -- Dim mDoc as Word.Document Sub Macro1() : set mDoc = ActiveDocument : End Sub Sub PrevDocActivate mDoc.Activate End Sub Bear in mind that while you're writing or debugging your code, mDoc will get cleared whenever you make any changes to any code. "Larry" wrote in message ... I guess my real question is, how do I put a variable in a function, so that it can then be accessed later by a macro? Then it would work like this. Macro 1 defines the FullName of the active document as a variable, MyDocFullName. This variable is then placed in a function. Then, after I've activated a different document, I run Macro 2, which accesses the variable MyDocFullName from the function and sticks it into a line of code: Documents(MyDocFullName).Activate Larry Larry wrote: The purpose of this is to be able to activate this document by a single command from another document. How would putting a static variable within the module enable me to do that? So that you can better understand what I'm doing here, here is the second macro, by which I activate the first document (the one whose name has been made into an AutoText entry by the first macro) from another document. What would be the equivalent of this using a variable instead of an AutoText entry? Sub PrevDocActivate() ' Open (or activate) the document whose name (or fullname) has been made into an ' AutoText entry. ' Uses Activate method if document is open, and Open method is document is closed. 'Application.ScreenUpdating = False Dim sDoc As Document Dim PrevDocName As String PrevDocName = NormalTemplate.AutoTextEntries("MyDocFullName").Va lue 'Instead of opening in all cases, do a For Each statement. For Each sDoc In Documents If sDoc.FullName = PrevDocName Then myFlag1 = True Exit For End If Next If myFlag1 = True Then 'DocFullNameStore Documents(PrevDocName).Activate Else Documents.Open (PrevDocName) End If End Sub Jezebel wrote: Storing the name as a DocumentProperty or DocVariable would be simpler; but if the name needs to be retained only for the current session, why not simply put it into a static variable within your VBA module? "Larry" wrote in message ... This macro works fine. It temporarily Inserts Fullname of the active document into the document and makes that string an AutoText entry called MyDocName, which is used later by another macro to reactivate this document from another document. This enables me to return instantly to this document without having to open the Window menu or scroll through the open documents. But I wonder if there is a more efficient way of doing it, so that the active document's FullName doesn't actually have to be inserted into the document as a range, but rather the FullName becomes an AutoText entry in one step. It seems that would make the macro a little faster. Any ideas? Thanks. Larry . Sub DocFullNameStore() Application.ScreenUpdating = False Dim X As Long, Y As Long Dim r As Range X = ActiveDocument.Range.End - 1 ActiveDocument.Range.InsertAfter ActiveDocument.FullName Y = ActiveDocument.Range.End - 1 Set r = ActiveDocument.Range(Start:=X, End:=Y) NormalTemplate.AutoTextEntries.Add Name:="MyDocFullName", Range:=r r.Delete End Sub |
#7
|
|||
|
|||
It will work with strings also, if you need to deal with documents that are
closed. Dim pFileName as string Dim pDoc as Word.Document : Set pDoc = Documents.Open(pFileName) : "Larry" wrote in message ... Fantastic, so simple. There's one drawback however. This would work only with open documents. My older macro, which uses the actual FullName of the document as an AutoText entry, can also open a closed document. However, this would still be great with an alternative: a macro that will alternatively activate two documents. Let's say I have seven documents open and I just want to go back and forth between two of them for a while. Instead of having to deal with the Window menu or using NextWindow to find the destination document each time, a simply macro will do it. Larry Jezebel wrote: Module level variable -- Dim mDoc as Word.Document Sub Macro1() : set mDoc = ActiveDocument : End Sub Sub PrevDocActivate mDoc.Activate End Sub Bear in mind that while you're writing or debugging your code, mDoc will get cleared whenever you make any changes to any code. "Larry" wrote in message ... I guess my real question is, how do I put a variable in a function, so that it can then be accessed later by a macro? Then it would work like this. Macro 1 defines the FullName of the active document as a variable, MyDocFullName. This variable is then placed in a function. Then, after I've activated a different document, I run Macro 2, which accesses the variable MyDocFullName from the function and sticks it into a line of code: Documents(MyDocFullName).Activate Larry Larry wrote: The purpose of this is to be able to activate this document by a single command from another document. How would putting a static variable within the module enable me to do that? So that you can better understand what I'm doing here, here is the second macro, by which I activate the first document (the one whose name has been made into an AutoText entry by the first macro) from another document. What would be the equivalent of this using a variable instead of an AutoText entry? Sub PrevDocActivate() ' Open (or activate) the document whose name (or fullname) has been made into an ' AutoText entry. ' Uses Activate method if document is open, and Open method is document is closed. 'Application.ScreenUpdating = False Dim sDoc As Document Dim PrevDocName As String PrevDocName = NormalTemplate.AutoTextEntries("MyDocFullName").Va lue 'Instead of opening in all cases, do a For Each statement. For Each sDoc In Documents If sDoc.FullName = PrevDocName Then myFlag1 = True Exit For End If Next If myFlag1 = True Then 'DocFullNameStore Documents(PrevDocName).Activate Else Documents.Open (PrevDocName) End If End Sub Jezebel wrote: Storing the name as a DocumentProperty or DocVariable would be simpler; but if the name needs to be retained only for the current session, why not simply put it into a static variable within your VBA module? "Larry" wrote in message ... This macro works fine. It temporarily Inserts Fullname of the active document into the document and makes that string an AutoText entry called MyDocName, which is used later by another macro to reactivate this document from another document. This enables me to return instantly to this document without having to open the Window menu or scroll through the open documents. But I wonder if there is a more efficient way of doing it, so that the active document's FullName doesn't actually have to be inserted into the document as a range, but rather the FullName becomes an AutoText entry in one step. It seems that would make the macro a little faster. Any ideas? Thanks. Larry . Sub DocFullNameStore() Application.ScreenUpdating = False Dim X As Long, Y As Long Dim r As Range X = ActiveDocument.Range.End - 1 ActiveDocument.Range.InsertAfter ActiveDocument.FullName Y = ActiveDocument.Range.End - 1 Set r = ActiveDocument.Range(Start:=X, End:=Y) NormalTemplate.AutoTextEntries.Add Name:="MyDocFullName", Range:=r r.Delete End Sub |
#8
|
|||
|
|||
Cool. I had given up on that, thinking it wasn't necessary anyway. I'l have two altnernative macros now. One that activates a certain document (even if it's closed) from any other document (I'll use this new info for that). And one that alternatively activates two documents so I can go back and forth between them with a single command no matter how many other documents are open. I did it by adding another public variable, mDoc2. Here's the code: Sub TwoDocsAltActivate() If ActiveDocument mDoc Then Set mDoc2 = ActiveDocument mDoc.Activate Else mDoc2.Activate End If End Sub Jezebel wrote: It will work with strings also, if you need to deal with documents that are closed. Dim pFileName as string Dim pDoc as Word.Document Set pDoc = Documents.Open(pFileName) "Larry" wrote in message ... Fantastic, so simple. There's one drawback however. This would work only with open documents. My older macro, which uses the actual FullName of the document as an AutoText entry, can also open a closed document. However, this would still be great with an alternative: a macro that will alternatively activate two documents. Let's say I have seven documents open and I just want to go back and forth between two of them for a while. Instead of having to deal with the Window menu or using NextWindow to find the destination document each time, a simply macro will do it. Larry Jezebel wrote: Module level variable -- Dim mDoc as Word.Document Sub Macro1() : set mDoc = ActiveDocument : End Sub Sub PrevDocActivate mDoc.Activate End Sub Bear in mind that while you're writing or debugging your code, mDoc will get cleared whenever you make any changes to any code. "Larry" wrote in message ... I guess my real question is, how do I put a variable in a function, so that it can then be accessed later by a macro? Then it would work like this. Macro 1 defines the FullName of the active document as a variable, MyDocFullName. This variable is then placed in a function. Then, after I've activated a different document, I run Macro 2, which accesses the variable MyDocFullName from the function and sticks it into a line of code: Documents(MyDocFullName).Activate Larry Larry wrote: The purpose of this is to be able to activate this document by a single command from another document. How would putting a static variable within the module enable me to do that? So that you can better understand what I'm doing here, here is the second macro, by which I activate the first document (the one whose name has been made into an AutoText entry by the first macro) from another document. What would be the equivalent of this using a variable instead of an AutoText entry? Sub PrevDocActivate() ' Open (or activate) the document whose name (or fullname) has been made into an ' AutoText entry. ' Uses Activate method if document is open, and Open method is document is closed. 'Application.ScreenUpdating = False Dim sDoc As Document Dim PrevDocName As String PrevDocName = NormalTemplate.AutoTextEntries("MyDocFullName").Va lue 'Instead of opening in all cases, do a For Each statement. For Each sDoc In Documents If sDoc.FullName = PrevDocName Then myFlag1 = True Exit For End If Next If myFlag1 = True Then 'DocFullNameStore Documents(PrevDocName).Activate Else Documents.Open (PrevDocName) End If End Sub Jezebel wrote: Storing the name as a DocumentProperty or DocVariable would be simpler; but if the name needs to be retained only for the current session, why not simply put it into a static variable within your VBA module? "Larry" wrote in message ... This macro works fine. It temporarily Inserts Fullname of the active document into the document and makes that string an AutoText entry called MyDocName, which is used later by another macro to reactivate this document from another document. This enables me to return instantly to this document without having to open the Window menu or scroll through the open documents. But I wonder if there is a more efficient way of doing it, so that the active document's FullName doesn't actually have to be inserted into the document as a range, but rather the FullName becomes an AutoText entry in one step. It seems that would make the macro a little faster. Any ideas? Thanks. Larry . Sub DocFullNameStore() Application.ScreenUpdating = False Dim X As Long, Y As Long Dim r As Range X = ActiveDocument.Range.End - 1 ActiveDocument.Range.InsertAfter ActiveDocument.FullName Y = ActiveDocument.Range.End - 1 Set r = ActiveDocument.Range(Start:=X, End:=Y) NormalTemplate.AutoTextEntries.Add Name:="MyDocFullName", Range:=r r.Delete End Sub |
#9
|
|||
|
|||
Be a little careful here. This line
If ActiveDocument mDoc Then isn't doing what you think it is. If you want to compare objects you have to use the IS keyword -- If not (ActiveDoicument is mDoc) then ... Otherwise, VB compares the default properties of the objects. The default property of a document is its Name, so your current line is actually shorthand for If ActiveDocument.Name mDoc.Name then which in this case works, but that won't always be the case. "Larry" wrote in message ... Cool. I had given up on that, thinking it wasn't necessary anyway. I'l have two altnernative macros now. One that activates a certain document (even if it's closed) from any other document (I'll use this new info for that). And one that alternatively activates two documents so I can go back and forth between them with a single command no matter how many other documents are open. I did it by adding another public variable, mDoc2. Here's the code: Sub TwoDocsAltActivate() If ActiveDocument mDoc Then Set mDoc2 = ActiveDocument mDoc.Activate Else mDoc2.Activate End If End Sub Jezebel wrote: It will work with strings also, if you need to deal with documents that are closed. Dim pFileName as string Dim pDoc as Word.Document Set pDoc = Documents.Open(pFileName) "Larry" wrote in message ... Fantastic, so simple. There's one drawback however. This would work only with open documents. My older macro, which uses the actual FullName of the document as an AutoText entry, can also open a closed document. However, this would still be great with an alternative: a macro that will alternatively activate two documents. Let's say I have seven documents open and I just want to go back and forth between two of them for a while. Instead of having to deal with the Window menu or using NextWindow to find the destination document each time, a simply macro will do it. Larry Jezebel wrote: Module level variable -- Dim mDoc as Word.Document Sub Macro1() : set mDoc = ActiveDocument : End Sub Sub PrevDocActivate mDoc.Activate End Sub Bear in mind that while you're writing or debugging your code, mDoc will get cleared whenever you make any changes to any code. "Larry" wrote in message ... I guess my real question is, how do I put a variable in a function, so that it can then be accessed later by a macro? Then it would work like this. Macro 1 defines the FullName of the active document as a variable, MyDocFullName. This variable is then placed in a function. Then, after I've activated a different document, I run Macro 2, which accesses the variable MyDocFullName from the function and sticks it into a line of code: Documents(MyDocFullName).Activate Larry Larry wrote: The purpose of this is to be able to activate this document by a single command from another document. How would putting a static variable within the module enable me to do that? So that you can better understand what I'm doing here, here is the second macro, by which I activate the first document (the one whose name has been made into an AutoText entry by the first macro) from another document. What would be the equivalent of this using a variable instead of an AutoText entry? Sub PrevDocActivate() ' Open (or activate) the document whose name (or fullname) has been made into an ' AutoText entry. ' Uses Activate method if document is open, and Open method is document is closed. 'Application.ScreenUpdating = False Dim sDoc As Document Dim PrevDocName As String PrevDocName = NormalTemplate.AutoTextEntries("MyDocFullName").Va lue 'Instead of opening in all cases, do a For Each statement. For Each sDoc In Documents If sDoc.FullName = PrevDocName Then myFlag1 = True Exit For End If Next If myFlag1 = True Then 'DocFullNameStore Documents(PrevDocName).Activate Else Documents.Open (PrevDocName) End If End Sub Jezebel wrote: Storing the name as a DocumentProperty or DocVariable would be simpler; but if the name needs to be retained only for the current session, why not simply put it into a static variable within your VBA module? "Larry" wrote in message ... This macro works fine. It temporarily Inserts Fullname of the active document into the document and makes that string an AutoText entry called MyDocName, which is used later by another macro to reactivate this document from another document. This enables me to return instantly to this document without having to open the Window menu or scroll through the open documents. But I wonder if there is a more efficient way of doing it, so that the active document's FullName doesn't actually have to be inserted into the document as a range, but rather the FullName becomes an AutoText entry in one step. It seems that would make the macro a little faster. Any ideas? Thanks. Larry . Sub DocFullNameStore() Application.ScreenUpdating = False Dim X As Long, Y As Long Dim r As Range X = ActiveDocument.Range.End - 1 ActiveDocument.Range.InsertAfter ActiveDocument.FullName Y = ActiveDocument.Range.End - 1 Set r = ActiveDocument.Range(Start:=X, End:=Y) NormalTemplate.AutoTextEntries.Add Name:="MyDocFullName", Range:=r r.Delete End Sub |
#10
|
|||
|
|||
I seem to have it working perfectly now.
Thanks a lot for your help. Larry Jezebel wrote: Be a little careful here. This line If ActiveDocument mDoc Then isn't doing what you think it is. If you want to compare objects you have to use the IS keyword -- If not (ActiveDoicument is mDoc) then ... Otherwise, VB compares the default properties of the objects. The default property of a document is its Name, so your current line is actually shorthand for If ActiveDocument.Name mDoc.Name then which in this case works, but that won't always be the case. "Larry" wrote in message ... Cool. I had given up on that, thinking it wasn't necessary anyway. I'l have two altnernative macros now. One that activates a certain document (even if it's closed) from any other document (I'll use this new info for that). And one that alternatively activates two documents so I can go back and forth between them with a single command no matter how many other documents are open. I did it by adding another public variable, mDoc2. Here's the code: Sub TwoDocsAltActivate() If ActiveDocument mDoc Then Set mDoc2 = ActiveDocument mDoc.Activate Else mDoc2.Activate End If End Sub Jezebel wrote: It will work with strings also, if you need to deal with documents that are closed. Dim pFileName as string Dim pDoc as Word.Document Set pDoc = Documents.Open(pFileName) "Larry" wrote in message ... Fantastic, so simple. There's one drawback however. This would work only with open documents. My older macro, which uses the actual FullName of the document as an AutoText entry, can also open a closed document. However, this would still be great with an alternative: a macro that will alternatively activate two documents. Let's say I have seven documents open and I just want to go back and forth between two of them for a while. Instead of having to deal with the Window menu or using NextWindow to find the destination document each time, a simply macro will do it. Larry Jezebel wrote: Module level variable -- Dim mDoc as Word.Document Sub Macro1() : set mDoc = ActiveDocument : End Sub Sub PrevDocActivate mDoc.Activate End Sub Bear in mind that while you're writing or debugging your code, mDoc will get cleared whenever you make any changes to any code. "Larry" wrote in message ... I guess my real question is, how do I put a variable in a function, so that it can then be accessed later by a macro? Then it would work like this. Macro 1 defines the FullName of the active document as a variable, MyDocFullName. This variable is then placed in a function. Then, after I've activated a different document, I run Macro 2, which accesses the variable MyDocFullName from the function and sticks it into a line of code: Documents(MyDocFullName).Activate Larry Larry wrote: The purpose of this is to be able to activate this document by a single command from another document. How would putting a static variable within the module enable me to do that? So that you can better understand what I'm doing here, here is the second macro, by which I activate the first document (the one whose name has been made into an AutoText entry by the first macro) from another document. What would be the equivalent of this using a variable instead of an AutoText entry? Sub PrevDocActivate() ' Open (or activate) the document whose name (or fullname) has been made into an ' AutoText entry. ' Uses Activate method if document is open, and Open method is document is closed. 'Application.ScreenUpdating = False Dim sDoc As Document Dim PrevDocName As String PrevDocName = NormalTemplate.AutoTextEntries("MyDocFullName").Va lue 'Instead of opening in all cases, do a For Each statement. For Each sDoc In Documents If sDoc.FullName = PrevDocName Then myFlag1 = True Exit For End If Next If myFlag1 = True Then 'DocFullNameStore Documents(PrevDocName).Activate Else Documents.Open (PrevDocName) End If End Sub Jezebel wrote: Storing the name as a DocumentProperty or DocVariable would be simpler; but if the name needs to be retained only for the current session, why not simply put it into a static variable within your VBA module? "Larry" wrote in message ... This macro works fine. It temporarily Inserts Fullname of the active document into the document and makes that string an AutoText entry called MyDocName, which is used later by another macro to reactivate this document from another document. This enables me to return instantly to this document without having to open the Window menu or scroll through the open documents. But I wonder if there is a more efficient way of doing it, so that the active document's FullName doesn't actually have to be inserted into the document as a range, but rather the FullName becomes an AutoText entry in one step. It seems that would make the macro a little faster. Any ideas? Thanks. Larry . Sub DocFullNameStore() Application.ScreenUpdating = False Dim X As Long, Y As Long Dim r As Range X = ActiveDocument.Range.End - 1 ActiveDocument.Range.InsertAfter ActiveDocument.FullName Y = ActiveDocument.Range.End - 1 Set r = ActiveDocument.Range(Start:=X, End:=Y) NormalTemplate.AutoTextEntries.Add Name:="MyDocFullName", Range:=r r.Delete End Sub |
Reply |
Thread Tools | |
Display Modes | |
|
|
Similar Threads | ||||
Thread | Forum | |||
simple word question | New Users | |||
Autotext exclusive to a New Document Template I Create in Word 200 | Microsoft Word Help | |||
auto current date entry in your document | Microsoft Word Help | |||
Inserting a hyperlink - Place in this document | Microsoft Word Help | |||
Using Hyperlinks in Mail Merge IF...THEN...ELSE Statements | Mailmerge |