Open a Word document and bring to the front

Below is a snippet of code (working) that opens a Microsoft Word document and jumps to a specific index from the Table of Contents. filePath is just the file path, and strTopic is the value that refers to ToC in the Word Doc.

 Set objWord = CreateObject("Word.Application") objWord.Visible = True Set docWord = objWord.Documents.Open(fileName:=strPath, ReadOnly:=True) docWord.Bookmarks(strTopic).Range.Select 

However, I need to bring the Word document to the forefront.

Is this possible with VBA? Is there a type toFront() function that I can use? AFAIK C # has something like app.ActiveWindow.Activate(); but i cant get anything like that in .

+6
source share
6 answers

You can achieve what you want using APIS. I use 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 another Word application is not open, besides what you opened, you can also use this :)

 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 
+10
source

What about,

 docWord.Activate 

This should cause the Install file to be installed in the foreground for the docWord object.

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

+5
source

You can also use AppActivate "Microsoft Word"


visit Microsoft help: here

Using AppActivate requires the exact name of the window you want to focus on. For instance. for a 2013 word template open as add, you will need to use AppActivate "Document1 - Word"

+1
source

Once you have opened the document (or added it), you can get Hwnd to pass the SetForegroundWindow API function from the ActiveWindow object (for example, obWord.ActivieWindow.Hwnd). This way, you don’t have to look for the right instance of Word to bring it to the forefront.

+1
source

found!

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

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

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

0
source

It seems to work every time. Word works or not, several documents open.

 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 
0
source

Source: https://habr.com/ru/post/973972/


All Articles