سؤال

Using Windows XP, Office 2007 and the Microsoft Custom UI Editor for building add-ins.

I'm creating an add-in which simply opens some pre-defined word .dotx templates. But I've ran into an od problem.

The new word document simply won't activate. I think it's because each user either use two or three monitors, but I'm not sure how to handle such a setup in VBA.

Also, it's only sometimes the new document won't activate. Whenever a userform is present in the code it seems to bug.

With activate I mean the user is presented with the "old" document, and the new opened document doesn't get the focus.

Anyways, here's the code that won't get i to work:

Private Sub btnOK_Click()

If Me.chbxFigures = False Then
    If Me.obtnDanish Then
        Set newDoc = Documents.Add(Template:="path", NewTemplate:=False, DocumentType:=0, Visible:=True)
    ElseIf Me.obtnEnglish Then
        Set newDoc = Documents.Add(Template:="path", NewTemplate:=False, DocumentType:=0, Visible:=True)
    End If
ElseIf Me.chbxFigures Then
    If Me.obtnDanish Then
        Set newDoc = Documents.Add(Template:="path", NewTemplate:=False, DocumentType:=0, Visible:=True)
    ElseIf Me.obtnEnglish Then
        Set newDoc = Documents.Add(Template:="path", NewTemplate:=False, DocumentType:=0, Visible:=True)
    End If
End If

'Documents.Add.ActiveWindow.Panes(2).Activate
newDoc.ActiveWindow.Visible = True
newDoc.ActiveWindow.Activate
newDoc.Activate
Set newDoc = Nothing

Unload Me

End Sub

And this code seems to work fine:

Sub LoadSC(control As IRibbonControl)
    Set newDoc = Documents.Add(Template:="path", NewTemplate:=False, DocumentType:=0, Visible:=True)
    newDoc.Activate
    newDoc.ActiveWindow.Visible = True
    newDoc.ActiveWindow.Activate
    newDoc.ActiveWindow.ActivePane.Activate
    Set newDoc = Nothing
End Sub

Google hasn't come up with an answer in two days, so I hope some of you might lead me in a helpfull direction.

هل كانت مفيدة؟

المحلول

As per comment and confirmation, moving the Unload Me above the line newDoc.ActiveWindow.Visible = True will solve this issue - possibly the Form is stopping the launch of new document.

In VBA, codes are still executed until End Sub/Function or Exit Sub/Function.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top