Avoid "Save" dialog after running AutoOpen macro

L

LeonidG

I wrote AutoOpen macro that shows or hides the Document Map depending of a
document properties. The macro works properly, but now, when I close any
document, I get a prompt "Do you want to save the changes..." even if I have
changed nothing in the document.

I tried to put "ActiveDocument.Saved = True" as the last command of the
AutoOpen macro, but it doesn't help.

So far I've found a workaround: I put "ActiveDocument.Save" as the last
command of the AutoOpen macro. However, the workaround causes updating a
document's modified time every time when the document is open.

Can anyone suggest a better solution?
 
R

Rinze Smit

Hi LeonidG
Looks like your code changes something else to. As far as I know,
showing the Document Map is not a change to the document.
Could there be anyting else happening in the code that triggers the
change, or do you have fields in the documents that are updated on
opening?

Greetings,
Rinze Smit
Revalidatie Friesland
 
L

LeonidG

Hi, Rinze

Here's the code of my macro:

Sub AutoOpen()
On Error GoTo ErrHandler
If ActiveDocument.BuiltInDocumentProperties("Keywords").Value =
"SHOWMAP" Then
ActiveWindow.DocumentMap = True
ActiveWindow.View.ShowHeading 1
Else
ActiveWindow.ActivePane.Close
End If
ActiveDocument.Save
GoTo EndOfMacro

ErrHandler:
If Err.Number = 5867 Then
Err.Clear
Else
MsgBox Err.Description & " " & Err.Number
End If
EndOfMacro:
End Sub

I've made some tests and found that my dealing with the document properties
triggers the change. So I see two alternative ways:
- find another way to recognize a "mapped" document;
- clean the flag of changes after it has been set.

I've forgot to mention, my version of the Word is 2007
 
D

Doug Robbins - Word MVP

Another way to recognize a "mapped" document would be to set a document
variable in the document to True using

ActiveDocument.Variables("Mapped") = "ShowMap"

Then for your autoopen macro use:

'Sub AutoOpen()
On Error GoTo ErrHandler
If ActiveDocument.Variables("Mapped").Value = "ShowMap" Then
ActiveWindow.DocumentMap = True
ActiveWindow.View.ShowHeading 1
End If
ActiveDocument.Save
GoTo EndOfMacro

ErrHandler:
If Err.Number = 5825 Then
ActiveDocument.Close
GoTo EndOfMacro
Else
MsgBox Err.Description & " " & Err.Number
End If
EndOfMacro:
End Sub




--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP, originally posted via msnews.microsoft.com
 
L

LeonidG

Doug, many thanks. With a document variable it works much better. Here's my
final code:

Sub AutoOpen()
On Error GoTo ErrHandler
ActiveWindow.DocumentMap = True
If ActiveDocument.Variables("Mapped").Value = "ShowMap" Then
ActiveWindow.View.ShowHeading 1
End If
GoTo EndOfMacro

ErrHandler:
If Err.Number = 5825 Then
Err.Clear
ActiveWindow.ActivePane.Close
Else
MsgBox Err.Description & " " & Err.Number
End If
EndOfMacro:
End Sub
 

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