storing info about current document to use later, when document is inactive

L

Larry

How do you store information about the currently active document in a
macro or function, in order to access it later?

For example, I want to store the name of the current document, so that I
can run that name later, when a different document is active.

I know about storing information about a document in settings.text, and
then accessing it later, as below. I just wondered if there was a way
to do the same thing completely within Word.

Selection.Font.name = System.PrivateProfileString("C:\Settings.Txt", _
"MacroSettings", "Font")


Thanks,
Larry
 
L

Larry

Thanks for that reference. I ended up using the AutoText method. I was
having trouble making the Settings.txt method work with this one, I
don't know why.

Larry
 
L

Larry

Here's what I did. The first macro creates an AutoText entry with the
name of the active document. Then, after I've activated another
document, and want to return to the first, I run the second macro which
takes me back to the first document.

The reason I wanted this was, often I have a bunch of documents open,
I'm working on one main document, then going to some other document, and
then I either have to cycle through the documents to get back to the
main document, or else open the Window menu to look for the window
number of the main document and press that. I wanted something that
would be automatically and in one step take me back to the document I
was working on.

Sub DocNameStore()

Application.ScreenUpdating = False
ActiveWindow.SplitVertical = 50
Selection.EndKey wdStory
Selection.InsertAfter ActiveDocument.Name
NormalTemplate.AutoTextEntries.Add Name:="MyDocName",
Range:=Selection.Range
Selection.TypeBackspace
ActiveWindow.SplitVertical = 100

End Sub


Sub PrevDocActivate()

Dim PrevDocName As String
PrevDocName = NormalTemplate.AutoTextEntries("MyDocName").Value
Documents(PrevDocName).Activate

End Sub
 
L

Larry

This is better. Now the operation has no effect on the on-screen
appearance of the document.

Sub DocNameStore()

Dim X As Long, Y As Long
Dim r As Range
X = ActiveDocument.Range.End - 1
ActiveDocument.Range.InsertAfter ActiveDocument.Name
Y = ActiveDocument.Range.End - 1
Set r = ActiveDocument.Range(Start:=X, End:=Y)
NormalTemplate.AutoTextEntries.Add Name:="MyDocName", Range:=r
r.Delete

End Sub
 
T

Tony

You can also use ProfileString (storing in registry) to
store the information e.g.

System.ProfileString("SubkeyName", "EntryName") = "Value"
MsgBox System.ProfileString("SubkeyName", "EntryName")

or store the information in normal.dot as a document
variable e.g.

Sub Test000()
ScreenUpdating = False

With NormalTemplate.OpenAsDocument
.Variables.Add Name:="UserName",
Value:=Application.UserName
.Close SaveChanges:=wdSaveChanges
End With

ScreenUpdating = True
End Sub

Grtz, Tony
 

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