How do I use the Word API to programmatically open a document using a bookmark at the top of the page?
Using the following code to open a Word document in a bookmark
Object readOnly = true;
Object isVisible = true;
Object missing = System.Reflection.Missing.Value;
Microsoft.Office.Interop.Word.Application wordApp = new Microsoft.Office.Interop.Word.Application();
Microsoft.Office.Interop.Word.Document wordDoc = new Microsoft.Office.Interop.Word.Document();
wordApp.Visible = true;
wordDoc = wordApp.Documents.Open(ref fileName, ref missing, ref readOnly, ref missing, ref missing,
ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref isVisible,
ref missing, ref missing, ref missing, ref missing);
object bookmarkName = BookMarkName;
if (wordDoc.Bookmarks.Exists(bookmarkName.ToString()))
{
Microsoft.Office.Interop.Word.Bookmark bookmark = wordDoc.Bookmarks.get_Item(ref bookmarkName);
bookmark.Select();
}
The Word document shows the bookmark, but not at the top of the page. Is there a way to show the bookmark at the top of the page as shown with Goto-> Bookmark?
Code suggested in StackOverflow question
How do I use the Microsoft Word API and Bookmarks to programmatically open a Word document at a specific location?
which the
Object item = Microsoft.Office.Interop.Word.WdGoToItem.wdGoToBookmark;
Object whichitem = Microsoft.Office.Interop.Word.WdGoToDirection.wdGoToFirst;
Object count = 1;
Object name = BookMarkName;
wordDoc.GoTo(ref item, ref missing, ref missing, ref name);
does not work and gives all the errors described in the post.
source to share
GotoBookmark in MS Word
In pascal, execute this code:
procedure GotoBookMark(name:string); var d1,d2,d7,d8:olevariant; begin d1:=name; d2:= wdGoToBookmark; d7:=unAssigned; d8:=unAssigned; try WA.selection.GoTo_(d2, d7,d8,d1); except end; end;
Where is the name
name of the bookmark,
wa
Microsoft.Office.Interop.Word.Application wa= new Microsoft.Office.Interop.Word.Application();
open MS Word document
procedure OpenWordDoc(Name:string); var FileName,Visible,newtemplate,documenttype:olevariant; begin FileName:=Name; newtemplate:=false; documenttype:=0; visible:=true; WA.Documents.Add( FileName,newtemplate,documenttype,visible); end;
source to share
This is tricky as Word VBA is not really screen oriented. The following code works on my screen, but you will need to check to make sure it works in other configurations. You may need to adjust the "fantasy factor" (letter 40). Either way, it should get the bookmark at the top of the screen, inside a line or so.
Window.GetPoint method returns position in pixels Range is a Windows API method that was added in VBA, so you need to pass in variables that are then populated with values.
The height of the window, other than the used height, plus the "fancy factor" gives you the position at the points of the visible top of the document. The window is scrolled in turn until the position returned by GetPoint is no longer greater than that position.
Sub ScrollBookmarkTopOfPage()
Dim rng As word.Range
Dim pxLeft As Long, pxTop As Long, pxWidth As Long, pxHeight As Long
Dim winUseHeight As Long, winHeight As Long, pos As Long
Set rng = ActiveDocument.Bookmarks("test").Range
ActiveWindow.GetPoint pxLeft, pxTop, pxWidth, pxHeight, rng
'Debug.Print pxLeft, pxTop, pxWidth, pxHeight
winUseHeight = ActiveWindow.UsableHeight
winHeight = ActiveWindow.height
pos = winHeight - winUseHeight + 40
Do While PixelsToPoints(pxTop) > pos
ActiveWindow.SmallScroll
ActiveWindow.GetPoint pxLeft, pxTop, pxWidth, pxHeight, rng
Loop
rng.Select
End Sub
source to share