none
Controls appear on wrong page in Word 2010 RRS feed

  • Question

  • in a template, I use label controls as helpbuttons. The reason I use labels and not buttons is that the invisible-propertie doesn't exist for buttons and labels, so i have to color both forground and background to white (and reduce the size to a single point) when printing the document. Buttons always remain visible, because you can't switch off the border.

    So far so good, the labels function exactly as I want them to, and when printing, they are hidden on the printout.

    I just have one problem: if the cursor is still on another page when I press a helpbutton, then the helpbuttons of the page where the cursor is, are shown on the page where I pressed the button. As soon as I click my mouse within the text on the page where the button is, the buttons from the other page disappear again, so a solution would be to click the mouse in the text, directly under the button, but I can't find how to do this in VBA.

    Does anyone know how to find out what the location of a control or schape is, relative to the document, preferrably the paragraph number?

    Friday, May 29, 2015 8:45 AM

Answers

  • Thanks Cindy,

    I actually used the ActiveX label control. But seeing your comment, I'll be working with macrobuttons in the future.

    I found out that the displaying also goes wrong when I just open the help with the cursor on another page. When I select the anchor of the shape (parent to the control), the problem is solved.

    This is the code to hide all buttons while printing (the code resides in a class module)

     -----start code -----           

    Public Sub myApp_DocumentBeforePrint(ByVal doc As Document, Cancel As Boolean)
        Dim lb As Shape

        Cancel = True 'stop print

    ' hide all helpbuttons
     Dim locatop() As Long
     Dim localef() As Long

        vormen = Application.ActiveDocument.Shapes.Count
        ReDim locatop(vormen) As Long
        ReDim localef(vormen) As Long

        For x = 1 To vormen
            With ActiveDocument
                locatop(x) = .Shapes(x).OLEFormat.Object.top
                localef(x) = .Shapes(x).OLEFormat.Object.Left
                naam = .Shapes(x).Name
                    If (TypeName(.Shapes(x).OLEFormat.Object) = "Label") And Left(.Shapes(x).OLEFormat.Object.Name, 4) = "Help" Then
                       .Shapes(x).OLEFormat.Object.Caption = ""
                       .Shapes(x).OLEFormat.Object.BackColor = &HFFFFFF
                       .Shapes(x).OLEFormat.Object.Height = 0
                       .Shapes(x).OLEFormat.Object.Width = 0
                    End If
            End With
        Next x

    Application.ActiveDocument.PrintOut
    ' unhide all helpbuttons
        For x = 1 To vormen
            With ActiveDocument
                .Shapes(x).OLEFormat.Object.top = locatop(x)
                .Shapes(x).OLEFormat.Object.Left = localef(x)
                naam = .Name
                    If (TypeName(.Shapes(x).OLEFormat.Object) = "Label") And Left(.Shapes(x).OLEFormat.Object.Name, 4) = "Help" Then
                       .Shapes(x).OLEFormat.Object.Caption = "?"
                       .Shapes(x).OLEFormat.Object.BackColor = &HFF0000
                       .Shapes(x).OLEFormat.Object.Height = 24
                       .Shapes(x).OLEFormat.Object.Width = 24
                    End If
            End With
        Next x
    End Sub

    ----- end code -----

    the GetHelp funtion, executed when the button is clicked, does nothing else then getting the helptekst from an external document and show it on a userform (in a textbox). This form is opened as vbModeless so that it can remain open while the text of the document is being edited.

    I made a macro to make sure all names of the shapes and the labels are equal (

    ----- start code -----

    dim vorm as shape

    ....

    vorm.Name = vorm.OLEFormat.Object.Name

    ----- end code -----

    After this has been done, I can use the code below to select the anchor, making Word go to that page, and displaying only the buttons for that page rather then showing the buttons for the page on screen and the page, where the cursor is.

    ----- start code -----

    Private Sub HelpButton003_Click()
    GetHelp 3
    Application.ActiveDocument.Shapes("Helpbutton003").Anchor.Select
    End Sub

    ----- end code -----

    I hope this explains a bit what I was doing, at this moment al is working, so I will try the MacroButtons for another project, I think, but still: thank you for the suggestion, I'm sure it will work for me.

    Tuesday, June 2, 2015 9:23 AM

All replies

  • Hi Han

    We need a bit more context as it's not clear what kind of controls you're using? What command do you use to insert them? It would also help if you could provide minimal steps to reconstruct your problem or if you could share a sample document with code on a service such as OneDrive.

    Have you considered using MacroButton fields instead of controls? If you format them with a specific character style it should be sufficient to set the Hidden property of the Font to change they're visibility?


    Cindy Meister, VSTO/Word MVP, my blog

    Friday, May 29, 2015 5:17 PM
    Moderator
  • Hi Cindy

    In Word, I go to the design mode and then insert a label control (as I said in my first message). This label control is blue with a white question mark as caption.

    I use  these labels as helpbuttons to specific pieces of text (I'll call them helpbuttons hereafter). I don't use any command for this. I use copy/paste to create another button sometimes. 

    When I print the document, all helpbuttons are hidden to prevent them from being printed.

    To reproduce my problem I have to do the following:

    1 make sure my cursor is on another page then the page that is currently displayed (for instance by scrolling up or down, using the mouse wheel).

    2 print the document

    That's all. As soon as the print has been finished (or spooled), the helpbuttons of the page where the cursor was are displayed on the current page together with the helpbuttons of the current page. But as soon as I click with my mouse on the current page, the helpbuttons of the other page disappear again.

    So a work-around could be to have the macro to hide the helpbuttons click on the current page after all buttons have been made visible again.

    Hope to have given you enough extra information now. And to answer your second question: I did not try Macrobuttons yet, maybe it is a good suggestion, but I have to solve at least the folleowin questions:

    1 If I set the font to invisible, does the backgroundcolor also disappear?

    2 To place the buttons where I need them (mostly in the left margin of the document), I would have to use textboxes, is this more efficient then using labels?

    Monday, June 1, 2015 6:06 AM
  • <<In Word, I go to the design mode and then insert a label control (as I said in my first message). This label control is blue with a white question mark as caption. >>

    Please be more specific about the command. Is this the legacy ActiveX control?

    From your description, it appears these are formatted with text wrap, so they belong to the Shapes collection?

    Please share the VBA code that triggers when a button is clicked, as well as that which should hide the buttons and do the printing.

    Macrobutton fields: <<1 If I set the font to invisible, does the backgroundcolor also disappear?

    2 To place the buttons where I need them (mostly in the left margin of the document), I would have to use textboxes, is this more efficient then using labels?>>

    1. Insert an Inline graphic as the display then changing the font visibility should hide it completely

    2. I'd use a Frame, rather than a Textbox, but in either case it should be a more satisfactory approach. The ActiveX controls are actually designed for VBA UserForms. Using them on a document surface was more an after-thought and they are not optimized for that - as you're discovering. Display and printing issues are common.


    Cindy Meister, VSTO/Word MVP, my blog

    Monday, June 1, 2015 5:41 PM
    Moderator
  • Thanks Cindy,

    I actually used the ActiveX label control. But seeing your comment, I'll be working with macrobuttons in the future.

    I found out that the displaying also goes wrong when I just open the help with the cursor on another page. When I select the anchor of the shape (parent to the control), the problem is solved.

    This is the code to hide all buttons while printing (the code resides in a class module)

     -----start code -----           

    Public Sub myApp_DocumentBeforePrint(ByVal doc As Document, Cancel As Boolean)
        Dim lb As Shape

        Cancel = True 'stop print

    ' hide all helpbuttons
     Dim locatop() As Long
     Dim localef() As Long

        vormen = Application.ActiveDocument.Shapes.Count
        ReDim locatop(vormen) As Long
        ReDim localef(vormen) As Long

        For x = 1 To vormen
            With ActiveDocument
                locatop(x) = .Shapes(x).OLEFormat.Object.top
                localef(x) = .Shapes(x).OLEFormat.Object.Left
                naam = .Shapes(x).Name
                    If (TypeName(.Shapes(x).OLEFormat.Object) = "Label") And Left(.Shapes(x).OLEFormat.Object.Name, 4) = "Help" Then
                       .Shapes(x).OLEFormat.Object.Caption = ""
                       .Shapes(x).OLEFormat.Object.BackColor = &HFFFFFF
                       .Shapes(x).OLEFormat.Object.Height = 0
                       .Shapes(x).OLEFormat.Object.Width = 0
                    End If
            End With
        Next x

    Application.ActiveDocument.PrintOut
    ' unhide all helpbuttons
        For x = 1 To vormen
            With ActiveDocument
                .Shapes(x).OLEFormat.Object.top = locatop(x)
                .Shapes(x).OLEFormat.Object.Left = localef(x)
                naam = .Name
                    If (TypeName(.Shapes(x).OLEFormat.Object) = "Label") And Left(.Shapes(x).OLEFormat.Object.Name, 4) = "Help" Then
                       .Shapes(x).OLEFormat.Object.Caption = "?"
                       .Shapes(x).OLEFormat.Object.BackColor = &HFF0000
                       .Shapes(x).OLEFormat.Object.Height = 24
                       .Shapes(x).OLEFormat.Object.Width = 24
                    End If
            End With
        Next x
    End Sub

    ----- end code -----

    the GetHelp funtion, executed when the button is clicked, does nothing else then getting the helptekst from an external document and show it on a userform (in a textbox). This form is opened as vbModeless so that it can remain open while the text of the document is being edited.

    I made a macro to make sure all names of the shapes and the labels are equal (

    ----- start code -----

    dim vorm as shape

    ....

    vorm.Name = vorm.OLEFormat.Object.Name

    ----- end code -----

    After this has been done, I can use the code below to select the anchor, making Word go to that page, and displaying only the buttons for that page rather then showing the buttons for the page on screen and the page, where the cursor is.

    ----- start code -----

    Private Sub HelpButton003_Click()
    GetHelp 3
    Application.ActiveDocument.Shapes("Helpbutton003").Anchor.Select
    End Sub

    ----- end code -----

    I hope this explains a bit what I was doing, at this moment al is working, so I will try the MacroButtons for another project, I think, but still: thank you for the suggestion, I'm sure it will work for me.

    Tuesday, June 2, 2015 9:23 AM
  • Hi Han

    Thanks for the thorough explanation. Glad you found a way to get it to work and for sharing it with us :-)


    Cindy Meister, VSTO/Word MVP, my blog

    Tuesday, June 2, 2015 1:48 PM
    Moderator