excelvbams-word

Opening a Word doc using VBA within excel - Word remains minimised


I am writing a VBA routine in excel that creates an rtf file and then opens it in Word. The file opens quite happily (both from explorer and from within Excel). But from within Excel, it remains minimised. The Word icon in the task bar indicates that there is a file open. If I click the Word icon in the task bar, it shows up fine. But I don't want to have to click the icon, I just want it to appear in front of the Excel window.

The code is called from and ACtiveX button on a worksheet.

Windows 11 and Office 365.

I'm sure it is an easy thing I am missing (probably a brain) but hoping that someone might help!

The code to open it is as follows (due credit to Mr Excel!):

Public Sub OpenWordFile(FName As String)

Dim WordApp As Object
Dim strFile As String

'If Word is already opened
On Error Resume Next
Set WordApp = GetObject("Word.Application")
If WordApp Is Nothing Then
    'Word wasn´t running, so open Application
    Set WordApp = CreateObject("Word.Application")
End If
On Error GoTo 0

'Check for the existence of the file
If Len(Dir(FName)) > 0 Then
    WordApp.Documents.Open FName
    WordApp.Visible = True
Else
    MsgBox "Couldn´t open '" & FName & "'!", vbInformation, "Error loading Document"
End If

End Sub

Solution

  • Use the AppActivate method to bring the Word document in front of the Excel application.

    .....
    ....
    ..
        WordApp.Documents.Open FName
        WordApp.Visible = True
        AppActivate Dir(FName)
    ....
    ...