excel - Open Word Document and Bring to Front -
below (working) code snippet opens microsoft word document, , goes specific index table of contents. filepath
filepath, , strtopic
value links toc in 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, need bring word document foreground.
is possible vba firstly? there tofront()
type "function" can use? afaik c# has app.activewindow.activate();
cannot similar working in vba.
you can achieve want using apis. using 2 apis setforegroundwindow , findwindow
private declare function setforegroundwindow lib "user32" (byval hwnd long) _ long private declare function findwindow lib "user32" alias _ "findwindowa" (byval lpclassname string, byval lpwindowname string) _ long sub sample() dim objword object, docword object dim strpath string, filename string dim hwnd long set objword = createobject("word.application") objword.visible = true '~~> change relevant filename , path strpath = "c:\users\siddharth rout\desktop\sample.docx" '~~> put acutal file name here without extension filename = "sample" set docword = objword.documents.open(filename:=strpath, readonly:=true) hwnd = findwindow(vbnullstring, filename & " [read-only] - microsoft word") if hwnd > 0 setforegroundwindow (hwnd) end if end sub
note: if sure there no other word application open other opened can use :)
private declare function setforegroundwindow lib "user32" (byval hwnd long) long private declare function findwindow lib "user32" alias _ "findwindowa" (byval lpclassname string, byval lpwindowname string) long sub sample() dim objword object, docword object dim strpath string dim hwnd long set objword = createobject("word.application") objword.visible = true '~~> change relevant filename , 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 setforegroundwindow (hwnd) end if end sub
Comments
Post a Comment