View Single Post
 
Old 06-28-2022, 08:58 PM
oyster oyster is offline Windows 10 Office 2013
Novice
 
Join Date: Jun 2022
Posts: 1
oyster is on a distinguished road
Default how to use word vba to read header, mixed with text and shape, on last page of very section?

In fact, I have asked this on how to use word vba to read header, mixed with text and shape, on last page of very section? - Stack Overflow but there is no solution yet.


The situation is, sometimes I have to check some papers. The check items include checking whether the header has proper literal information. macropod supplies a solution for text in header on how to use word vba to read header's text on last page? - Stack Overflow .

However, I meet doc which mixes text and shape in the header, and the useful information is actually in shape. I made a simplified version mix_txt_shape_in_header.docx and put it on GitHub - retsyo/study_vba_doc

For this doc, what I expected is to extract all displayed text( so it should be read from both text and text_in_shape) on last page in every section, and prompt something like

Code:
Index page i of i  
Main part page 2 of 32
English is not my native language. I hope I have make it clear by using above doc and expected output.

After study macropod's code, I think if I can select the shape in the header, I can read the information. But soon I meet questions


  • how to select the shape
I record a vba code while I operate on shape in header, and find that shape's name is used in the vba, for example
Code:
ActiveDocument.Shapes("TextBox 1").Select
obviously, it is not applicable because I can't know shape's name in advance.

  • for the reason I stated above, I can't figure out how to get the text of shape, which is on the last page of every section. Here is what I get as far as I can

Code:
Sub read()     
    NumSections = ActiveDocument.Sections.Count     
    For idxsec = 1 To NumSections         
        With ActiveDocument.Sections(idxsec).Headers(wdHeaderFooterPrimary)
             numShape = .Shapes.Count
             For idxShape = 1 To numShape
                 txt = "section " & idxsec & ", shape " & idxShape & ": "
                 txt = txt & .Shapes(idxShape).TextFrame.TextRange.Text
                 Debug.Print txt
             Next
         End With
         Debug.Print "====="
     Next
End Sub



However it says
Code:
section 1, shape 4:  
section 1, shape 4: page i of  i 
section 1, shape 4:                          <-- why this shape is in section 1 ?
section 1, shape 4: page 1 of  32            <-- why this shape is in section 1 ?
===== 

section 2, shape 4:                          <-- why this shape is in section 2 ?
section 2, shape 4: page i of  i             <-- why this shape is in section 2 ?
section 2, shape 4:  
section 2, shape 4: page 1 of  32 
=====
it is clear that
Code:
 Sections(idxsec).Headers(wdHeaderFooterPrimary).Shapes
does not find shape in the section only, on the other hand, it finds all shape in the doc

  • to simulate what human eyes read from header, I have to judge which one, text or text_in_shape, should be placed first.

3.1 for the text
there is no something like
Code:
? ActiveDocument.Sections(1).Headers(wdHeaderFooterEvenPages, wdHeaderFooterOddPages, or wdHeaderFooterPrimary).Range.Width
3.2 for the shape
Code:
? ActiveDocument.Sections(1).Headers(wdHeaderFooterEvenPages).Shapes(2).Width
produces

Code:
 68.2
which seems to be reasonable, however

Code:
? ActiveDocument.Sections(1).Headers(wdHeaderFooterPrimary).Shapes(1).left
returns
Quote:
-999996


as what Shrotter told me, it is WdShapePosition, then how to get its real x position


Any help? Thanks.
Reply With Quote