views:

170

answers:

3

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 2010, 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
        ' Update the document from database'
    End If
End Sub

The problem is that the ActiveDocument gives the error

This command is not available Because no document is open

The documents is open with script on the intranet:

<a href="javascript:opendokument('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 the script from my local computer or open the document via Windows, I don't get the error

+1  A: 

I haven't worked with the Word event model in Word 2010, but there are few a things I would look at.

First, see if there are additional events that you may be able to hook into. In Word 2000, I only see New, Open, and Close. Perhaps in Word 2010, there are other events such as Loaded? If so, you might try placing the code in one of those events where the document is sure to already be loaded.

Otherwise, you might write some code that "waits" until ActiveDocument is set to an instance of object. You might try something like this:

Private Sub Document_Open()

  Do While ActiveDocument Is Nothing
    DoEvents
  Loop

  If ActiveDocument.Type = wdTypeDocument Then
    Debug.Print "Now the document is open"
  End If

End Sub

The DoEvents within the loop should allow the document to load and the While condition will eventually catch that ActiveDocument is not Nothing and will allow the program to proceed. Granted, this assumes that document will in fact become open, but it's something worth trying. To get an idea of how this code might work, look at the following code:

Private Sub Document_Open()

Dim dtmLater As Date
Dim doc As Document

dtmLater = DateAdd("s", 5, Now())

  Do While doc Is Nothing
    DoEvents
    If Now() >= dtmLater Then
      Set doc = ActiveDocument
    End If
  Loop

  If ActiveDocument.Type = wdTypeDocument Then
    Debug.Print "Now the document is open"
  End If

End Sub

The above code arbitrarily pauses for 5 seconds so you can see how the code loops until the doc object is set. Once the object is instantiated, the code can move forward.

Ben McCormack
"Do While ActiveDocument Is Nothing" are giving the same error as before.
magol
And if I try the secound code example, I get the same error, even if I set the timeout to a high number
magol
@magol where is it throwing the error? Can you pin it down to an individual line of code? You might consier removing `On Error Resume Next` to better find the line of code that's throwing the error.
Ben McCormack
@magol Admittedly, the second example might be a little confusing because `doc` isn't related to `ActiveDocument`. Try exploring `ActiveDocument.Type` in code and making sure you are working with the right objects. For example, try doing `Debug.Print ActiveDocument.Name` to see if the `ActiveDocument` object is at least "working."
Ben McCormack
@Ben McCormack When I debug I replace On error resume next with on error goto 0.The exact exception I get is: "Run-time error '4248':This command is not available because no document is open."I get the exception on the line Set doc = ActiveDocumentAnd if I try Debug.Print ActiveDocument.Name, I also get the exception.
magol
@magol I wish I had Word 2010 available so I could look at it. I don't know what else to tell you other than to keep checking the `ActiveDocument` object at different places in your code. For example, remove your current VBA code and just add a simple method that has `Debug.Print ActiveDocument.Name` and see if you can access that object *anywhere* in your code. If that doesn't work, try it with a brand new document. Keep trying different combinations until you can isolate why it's causing a problem in your particular document. Without knowing the exact solution, that's the best I've got.
Ben McCormack
The error is only if I start the document from a script on the intranet. Se my changes in the question
magol
@magol it sounds like Word is providing a security block because the document is being opened on the web. You might try changing your Macro security to see if that changes things, but I'm not sure I would want to do that in a production environment. Combining the phrases *VBA Macro* and *from the internet* always sounds like a dangerous recipe.
Ben McCormack
@Ben McCormack I will try tomorrow. But the website is on an intranet that is not possible to reach beyond the company network. And the Word documents is an internal file server.
magol
A: 

I did add the intranet server to the Local intranet zone in Internet Explorer. And then I set "Initialize and script ActiveX controls not marked as safe for scripting" to Enabled.

magol
A: 

@Magol

Sorry for create an answer (I don't have rigths for comments yet), but your solution don't works for me.

My problem is similar to your problem. If possible, see: http://stackoverflow.com/questions/3506510/iis-ie8-word-2007-doc-with-macro

Thanks for your attention

William John Adam Trindade