• Manipulating graphics within macros (2003 SP1)

    Home » Forums » AskWoody support » Productivity software by function » MS Word and word processing help » Manipulating graphics within macros (2003 SP1)

    Author
    Topic
    #421772

    Hi all

    My clients occasionally ask me to create Word template versions of their stationery (usually opening page and follower) with the graphics embedded in the header.

    In the past, I’ve been able to impress the heck out of them with a toolbar button or menu command that toggles the display of the graphics without altering vertical positioning of the document body, so that they can use preprinted stationery if required (it’s easier than it sounds – just use a macro to select each graphic and turn its brightness up to 100% – or back to default, whichever they want).

    Since switching to Word 2003, I’ve noticed that it no longer permits graphics to be selected and manipulated if I am recording a macro – it doesn’t matter whether they are in the header / footer or document body. While recording a macro, I’ve also tried to select a graphic by hitting F5 and “+” – but this just takes me out of header / footer view. If I try it in the document body, it just generates an audible warning.

    My preference is to position the graphics in-line rather than floating, but I’ll compromise on this if necessary (I don’t seem to be getting the desired result with either option.)

    And if that wasn’t enough of a challenge, my current client operates Word 97, 2000 and XP (no, they haven’t taken the plunge on 2003 yet).

    Once again, I’d be enormously grateful if someone who knows more than me about this stuff could suggest a workaround for this limitation that will work on all of these versions of Word. (I have, of course, trawled previous postings, but not found anything on this topic.) I am sure some of the Lounge Legends know how to get around this.

    Thanks in advance

    Neil

    Viewing 1 reply thread
    Author
    Replies
    • #959455

      If you have named the shape, you can use this macro.:

      Sub LogoOnOff(strName As String, blnVisible As Boolean)
          ActiveWindow.ActivePane.View.SeekView = wdSeekCurrentPageHeader 'or wdSeekCurrentPageFooter
          With Selection.HeaderFooter.Shapes(strName)
              If blnVisible = True Then
                  .Visible = msoTrue
              Else
                  .Visible = msoFalse
              End If
          End With
      End Sub
      

      If you haven’t given the shape a name you can use the index number. Change the relevant lines into
      Sub LogoOnOff(iIndex As Integer, blnVisible As Boolean)
      and
      With Selection.HeaderFooter.Shapes(iIndex)

      • #959457

        Jan hi

        That sounded like a plan – but I’m embarrassed to say that I’m not familiar with “naming” objects, so I’ll have to research this a little.

        I tried pasting the code (with the indexing changes you proposed) as a new macro and it won’t even display in the list of available macros. I like to think that I’m not a complete amateur with this stuff, but I’ve never seen that happen before!

        Thanks for your help

        Neil

        • #959466

          Don’t know why the macro doesn’t appear in the list of macros.
          Just curious: why are you manipulating the brightnes of the shape instead of the Visible property?

          • #959469

            A macro is a Sub without any arguments. Your procedure takes two arguments, therefore it is not listed in the Tools | Macro | Macros dialog. It has to be called from other code (for example from a macro), or from the Immediate window.

          • #959545

            Jan

            I got Hans’ tip earlier about calling your macro from another macro, so I’ll give that a try.

            As for brightness vs Visible, I wasn’t aware of the Visible property – is that available from the format picture options, or only through a macro? If it’s the latter, the reason would be that I tend to create almost all of my macros by recording and and tweaking them if necessary with limited skills. But I am always delighted to learn from experts!

            Any suggestions most welcome!

            Thanks again

            Neil

            • #959782

              It’s available through a macro. In the letterhead of my employer there are 2 extra logo’s (floating shapes) from different departments. With a button on a commandbar a user can loop trough 3 possibilities: both invisible (default), dep1 visible or dep2 visible. The code looks a lot like Hans’ last version of his macro in this thread (but with manipulation of the Visible property instead of Brightness), including saving and restoring the users view. Because I believe it’s good programming practice to save and restore the state of properties, options, etc. that are changed by the macro to perform a certain task.

            • #960017

              Jan

              That sounds really interesting – if you have an opportunity to post that code (or send it off-line), that would be outstanding. I’m very interested in knowing how to manage flotaing graphics & I’d also like to hear more about the ‘visible’ property that you mentioned – I assume that it’s not the same as transparency. I have searched around for it and clearly need to learn more!

              Thanks very much for your responses to my query – they’ve been very helpful and are much appreciated.

              Best regards

              Neil

            • #960793

              Just back from a long weekend in the sun…
              OK, I’ll post a doc with a working example. But I have to isolate that from a large template and maybe translate some items from Dutch to English. So it can take a day (or two).

            • #960887

              OK, here’s the demo doc. Enjoy!

            • #960896

              Jan – that’s cool – I look forward to pulling it to bits and figuring out how it works.

              cheers

              neil

            • #961064

              You’re welcome.
              I saw that I forgot to protect the doc, so that you can tab through the formfields. Also I didn’t explain how to add the logo’s to the doc and name them. Follow these steps:
              – switch to header/footer view and click in the footer
              – insert a logo via Insert | Picture | From file…
              – be sure that it’s a floating shape
              – select the logo, drag it to the correct location and adjust the dimensions
              – leave the logo selected
              – switch to the VB editor (Alt+F11)
              – in the Immediate window type: Selection.ShapeRange(1).Name = “Dep1”
              – repeat these steps for the second logo (don’t change (1) in the previous line…!)

            • #961112

              Jan

              This rocks – I’ll have hours of fun with it!!

              Thanks heaps for all your help.

              Best regards

              Neil

    • #959456

      I don’t have word 2003, but perhaps you can use or adapt the following code:

      Sub ToggleShapes()
      Dim shp As Shape
      For Each shp In ActiveDocument.Sections(1).Headers(1).Shapes
      shp.PictureFormat.Brightness = 1.5 – shp.PictureFormat.Brightness
      Next shp
      End Sub

      It’ll toggle the brightness of shapes between 0.5 (50%) and 1 (100%).

      • #959458

        Hans

        Many thanks for your prompt reply. I tried this & the graphic briefly blinked on and off – but nothing else. I tried starting with brightness=0 and 100% to see if I could figure out what was going on, but after running the macro, the value is unchanged.

        But the code does appear to allow me access the graphics in the header through a macro, so that’s a step forward!

        Thanks again

        Neil

        • #959460

          Are the graphics inline shapes or floating shapes?

          • #959543

            Hans

            I’d prefer inline, but they can be floating if that will expedite a solution.

            neil

            • #959547

              Try this macro:

              Sub ToggleGraphhics()
              Dim hf As HeaderFooter
              Dim oShape As InlineShape
              Dim sect As Section
              ‘ Hide what we’re doing
              Application.ScreenUpdating = False
              ‘ Switch to header/footer view
              If ActiveWindow.View.SplitSpecial wdPaneNone Then
              ActiveWindow.Panes(2).Close
              End If
              If ActiveWindow.ActivePane.View.Type = wdNormalView Or ActiveWindow. _
              ActivePane.View.Type = wdOutlineView Or ActiveWindow.ActivePane.View.Type _
              = wdMasterView Then
              ActiveWindow.ActivePane.View.Type = wdPageView
              End If
              ActiveWindow.ActivePane.View.SeekView = wdSeekCurrentPageHeader
              ‘ Loop through sections
              For Each sect In ActiveDocument.Sections
              ‘ Loop through headers
              For Each hf In sect.Headers
              If Not hf.LinkToPrevious And hf.Exists Then
              ‘ select range
              hf.Range.Select
              ‘ Toggle shape
              For Each oShape In Selection.InlineShapes
              oShape.PictureFormat.Brightness = 1.5 – oShape.PictureFormat.Brightness
              Next oShape
              End If
              Next hf
              Next sect
              ‘ Back to main doc
              ActiveWindow.ActivePane.View.SeekView = wdSeekMainDocument
              ‘ Show result
              Application.ScreenUpdating = True
              End Sub

            • #959562

              Hans

              That’s amazing[/u] – it works brilliantly! It even works on a second page header that has been defined, even when there is only one page in the document.

              At the risk of stretching a friendship (I always seem to do this on the 2-3 occasions a year that I post queries here), can I ask your advice on making this apply at the same time to a graphic that will be in the footer?

              I’m sure I could simply copy the body of the macro & alter it to change “header” references to “footer” – or is there a substantially more efficient way of doing it?

              I’ve noticed that this is definitely oriented towards in-line graphics – so I may set myself a project of figuring out how to adapt this to floating pix also.

              Once again – thanks very much indeed (to you and Jan) for taking the time to come up with solutions for me – it is very greatly appreciated!

              Kindest regards

              Neil

            • #959572

              You don’t have to duplicate the entire macro, you only need to duplicate the loop

              For Each hf In sect.Headers

              Next hf

              and change the copy to

              For Each hf In sect.Footers

              Next hf

            • #959577

              Hans – you may be encouraged to know that I just figured that out by myself!

              And one very, very last question (for real – it’s almost 11pm here): can you suggest a way to detect the current view (e.g. normal, print) etc at the beginning of the macro & then set it back to that at the end? Some of my client’s users have extremely limited skills and even changing view would spook them.

              Thanks a million for all your help

              neil

            • #959584

              You could add this at the beginning to store the current view type in a variable:

              Dim curViewType
              curViewType = ActiveWindow.ActivePane.View.Type

              and restore the view type at the end:

              ActiveWindow.ActivePane.View.Type = curViewType

            • #959592

              Hans

              That is sensational – at the risk of repeating myself, many thanks for all your help!

              All the best

              Neil

    Viewing 1 reply thread
    Reply To: Manipulating graphics within macros (2003 SP1)

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

    Your information: