开发者

Access Active Document in VBA when document is opening on the intranet

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(ex开发者_如何转开发ception){
    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


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.


I've got the same problem, my dodgy solution for now is this

Private Sub Document_Open()
    Application.OnTime (Now + TimeValue("00:00:02")), "ProcessActiveDocument"
End Sub

Sub ProcessActiveDocument
    'ActiveDocument works here
End Sub


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜