• WORD (Office 365 version) Record macro to hide specific (not all) images

    Home » Forums » AskWoody support » Productivity software by function » MS Word and word processing help » WORD (Office 365 version) Record macro to hide specific (not all) images

    Author
    Topic
    #508415

    Hi all,

    Have attached skeleton of template which has headers/footers, section breaks, styles, etc.

    Some users will not want to print or email this document WITH the images on pages 2, 6 and 8. Ideally I would like to record a macro, then put a button on the ribbon so that when they create new document based on this template then click my button; those 3 images will be either deleted or hidden … don’t care which as long as they disappear from the specific document.

    I have done some research, but no suggestions have worked. EG:

    Attempt No. 1 – I have created a style called ‘pic’ with a view to hiding all instances of that style, but when i select the pic, my ‘styles’ ribbon buttons are ghosted and i can apply a style to any pics. Further research seems to refer to styles for pictures, as those built in ones available in the Format Pictures ribbon. But I cant work out how to search and replace those?

    Attempt No. 2 – I can bookmark each pic (bg1, bg2 and bg3) and then record a ‘go to’ graphic, and can cycle through to each bookmark, but i cant get “select” to record in the macro and don’t know what the code is to type it in, then to move on to the next graphic? So the result of the macro is that i just end up on picture no. 3 on page 8 but they’re all still in the document. So the macro has just moved from one to the next.

    Attempt No. 3 – I have tried doing a ‘selecting all, graphics’ but then it includes the org chart on page 5 – which i don’t want hidden! I can see that if I ‘go to next graphic’ i can move to the graphic, but then in the Selection Pane i have to manually click on the picture # that i want to hide…. again i can’t record that step in my macro?

    I have also tried the technique of changing the Wrapping to ‘in line with text’ then selecting the paragraph mark to which my anchor is locked and formatting that as hidden. But it throws the picture way off the page because as you can see, the picture is page size and sits behind the text (Note, none of the ones I want hidden are set as background, nor are they in headers.

    Can anyone help me please?
    PS. Site wouldn’t let me upload as a **.dotm so have saved as doc for now and had to compress all pics to reduce for max upload limits.

    Webmistress!

    Viewing 3 reply threads
    Author
    Replies
    • #1593562

      Try the following macro, which toggles the visibility of your images. Running it once hides them; running it again unhides them.

      Code:
      Sub Demo()
      Application.ScreenUpdating = False
      Dim Shp As Shape
      For Each Shp In ActiveDocument.Shapes
        With Shp.Anchor.Characters.First.Font
          .Hidden = Not .Hidden
        End With
      Next
      Application.ScreenUpdating = True
      End Sub

      Cheers,
      Paul Edstein
      [Fmr MS MVP - Word]

      • #1593832

        Thank you Paul, but no success thus far.

        I’ve simply copy/pasted your code above, saved then clicked on Macro, [select demo] and Run, but nothing is happening??

        Webmistress!

    • #1593833

      Whether you’ll see any difference on screen depends on whether you have Word’s hidden text display ‘on’. Clicking on the ¶ symbol on the Ribbon’s Home tab toggles Word’s hidden text display ‘on/off’.

      Here’s a more refined version of the macro, which works better if you have Word’s hidden text display ‘off’ and you want to unhide the images:

      Code:
      Sub Demo()
      Application.ScreenUpdating = False
      Dim Shp As Shape, bView As Boolean
      bView = ActiveWindow.ActivePane.View.ShowAll
      ActiveWindow.ActivePane.View.ShowAll = True
      For Each Shp In ActiveDocument.Shapes
        With Shp.Anchor.Characters.First.Font
          .Hidden = Not .Hidden
        End With
      Next
      ActiveWindow.ActivePane.View.ShowAll = bView
      Application.ScreenUpdating = True
      End Sub

      Note: You can get some interesting effects if you hide some images, then add more (visible) images to the document. Running the macro will then hide one set while displaying the other…

      Cheers,
      Paul Edstein
      [Fmr MS MVP - Word]

      • #1593943

        Thanks again Paul,

        I can see how turning on/off the Paragraph Marker does hide the graphics, but there are a couple of problems associated with that:

          [*]I cant rely on all users (approximately 100) knowing whether to have their show/hide is off, much less that they all even know what that is. The Macro really needs to be independent of paragraph settings if possible.
          [*]I only want to hide pictures that are inserted ‘Behind Text’. I don’t want to hide pictures in the header or ‘graphics’ because that also hides my ‘SmartArt’ org chart – which i DON’T want.
          [*]After i run the macro, because it’s obviously hiding certain paragraphs, it completely throws out the pagination and section formatting as well. Eg. the newspaper columns in the T & C’s section, are on the next page from the heading

        I’m afraid I don’t know VBA well so I can’t code this myself.

        I was hoping just hiding a particular STYLE would do it, but that hasn’t worked either.

        Any other suggestions or help you can offer is very welcome 🙁

        Thanks again.

        Whether you’ll see any difference on screen depends on whether you have Word’s hidden text display ‘on’. Clicking on the ¶ symbol on the Ribbon’s Home tab toggles Word’s hidden text display ‘on/off’.

        Here’s a more refined version of the macro, which works better if you have Word’s hidden text display ‘off’ and you want to unhide the images:

        Code:
        Sub Demo()
        Application.ScreenUpdating = False
        Dim Shp As Shape, bView As Boolean
        bView = ActiveWindow.ActivePane.View.ShowAll
        ActiveWindow.ActivePane.View.ShowAll = True
        For Each Shp In ActiveDocument.Shapes
          With Shp.Anchor.Characters.First.Font
            .Hidden = Not .Hidden
          End With
        Next
        ActiveWindow.ActivePane.View.ShowAll = bView
        Application.ScreenUpdating = True
        End Sub

        Note: You can get some interesting effects if you hide some images, then add more (visible) images to the document. Running the macro will then hide one set while displaying the other…

        Webmistress!

    • #1593948

      OK, try this alternative approach. It should hide/unhide the shapes without the need for toggling the font visibility and shouldn’t affect the layout.

      Code:
      Sub Demo()
      Application.ScreenUpdating = False
      Dim Shp As Shape
      For Each Shp In ActiveDocument.Shapes
        Shp.Visible = Not Shp.Visible
      Next
      Application.ScreenUpdating = True
      End Sub

      Cheers,
      Paul Edstein
      [Fmr MS MVP - Word]

      • #1594067

        OK, try this alternative approach. It should hide/unhide the shapes without the need for toggling the font visibility and shouldn’t affect the layout.

        I was intrigued by this, and wondered if it would be possible to somehow identify the images, so only the desired ones hide. It seems that determining the picture’s name from within the normal Word editor is impossible. I then tried adding a hyperlink to one image, which shows up as an attribute in code, but if an image doesn’t have a hyperlink an error is generated when trying to check it. I then ‘discovered’ that if you add alternative text to an image (Format>Alt Text) by inserting something unique under ‘description’, then this can be read in the code. So in the example below, pictures with Alt-text of ‘hide’ will toggle, leaving others untouched. This only works for floating images, in-line items don’t seem to show up in the list of shapes – which is why your org chart stays visible, I think.

        Code:
        Sub Demo()
        Application.ScreenUpdating = False
        Dim Shp As Shape
        For Each Shp In ActiveDocument.Shapes
          If Shp.AlternativeText = “hide” Then
            Shp.Visible = Not Shp.Visible
          End If
        Next
        Application.ScreenUpdating = True
        End Sub

        Even if this isn’t any help to the OP, I may find it useful one day.

        • #1594072

          There are numerous approaches that could be taken. For example, in webmistress’ document, all the images were positioned behind the text. If there had been other images that weren’t behind the text and were to remain visible, one could test that property. Another approach might be to use the relative location of the shape in the document (e.g. Shapes 1 & 3). Yet another approach is to name the images, then toggle the visibility by reference to the name. And, of course, there’s the alternative text method you’ve employed.

          As for inline shapes, they’re InlineShape objects – not Shape objects – and would have to be handled as such. Since the images to be processed were all Shape objects, there was no need to include any code regarding InlineShape objects.

          Cheers,
          Paul Edstein
          [Fmr MS MVP - Word]

          • #1594079

            Yet another approach is to name the images, then toggle the visibility by reference to the name.

            I experimented with that, but don’t know how to name the images from the screen editor, I can only think of stepping through code and changing the name (if it can be changed) within the VBA interface, which seems tiresome – along with the fun of guessing (?) which image the code is looking at – is there a better way?

        • #1594309

          Thank you so much mngerhold !! IT WORKED !! woo hoo.
          Perfect for me as all the images i want to show hide ARE floating, not in-line. Thank you again. 😉

          Webmistress!

          • #1594311

            Glad to hear it (I wondered yesterday if you had had a chance to try it). You can help me catch up with Paul’s (Macropod) ‘thanks’ score by clicking on the ‘thanks’ button on bottom left of the post (but should also thank him, as I only tweaked his code – still, that should improve my percentage by more than his !).

            atb, Martin

    • #1594098

      You could use a macro like the following to both retrieve a shape’s current name and to rename it:

      Code:
      Sub ShapeReNamer()
      Dim Result As String
      With Selection
        If .Type = wdSelectionShape Then
          With .ShapeRange(1)
            Result = InputBox("Please give this shape (" & .Name & _
              ") a meaningful name", "Rename Shape", .Name)
            If Result = .Name Then Exit Sub
            .Name = Result
          End With
        End If
      End With
      End Sub

      Cheers,
      Paul Edstein
      [Fmr MS MVP - Word]

    Viewing 3 reply threads
    Reply To: WORD (Office 365 version) Record macro to hide specific (not all) images

    You can use BBCodes to format your content.
    Your account can't use all available BBCodes, they will be stripped before saving.

    Your information: