PDA

View Full Version : Solved: Document Path in Title Bar Toggle Button



bstephens
07-19-2010, 07:59 PM
Hi, I have the following two macros:

'Displays Full Name in Title Bar

Sub TitleBarDisplayFullName()
ActiveWindow.Caption = ActiveDocument.FullName
End Sub
and

'Displays Name in Title Bar

Sub TitleBarDisplayName()
ActiveWindow.Caption = ActiveDocument.Name
End Sub
which I have as two separate macros to toggle between the "Name" and "FullName" in the word title bar.

Does anyone know how to combine these macros so that I can combine them into one button that will toggle between the two states?

Even more of a challenge, does anyone how to make the state ("Name" or "FullName") persistent so that if you exit word it will remember the last used state?

Appreciate any input!

Best,
Brian

geekgirlau
07-19-2010, 08:25 PM
This answers the first question:

Sub TitleBarDisplayName()
If ActiveWindow.Caption = ActiveDocument.Name Then
ActiveWindow.Caption = ActiveDocument.FullName
Else
ActiveWindow.Caption = ActiveDocument.Name
End If
End Sub


I'm not sure about the 2nd option. You could save the value as a document property in the document you are working with, but that would be specific to the document rather than Word. If you really wanted to do it another option would be to store the value in a text file or on the registery, but that is probably overkill for a simple toggle switch.

bstephens
07-20-2010, 09:07 AM
Hi lau, thanks for the code, I tried your code and derivations similar to that, but I couldn't get it to work.

On your code (and the other I tried), once the "if condition" is met, it no longer toggles.

Do I have to declare a variable passing the current state of the title bar ("Name" or "FullName") and then do some sort of logical comparison?

Best,
Brian

geekgirlau
07-20-2010, 03:31 PM
Mine toggled - can you post your code?

bstephens
07-20-2010, 04:41 PM
.

bstephens
07-20-2010, 04:42 PM
I used your code, as follows:

Sub TitleBarDisplayName()
If ActiveWindow.Caption = ActiveDocument.Name Then
ActiveWindow.Caption = ActiveDocument.FullName
Else
ActiveWindow.Caption = ActiveDocument.Name
End If
End Sub

On your code, the result I am getting is, if the document title bar displays the full path (in other words, ActiveWindow.Caption = ActiveDocument.FullName), then the macro will toggle it to ActiveDocument.Name, but once the title bar is set to ActiveWindow.Caption = ActiveDocument.Name, then it never toggles back to ActiveDocument.FullName.

I don't have enough experience to know how to solve it, do you have to use a variable, or can you do it purely with IF or CASE?

Thanks,
Brian

geekgirlau
07-20-2010, 06:31 PM
The code as written works, so it's a matter of looking at what's happening in your specific case.

In your code,

Highlight ActiveWindow.Caption
Click the right mouse button
Select Add Watch, OK
Highlight ActiveDocument.Name
Click the right mouse button
Select Add Watch, OKNow start stepping through the code by pressing [F8] once. The Watches window at the bottom of the screen will tell you the value of both the caption and the name so that you can compare them.

bstephens
07-20-2010, 07:22 PM
Thanks geekgirlau, the problem is "compatibility mode", I'll use "Doc.2" as an example:

ActiveDocument.Name = "Doc2.doc" and
ActiveWindow.Caption = "Doc2.doc [Compatibility Mode]"

I must have compatibility mode turned on because of a company policy.

Does anyone know how to modify the original procedure so that the strings will compare as expected even if compatibility mode is turned on?

Thanks,
Brian

geekgirlau
07-20-2010, 09:44 PM
Sub TitleBarDisplayName()
If Replace(ActiveWindow.Caption, " [Compatibility Mode]", "") = ActiveDocument.Name Then
ActiveWindow.Caption = ActiveDocument.FullName
Else
ActiveWindow.Caption = ActiveDocument.Name
End If
End Sub

bstephens
07-20-2010, 10:17 PM
Thanks!

Quick note for anyone else who is using compatibility mode and finds this code useful, I got it to work after removing the extra space in this line:

"If Replace(ActiveWindow.Caption, " [Compatibility Mode]", "") = ActiveDocument.Name Then"

Code was as follows:

IF USING COMPATIBILITY MODE:

Sub TitleBarDisplayName()
If Replace(ActiveWindow.Caption, " [Compatibility Mode]", "") = ActiveDocument.Name Then
ActiveWindow.Caption = ActiveDocument.FullName
Else
ActiveWindow.Caption = ActiveDocument.Name
End If
End Sub

IF NOT USING COMPATIBILITY MODE:

Sub TitleBarDisplayName()
If ActiveWindow.Caption = ActiveDocument.Name Then
ActiveWindow.Caption = ActiveDocument.FullName
Else
ActiveWindow.Caption = ActiveDocument.Name
End If
End Sub Anyone know how to make word remember the last used state after you exit and restart?

fumei
07-21-2010, 10:29 AM
Would not the issue be, if you have Doc2, that this is NOT (usually) a saved document, and therefore does not have a FullName? Or more accurately, they are the same. If a document has not been saved, then:

MsgBox ActiveDocument.FullName & _
vbCrLf & vbCrLf & ActiveDocument.Name

returns:

Document1
Document1

fumei
07-21-2010, 10:40 AM
"Anyone know how to make word remember the last used state after you exit and restart?"

The last used state of WHAT? Please explain. Remember, so far, this is only affecting the current ActiveDocument. So say you have FullName currently - C:\Whatever\Blah.doc. You close Word.

You open Word. You open a different file - C:\Blah\Whatever.doc.

Do you want FullName? Yes? No?

I am assuming that if you open C:\Whatever\Blah.doc. again, you DO want the last state. If this is correct, then as geekgirlau suggests, use a DOCVARIABLE.

Sub TitleBarDisplayName()
If ActiveWindow.Caption = ActiveDocument.Name Then
ActiveWindow.Caption = ActiveDocument.FullName
ActiveDocument.Variables("Full").Value = 1
Else
ActiveWindow.Caption = ActiveDocument.Name
ActiveDocument.Variables("Full").Value = 0
End If
End Sub


Now with your Document Open...
Sub Document_Open()
If ActiveDocument.Variables("Full").Value = 1 Then
ActiveWindow.Caption = ActiveDocument.FullName
Else
ActiveWindow.Caption = ActiveDocument.lName
End If
End Sub

bstephens
07-22-2010, 10:58 AM
Hi Fumei, I confusingly named that document "Doc2.doc" however it was previously saved, you are right about it not toggling for unsaved docs.

I apologize for my bad explanation of "state" (no real programming background). What I mean is that word will remember whether the title bar was in "Name" or "FullName" after exiting and restarting word and changing documents. Ideally, I would like this to be independent of the document.

Could I accomplish this by sticking the document variable in normal.dotm?

Thanks for your input.

fumei
07-22-2010, 12:06 PM
" I would like this to be independent of the document."

Ah.

The problem though is that you are dealing with values for ActiveWindow. So, even if you could start Word with its default document (or even start with with a given document) with the titlebar either Full or Not-Full, this state would NOT - repeat NOT - apply to any other document opened.

It is a window-state...not a document state. If you open another document, you have....a new window. So while you may have started Word setting Full or otherwise, that code will NOT apply to any new documents started in the current session.

So....

a) you can have the toggle in each document.
b) you can start Word with a state determination (using an INI file is one way).

Either way, any NEW document is not affected.

bstephens
08-25-2010, 04:01 PM
Sub TitleBarDisplayNameToggle()

Dim X As String
X = Right(ActiveWindow.Caption, 21)
Dim Y As String
Y = " [Compatibility Mode]"

On Error GoTo ErrHandler

If StrComp(X, Y, vbTextCompare) = 0 Then
If Replace(ActiveWindow.Caption, " [Compatibility Mode]", "") = ActiveDocument.Name Then
ActiveWindow.Caption = ActiveDocument.FullName
Else
ActiveWindow.Caption = ActiveDocument.Name
End If
Else:
If ActiveWindow.Caption = ActiveDocument.Name Then
ActiveWindow.Caption = ActiveDocument.FullName
Else
ActiveWindow.Caption = ActiveDocument.Name
End If
End If

ErrHandler:
If Err <> 0 Then
' Display an error message.
MsgBox "No document open"
'Clear the error.
Err.Clear
Resume Next
End If
End Sub

geekgirlau
08-30-2010, 10:07 PM
Let's talk about the Replace function.

Replace("This is a test geekgirlau", " geekgirlau", "")

This will give you a result of "This is a test" - replacing " geekgirlau" with a blank string.

Replace("This is a test", " geekgirlau", "")

This will ALSO give you a result of "This is a test" - " geekgirlau" does not exist in the string, so effectively nothing happens.

THEREFORE you don't need to test whether "compatibility mode" exists in your window caption - it doesn't matter. Replace will handle it if it appears.

I've also made a change to your error handler. Generally the accepted process is to go to the error handler ONLY when an error actually occurs, not every single time the procedure is run. You are also making an assumption that only a single type of error will occur.


Sub TitleBarDisplayNameToggle()
On Error GoTo ErrHandler

If Replace(ActiveWindow.Caption, " [Compatibility Mode]", "") = ActiveDocument.Name Then
ActiveWindow.Caption = ActiveDocument.FullName
Else
ActiveWindow.Caption = ActiveDocument.Name
End If

ExitHere:
Exit Sub

ErrHandler:
MsgBox Err.Description
Resume ExitHere
End Sub



So we are back to exactly the same code I posted in #9, but with error handling added!

bstephens
08-31-2010, 01:11 PM
Your code is working in both cases for me now.

I'm not sure why I couldn't get that exact same code to toggle before.

There are obviously many things I need to learn about VBA!

Thanks!
Brian

geekgirlau
08-31-2010, 03:17 PM
We all have to start somewhere! Generally the first challenge is just to get something working, which often means challenging your assumptions about the best way to do that. As you gain more experience, the fun part comes in trying to get the desired result with the least amount of code possible :tongue: