Solved Go To Page from Word Ribbon

Microsoft Word 2013 32/64-bit
January 24, 2019 at 09:32:45
Specs: Windows 7 x64, 2,4 GHz / 4 GB
I am currently building a custom ribbon to use in Word when working because the QAT icons aren't enough anymore.

I want to add an editBox to that ribbon in which to write the number of the desired page and press enter, then word would go to that page.

Doing this in VBA alone is simple enough - 1 line of code I can find out with macro recorder.

Upon adding it to the ribbon and to XML, the same VBA line of code jumps 1 page forward each time, no matter what page number I insert into the editBox.

Here's the VBA code I am using in my CustomRibbon.dotm file:

Option Explicit
Public myRibbon As IRibbonUI
Public PagNum As String

Public Sub rbnOnLoad(control As IRibbonUI)
    Set myRibbon = control
End Sub

Public Sub onGetText(control As IRibbonControl, ByRef text)
    text = PagNum
End Sub

Public Sub onChange(control As IRibbonControl, text As String)
    myRibbon.InvalidateControl ("PagNumText")
    Selection.GoTo What:=wdGoToPage, Name:=PagNum
End Sub

Here's the XML code I am using in my CustomRibbon.dotm file:

<customUI xmlns="http://schemas.microsoft.com/office/2009/07/customui" onLoad="rbnOnLoad">
<ribbon><tabs>
<tab id="customTab" 
     label="CUSTOM" 
     insertBeforeMso="TabHome">

<group id="customGroup1" label="PAGE">

<editBox id="PagNumText" 
         label="Page" 
         onChange="onChange" 
         sizeString="XXX" />

</group></tab></tabs></ribbon></customUI>

Assuming I have a 4 page word file. Entering 3 goes to page 2, then entering 1 goes to page 3 and so on.

Can anyone tell me what I am doing wrong?

message edited by Mrrrr


See More: Go To Page from Word Ribbon

Report •

#1
January 26, 2019 at 00:57:41
✔ Best Answer
Finally figured it out.

Small change added to XML code, getText callback was actually needed - above I had the VBA but no callback in XML, also changed onChange callback to be in accordance with the modified VBA code:

<editBox id="PagNumText" 
         label="Page"
	 getText="GetText"
         onChange="EditBoxOnChange" 
         sizeString="XXX" />

As for VBA, declaration and rbnOnLoad remained the same; modified sub names onGetText to just GetText and onChange to EditBoxOnChange - won't work with old names no matter what I do; I added a condition to warn user if number is larger than number of pages or if a non-numeric value is inserted in the EditBox:

Public Sub GetText(control As IRibbonControl, ByRef Text)
    PagNum = Text
End Sub

Public Sub EditBoxOnChange(control As IRibbonControl, Text As String)
Dim AllPages As Integer
    AllPages = ActiveDocument.Range.Information(wdNumberOfPagesInDocument)
    
    PagNum = Text
    myRibbon.InvalidateControl ("PagNumText")
        
If IsNumeric(PagNum) = True Then
    If PagNum <= AllPages Then
       Selection.GoTo What:=wdGoToPage, Name:=PagNum
    Else
        MsgBox "This document doesn't have " & PagNum & " pages," & vbCrLf & _
                "it has " & AllPages & " pages", vbOKOnly
    End If
Else
MsgBox "Please insert a number"
End If
End Sub

Note:
if it shows error "Object variable or with block not set" on line

    myRibbon.InvalidateControl ("PagNumText")
just save document, close and reopen. I read somewhere that for some reason Word loses callback or something like that. It happens when I modify the VBA code sometimes.

Now I have what I needed: an empty field on the ribbon where I type page number and press enter and word jumps to that page.

Thanks to anyone who tried this.

I consider this solved.

message edited by Mrrrr


Report •
Related Solutions


Ask Question