7
votes

Below is a working code snippet that opens a Microsoft Word document, and goes to a specific index from the Table of Contents. filePath is a filepath, and strTopic is a value that links to the Table of Contents in the Word document.

Set objWord = CreateObject("Word.Application")
objWord.Visible = True

Set docWord = objWord.Documents.Open(fileName:=strPath, ReadOnly:=True)

docWord.Bookmarks(strTopic).Range.Select

I need to bring the Word document to the foreground.

Is there a toFront() type "function" in VBA?

9

9 Answers

11
votes

You can achieve what you want using APIS. I am using two APIs SetForegroundWindow and FindWindow

Private Declare Function SetForegroundWindow Lib "user32" (ByVal hwnd As Long) _
As Long

Private Declare Function FindWindow Lib "user32" Alias _
"FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) _
As Long

Sub Sample()
    Dim objWord As Object, docWord As Object
    Dim strPath As String, FileName As String
    Dim hwnd As Long

    Set objWord = CreateObject("Word.Application")
    objWord.Visible = True

    '~~> Change this to the relevant Filename and path
    strPath = "C:\Users\Siddharth Rout\Desktop\Sample.docx"
    '~~> Put the acutal file name here without the extension
    FileName = "Sample"

    Set docWord = objWord.Documents.Open(FileName:=strPath, ReadOnly:=True)

    hwnd = FindWindow(vbNullString, FileName & " [Read-Only] - Microsoft Word")

    If hwnd > 0 Then
      SetForegroundWindow (hwnd)
    End If
End Sub

NOTE: If you are sure that there is no other Word Application open other than what you opened then you can use this as well :)

Private Declare Function SetForegroundWindow Lib "user32" (ByVal hwnd As Long) As Long

Private Declare Function FindWindow Lib "user32" Alias _
"FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long

Sub Sample()
    Dim objWord As Object, docWord As Object
    Dim strPath As String
    Dim hwnd As Long

    Set objWord = CreateObject("Word.Application")
    objWord.Visible = True

    '~~> Change this to the relevant Filename and path
    strPath = "C:\Users\Siddharth Rout\Desktop\Sample.docx"

    Set docWord = objWord.Documents.Open(FileName:=strPath, ReadOnly:=True)

    hwnd = FindWindow("OpusApp", vbNullString)
    If hwnd > 0 Then
      SetForegroundWindow (hwnd)
    End If
End Sub
4
votes

How about,

docWord.Activate

This should bring the file that has been "Set" for the docWord object to foreground.

EDIT: Tested this on Access, quiet unreliable on Excel. Using an API is the best way to go if there are multiple instances of the Word application running.

3
votes

Once you've opened a document (or added one) you can get a Hwnd to pass to the SetForegroundWindow API function from the ActiveWindow object (e.g. obWord.ActivieWindow.Hwnd). That way you don't need to search for the correct Word instance to bring to front.

1
votes

This seems to work every time. Word running or not, multiple docs open or not.

OpenAlready:

On Error GoTo 0
With wrdApp
    .Selection.Goto What:=1, Which:=2, Name:=PageNumber
    .Visible = True
    .Activate '<---seems to work well. regardless of number of Word docs open OR not open.
End With
Set wrdDoc = Nothing
Set wrdApp = Nothing
1
votes

i'm quite new here, and in doing a ~30 min research on this specific case, I think I could bring something to the table here...

I found in this link: http://www.vbaexpress.com/forum/showthread.php?27589-bringing-Word-in-fornt-of-Excel this line of code: Application.ActivateMicrosoftApp xlMicrosoftWord It will force Word in front of everything, BUT if there is something opened it will create a new document, so what I found is that, if you want to open something and then bring it to front you use this sintax:

[...]

Application.ActivateMicrosoftApp xlMicrosoftWord
Word.Documents.Open(MyDocument)

[...]

Once in vba debugging the code will always be in front, but when using the code with the excel vba userform it works great! I can see the word changing the words on the fly!!!!

0
votes

Can also use AppActivate "Microsoft Word"


visit Microsoft help :here

Using AppActivate needs the exact title of the window you want focused. E.g. for a word 2013 template opened as an "add", you would have to use AppActivate "Document1 - Word"

0
votes

I just use;

FileAndPath = "C:\temp\Mydocument.docx"
ThisWorkbook.FollowHyperlink (FileAndPath)

It even works if the file is already open.

0
votes

Since ".Activate" is comented various times but at least for me calling it from excel to bring to fornt a word doc. I have to do a work around minimizing and maximazing the window. This works well for me until now:

Set wdApp = CreateObject("Word.Application")
wdApp.Visible = True
wdApp.ScreenUpdating = True
Set wdDoc = wdApp.Documents.Open(ThisWorkbook.Path & "\" & stWordDocument) 'update your path
wdDoc.Activate
ActiveDocument.ActiveWindow _
.WindowState = wdWindowStateMinimize
ActiveDocument.ActiveWindow _
.WindowState = wdWindowStateMaximize
-2
votes

found!

ActiveDocument.Activate 'might not be necessary
ActiveDocument.Windows.Application.WindowState = wdWindowStateMaximize

works flawlessly. I already had an "activedocument" I was working on.

http://www.access-programmers.co.uk/forums/showthread.php?t=173871