vba 打开 Word 文档并置于最前面
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 
原文地址: http://stackoverflow.com/questions/25362563/
Warning: these are provided under cc-by-sa 4.0 license.  You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Open Word Document and Bring to Front
提问by RossC
Below is a working code snippet that opens a Microsoft Word document, and goes to a specific index from the Table of Contents. filePathis a filepath, and strTopicis a value that links to the Table of Contents in the Word document. 
下面是一个工作代码片段,它打开一个 Microsoft Word 文档,并从目录中转到特定索引。filePath是一个文件路径,strTopic是一个链接到 Word 文档中目录的值。
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.
我需要将 Word 文档置于前台。
Is there a toFront()type "function" in VBA? 
toFront()VBA 中是否有类型“函数”?
回答by Siddharth Rout
You can achieve what you want using APIS. I am using two APIs SetForegroundWindowand FindWindow
您可以使用 APIS 实现您想要的。我正在使用两个 API SetForegroundWindow和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 :)
注意:如果您确定除了您打开的内容之外没有其他 Word 应用程序打开,那么您也可以使用它:)
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
回答by PaulFrancis
How about,
怎么样,
docWord.Activate
This should bring the file that has been "Set" for the docWord object to foreground.
这应该将已为 docWord 对象“设置”的文件带到前台。
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.
编辑:在 Access 上对此进行了测试,在 Excel 上安静不可靠。如果有多个 Word 应用程序正在运行,则使用 API 是最好的方法。
回答by John0987
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.
一旦您打开了一个文档(或添加了一个),您就可以从 ActiveWindow 对象(例如 obWord.ActivieWindow.Hwnd)获得一个 Hwnd 传递给 SetForegroundWindow API 函数。这样您就不需要搜索正确的 Word 实例以显示在前面。
回答by Dinesh Niduvani Somanna
Can also use AppActivate "Microsoft Word"
也可以使用 AppActivate "Microsoft Word"
visit Microsoft help :here
访问 Microsoft 帮助:此处
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"
使用 AppActivate 需要您想要聚焦的窗口的确切标题。例如,对于作为“添加”打开的 word 2013 模板,您必须使用 AppActivate“Document1 - Word”
回答by OGERWOLF
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
http://www.access-programmers.co.uk/forums/showthread.php?t=173871
回答by RealEstateMan
This seems to work every time. Word running or not, multiple docs open or not.
这似乎每次都有效。Word 运行与否,多个文档是否打开。
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
回答by Alex Silva
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...
我在这里很新,在对这个特定案例进行约 30 分钟的研究时,我想我可以在这里带来一些东西......
I found in this link: http://www.vbaexpress.com/forum/showthread.php?27589-bringing-Word-in-fornt-of-Excelthis 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:
我在这个链接中找到了:http://www.vbaexpress.com/forum/showthread.php?27589-bringing-Word-in-fornt-of-Excel 这行代码: Application.ActivateMicrosoftApp xlMicrosoftWord 它会强制把 Word 放在前面一切,但如果有东西打开它会创建一个新的文件,所以我发现,如果你想打开一些东西,然后把它放在前面,你可以使用这个语法:
[...]
[...]
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!!!!
一旦在 vba 调试中,代码将始终在前面,但是当将代码与 excel vba 用户表单一起使用时,效果很好!我可以看到单词在飞行中改变单词!!!!
回答by Nibby
I just use;
我只是使用;
FileAndPath = "C:\temp\Mydocument.docx"
ThisWorkbook.FollowHyperlink (FileAndPath)
It even works if the file is already open.
如果文件已经打开,它甚至可以工作。

