Results 1 to 4 of 4
  1. #1
    3 Star Lounger
    Join Date
    Jan 2001
    Location
    Sydney, Australia, New South Wales, Australia
    Posts
    251
    Thanks
    0
    Thanked 4 Times in 4 Posts

    Pages collection; adding text boxes to every page (Word 2000 SR 1)

    Hi
    I have a document that I'm programattically adding text boxes to every page. The code below works fine, except if the user changes the active document or page as the macro is running. (The real macro is longer and takes longer). It always adds the text box to the right document, but sometimes a page might be missed, or text boxes might be placed on top of each other.
    The problem occurs with the use of the Selection object to change which page the shape is being added to - if the selection changes during execution then the macro starts adding boxes to the wrong page.
    I have my Document object; I just need to specify the page to the AddTextBox method. I'm looking for a Pages collection of the Document object, or some way to specify the page to the AddTextBox method.
    Also the mucky switching out of HTML view shouldn't be necessary.
    Any ideas?
    Peter
    <pre>Sub AddTextBoxesToDocument()
    Dim doc As Word.Document
    Dim wd As New Word.Application
    Dim nNumPages As Integer
    Dim counter As Integer

    Set doc = wd.Documents.Open(FileName:="cl3page.doc", ReadOnly:=True)
    doc.Repaginate
    nNumPages = doc.BuiltInDocumentProperties(wdPropertyPages)

    wd.Selection.Goto What:=wdGoToPage, which:=wdGoToFirst, count:=1, Name:=""

    If wd.ActiveWindow.View.Type = wdWebView Then
    'If the document is in web view (such as an HTML document that has been
    'opened in Word), then we need to display it in Print View. This is
    'because an HTML document has different default margins to a normal
    'Word document, and the text boxes will be placed over the top of the
    'text.
    wd.ActiveWindow.View.Type = wdPrintView
    End If
    For counter = 1 To nNumPages
    'Details on left hand side (Left,Top,Width,Height)
    With doc.Shapes.AddTextbox(msoTextOrientationHorizontal , _
    20#, 15#, 300#, 27#)
    .ScaleHeight 1.68, msoFalse, msoScaleFromTopLeft
    .RelativeHorizontalPosition = wdRelativeHorizontalPositionPage
    .RelativeVerticalPosition = wdRelativeVerticalPositionPage
    .LockAnchor = True

    .Left = wd.CentimetersToPoints(0.71)
    .Top = wd.CentimetersToPoints(0.53)

    .Line.Visible = msoTrue
    .TextFrame.TextRange.Font.Name = "Arial"
    .TextFrame.TextRange.Font.Size = 8

    .TextFrame.TextRange.text = "blah"
    End With
    'MsgBox "Swap docs now!"
    doc.Application.Selection.Goto What:=wdGoToPage, which:=wdGoToNext, _
    count:=1, Name:=""
    Next

    doc.SaveAs "clwithTextBoxes.doc"
    wd.Quit
    Set wd = Nothing
    End Sub
    </pre>


  2. #2
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Pages collection; adding text boxes to every page (Word 2000 SR 1)

    This may be naive, but if you're placing the same text box on every page, wouldn't it be better to place a text box in the page header? You need to create only one text box, and it will be displayed on every page.

  3. #3
    3 Star Lounger
    Join Date
    Jan 2001
    Location
    Sydney, Australia, New South Wales, Australia
    Posts
    251
    Thanks
    0
    Thanked 4 Times in 4 Posts

    Re: Pages collection; adding text boxes to every page (Word 2000 SR 1)

    Thanks Hans. Sorry, my example is not complete. Each page gets a unique number, which we call a 'Folio number', made of a 'File part number' and then a unique 'Folio Page number', which is like a Word page number, but is synchronised across multiple documents. Also, the first page has the highest 'Folio Page Number', then it goes down through the document. So ordinary page numbers won't work.
    Any other ideas?

  4. #4
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Pages collection; adding text boxes to every page (Word 2000 SR 1)

    You could use a formula field to display descending numbers, e.g. if you want the Folio Page Number to go from (number of pages) down to 1, you could use the following field code:

    { ={ NumPages }+1-{ Page } }

    or if the number should descend from a specified number, you could use e.g.

    { =38-{ Page } }

    Note: the brackets { } are field brackets; you shouldn't type them yourself, but either use Insert | Field... or type Ctrl+F9.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •