Jump to content

Recommended Posts

Posted

I'm using a VBA for Word macro that creates a series of shapes inside the headers and footers of the document. The last one that I create is a line shape in the header of page two. After the creation of this shape, I'd like to get back into the main document and continue on from there but I can't seem to make it happen. I get an error message that says, "This method or property is not available because the current selection is not in a header or footer." However, the shape is created in the header, so I don't understand why I'm getting this error message.

 

Here's the code I'm using :

 

'Create the shape and set it's properties

.Headers(wdHeaderFooterPrimary).Shapes.AddShape msoLine, 50, 40, 500, 1

ActiveDocument.Sections(1).Headers(wdHeaderFooterFirstPage).Shapes(4).Select 'Select the shape

'Set the shape's properties

Selection.ShapeRange.Line.ForeColor = RGB(0, 0, 0) 'change line color to black

Selection.ShapeRange.Shadow.Type = msoShadow6

 

'Get back into the main document

ActiveWindow.ActivePane.View.SeekView = wdSeekMainDocument

 

I've tried deselecting the shape in order to select the paragraph mark inside the header and I still get the same error message.

 

I'm stumped. Any advice or help is greatly appreciated!

Posted

Like this?

 


    Dim Section As Long
    Section = 1

    With ActiveDocument.Sections(Section).Headers(wdHeaderFooterPrimary)
        .Shapes.AddShape msoShapeRegularPentagon, 10, 10, 100, 100, .Range
    End With
    
   ActiveWindow.ActivePane.View.SeekView = wdSeekMainDocument

 

Draws single shape in header, then goes to the main page for whatever else you're adding in.

 

I feel you're missing something from what you've shown us in the code above in terms of even getting that part to work, but see if what I mean is right :) (Works in 2007 at least)

 

Steve

Posted

Hi Steve, thank you for responding. I'm using Word 2010 if that means anything.

 

You are right about the code I posted. It is edited for simplicity and just show the trouble spot. The part where I create and format the shape is within a With/End With statement. In fact it is the fourth shape that I create - two on page one and then two on page two (including the line in the header of page two). They are all created fine but when it tries to execute the line:

 

ActiveWindow.ActivePane.View.SeekView = wdSeekMainDocument

 

I get the error, "This method or property is not available because the current selection is not in a header or footer." Which doesn't make sense, of course, since the blasted line was created in the header. I've also tried to deselect the shape and select the paragraph mark inside the header but I still get the same error message. I've even selected the paragraph mark in the header as well as the shape and still get the same error message.

 

Very strange.

Posted
In looking at this again, I'm noticing something that might be a clue. When the shape is created and thus "selected", the header window or pane (not sure what to call it) is not open but the shape is clearly selected. The same is true when I select the paragraph mark in the header - it's clearly selected but the header window/pane is not open - it looks as the though I'm in the main document but the selection is clearly not. By saying that the header/footer window or pane is not open, I mean that those "Header" and "Footer" upside down shaded tabs are not visible (if you double click on a document's header or footer the header/footer pane will open and these tabs appear).
Posted
Steve, your code works for me too. I'm thinking that because I SELECT the shape in order to set a few properties, this causes the macro to behave differently in some way. I still don't get why it still doesn't work when I deselect the shape and select the paragraph mark in the header.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now



×
×
  • Create New...