Access Active Document when document is opening

M

Magnus Olsson

We have old Word templates that were originally written for Word 97.
For each new version, we've updated the templates. Now we'll go from
Word 2003 to Word 2007, and of course there are problems.

The template contains the following code:

Private Sub Document_Open()
On Error Resume Next
If ActiveDocument.Type = wdTypeDocument Then
pAutoOpen.MAIN
dbOpen.MAIN
End If
End Sub
The problem is that the ActiveDocument sometimes gives the error "This
command is not available Because no document is open"

Why? How do I solve it?
 
G

Graham Mayor

How are you using the 'template'?

The macro that you quote will run if you open a document created from the
template.

If the template itself is opened the activedocument type will never be
wdTypeDocument so it will not do anything - which no doubt was the
intention.

It is normal practice to create new documents from a template and for the
code in that template to run when you create a new document from it, it
would need to be

Private Sub Document_New()

The core part of the macro

Private Sub Document_Open()
On Error Resume Next
If ActiveDocument.Type = wdTypeDocument Then
MsgBox "Open" 'this part works
End If
End Sub

will work in Word 2007.


The error therefore seems to be attributable to pAutoOpen or dbOpen. What do
they do?

Note that Sub AutoOpen() is somewhat faster than Sub Document_Open()

thus

Option Explicit
Sub AutoOpen()
On Error Resume Next
If ActiveDocument.Type = wdTypeDocument Then
MsgBox "Open"
End If
End Sub


--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP


<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
 
M

Magnus Olsson

We have old Word templates that were originally written for Word 97.
For each new version, we've updated the templates. Now we'll go from
Word 2003 to Word 2007, and of course there are problems.

The template contains the following code:

Private Sub Document_Open()
    On Error Resume Next
    If ActiveDocument.Type = wdTypeDocument Then
        pAutoOpen.MAIN
        dbOpen.MAIN
    End If
End Sub
The problem is that the ActiveDocument sometimes gives the error "This
command is not available Because no document is open"

Why? How do I solve it?

The documents is open with script on the intranet:

<a href="javascript:eek:pendokument('P:\\01\\2-010-01.doc')">012-010-01</
a>
<SCRIPT language=javascript>
function opendokument(dokument){
var objAppl;;

try{
objAppl = GetObject("","Word.Application");
objAppl.Documents.open(dokument);
}
catch(exception){
objAppl = new ActiveXObject("Word.Application");
objAppl.Visible = true;
objAppl.Documents.open(dokument);
}
objAppl = null;
}
</script>
If I run it from my local computer or open the document via Windows, I
don't get the error
 

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