Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 01-09-2014, 03:29 PM
jakeic jakeic is offline Add text box to each page in a word doc Windows 8 Add text box to each page in a word doc Office 2013
Novice
Add text box to each page in a word doc
 
Join Date: Jan 2014
Posts: 5
jakeic is on a distinguished road
Default Add text box to each page in a word doc

Dear vba pros,

I recently need to do some referencing job for a large amount of pages.
Most of the documents originally were in the PDF format. But i managed to convert them into word as images (because it seems the conversion would cause some format changes if they are not converted as images, each page could contain several images )

So what i need to do is, i need to add a text box at the top right hand side to EACH page, and the content for each text would be:

Ref no
Signature & Date

the ref no would be in the format as "letters + number"
e.g: T100 for the first page, T101 for the second page....


or: AB100 etc

--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Btw I don't think the header and footer would work in this situation, as some of images blocked the header ares, and also I can't customize the ref no to the way I need it to be
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

the Ref no would be in bold and red color
the signature and date would be in bold and blue.

Im thinking the macro could use a userform to obtain the ref no and the signature, however, after a million tries, i still stuck in the every beginning.

I would be grateful if any of you could help pls?

Thanks a million

Last edited by jakeic; 01-10-2014 at 01:28 AM.
Reply With Quote
  #2  
Old 01-09-2014, 04:50 PM
macropod's Avatar
macropod macropod is offline Add text box to each page in a word doc Windows 7 32bit Add text box to each page in a word doc Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,963
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

Based on what you've described, you don't need a macro for this. Instead, you can insert the required content into the page header. You'd only need to put the Signature & Date in once, coupled with a PAGE # field to number all the pages for the Ref#.
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #3  
Old 01-10-2014, 01:24 AM
jakeic jakeic is offline Add text box to each page in a word doc Windows 7 32bit Add text box to each page in a word doc Office 2010 32bit
Novice
Add text box to each page in a word doc
 
Join Date: Jan 2014
Posts: 5
jakeic is on a distinguished road
Default

Quote:
Originally Posted by macropod View Post
Based on what you've described, you don't need a macro for this. Instead, you can insert the required content into the page header. You'd only need to put the Signature & Date in once, coupled with a PAGE # field to number all the pages for the Ref#.
Hi Macropod, thanks for ur reply.
Sorry for the confusion, the header and footer would not work because some of images would block the header area, and also for header, I can't customize the ref no
I think it would only work for simple number like the page number etc.
what I would need to do is a referring sth like:

AB100
AB101....

I think it could only be done by macros?

Thanks
Reply With Quote
  #4  
Old 01-10-2014, 04:16 AM
macropod's Avatar
macropod macropod is offline Add text box to each page in a word doc Windows 7 32bit Add text box to each page in a word doc Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,963
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

You can indeed format the page #s with a prefix - and to start at a # other than 1 - but since it appears you need a macro, here's some code to get you started:
Code:
Sub Demo()
Dim i As Long, Rng As Range, Shp As Shape
With ActiveDocument
  For i = 1 To .ComputeStatistics(wdStatisticPages)
    Set Rng = .GoTo(What:=wdGoToPage, Name:=i)
    Set Rng = Rng.GoTo(What:=wdGoToBookmark, Name:="\page")
    Rng.Collapse wdCollapseStart
    Set Shp = .Shapes.AddTextbox(Orientation:=msoTextOrientationHorizontal, _
      Left:=0, Top:=0, Width:=180, Height:=60, Anchor:=Rng)
    With Shp
      .RelativeHorizontalPosition = wdRelativeHorizontalPositionMargin
      .Left = wdShapeLeft
      .RelativeVerticalPosition = wdRelativeVerticalPositionMargin
      .Top = wdShapeTop
      .Fill.Visible = False
      With .TextFrame.TextRange
        .Font.Bold = True
        .Font.ColorIndex = wdBlue
        .Text = "Ref. No.: T" & vbCr & "Signature " & Format(Now, "DDDD, D MMM YYYY")
        Set Rng = .Paragraphs.First.Range
        With Rng
          .Font.ColorIndex = wdRed
          .End = .End - 1
          .Collapse wdCollapseEnd
        End With
        .Fields.Add Range:=Rng, Type:=wdFieldEmpty, Text:="PAGE \# 000", PreserveFormatting:=False
      End With
    End With
  Next
End With
End Sub
As coded, the macro makes the textboxes transparent. If you don't want that, delete '.Fill.Visible = False'.
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #5  
Old 01-10-2014, 02:28 PM
jakeic jakeic is offline Add text box to each page in a word doc Windows 7 32bit Add text box to each page in a word doc Office 2010 32bit
Novice
Add text box to each page in a word doc
 
Join Date: Jan 2014
Posts: 5
jakeic is on a distinguished road
Thumbs up

Hi Macropod
Your code works perfectly!
Just one more small thing although.
I amended it a little bit to fit my purpose better, I added a userform to obtain the reference information from the user.

then store the starting reference numerical number in variable: Myref
say the user input 100
myref = 100

and i would like to reference the first page as 100, the next one is 101... etc

however currently the code is using the page number only.

Could you please advise how to amend this?

Thanks a mill

Last edited by macropod; 01-10-2014 at 03:17 PM. Reason: Deleted unnecessary quote of entire post replied to.
Reply With Quote
  #6  
Old 01-10-2014, 03:17 PM
macropod's Avatar
macropod macropod is offline Add text box to each page in a word doc Windows 7 32bit Add text box to each page in a word doc Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,963
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

Quote:
Originally Posted by jakeic View Post
I amended it a little bit to fit my purpose better, I added a userform to obtain the reference information from the user.

then store the starting reference numerical number in variable: Myref
say the user input 100
myref = 100

and i would like to reference the first page as 100, the next one is 101... etc
There are at least three ways of handling this:
1. set the page StartingNumber to whatever you want. For example, use:
Code:
.Sections(1).Headers(wdHeaderFooterPrimary).PageNumbers.StartingNumber = myref
2. hard-code the page #s instead of using a field. For example, use:
Code:
.Text = "Ref. No.: " & StrPre & (i + myref - 1) & vbCr & "Signature " & Format(Now, "DDDD, D MMM YYYY")
and replace:
Code:
        Set Rng = .Paragraphs.First.Range
        With Rng
          .Font.ColorIndex = wdRed
          .End = .End - 1
          .Collapse wdCollapseEnd
        End With
        .Fields.Add Range:=Rng, Type:=wdFieldEmpty, Text:="PAGE \# 000", PreserveFormatting:=False
with:
Code:
.Paragraphs.First.Range.Font.ColorIndex = wdRed
3. add a calculation to the page# field code. Either of the above two should meets your needs, though.
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #7  
Old 01-10-2014, 04:14 PM
jakeic jakeic is offline Add text box to each page in a word doc Windows 7 32bit Add text box to each page in a word doc Office 2010 32bit
Novice
Add text box to each page in a word doc
 
Join Date: Jan 2014
Posts: 5
jakeic is on a distinguished road
Default

Thanks a lot Macropod.

this is almost my final code:


Code:
Dim x As Long, Rng As Range, Shp As Shape
With ActiveDocument
 
  For x = 1 To .ComputeStatistics(wdStatisticPages)
    Set Rng = .GoTo(What:=wdGoToPage, Name:=x)
    Set Rng = Rng.GoTo(What:=wdGoToBookmark, Name:="\page")
    Rng.Collapse wdCollapseStart
    Set Shp = .Shapes.AddTextbox(Orientation:=msoTextOrientationHorizontal, _
      Left:=0, Top:=0, Width:=100, Height:=40, Anchor:=Rng)
    With Shp
      .RelativeHorizontalPosition = wdRelativeHorizontalPositionMargin
      .Left = wdShapeRight
      .RelativeVerticalPosition = wdRelativeVerticalPositionMargin
      .Top = wdShapeTop
      .Fill.Visible = False
      With .TextFrame.TextRange
        .Font.Bold = True
        .Font.ColorIndex = wdBlue
        .Text = AlphaPortion & StrPre & (x + NumPortion) & vbCr & Me.TextBox2.Value & " " & Format(Now, "D MMM YYYY")
        .ParagraphFormat.SpaceAfter = 0
        Set Rng = .Paragraphs.First.Range
        With Rng
         .Font.ColorIndex = wdRed
        .End = .End - 1
          .Collapse wdCollapseEnd
        End With
 
      End With
    End With
  Next
End With
End Sub

however, there is a small bug for this code, when i run it, it seems it will first add text box to the last page, and then starting from the second page, until the last page again.

So the first page won't have any textbox, but the last page would have two, one cover over the other one.

Im not sure where did i do wrong?

Last edited by macropod; 01-10-2014 at 04:43 PM. Reason: Deleted unnecessary quote of entire post replied to.
Reply With Quote
  #8  
Old 01-10-2014, 05:00 PM
macropod's Avatar
macropod macropod is offline Add text box to each page in a word doc Windows 7 32bit Add text box to each page in a word doc Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,963
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

I can't see why that would occur. the following works for me:
Code:
Sub Demo()
Dim i As Long, Rng As Range, Shp As Shape
Dim iStart As Long, StrPfx As String, StrSig As String
With ActiveDocument
  StrPfx = InputBox("What is the Ref Prefix?")
  StrSig = InputBox("Who's Signature?")
  iStart = CLng(InputBox("What is the Starting #?"))
  For i = 1 To .ComputeStatistics(wdStatisticPages)
    Set Rng = .GoTo(What:=wdGoToPage, Name:=i)
    Set Rng = Rng.GoTo(What:=wdGoToBookmark, Name:="\page")
    Rng.Collapse wdCollapseStart
    Set Shp = .Shapes.AddTextbox(Orientation:=msoTextOrientationHorizontal, _
      Left:=0, Top:=0, Width:=100, Height:=40, Anchor:=Rng)
    With Shp
      .RelativeHorizontalPosition = wdRelativeHorizontalPositionMargin
      .Left = wdShapeRight
      .RelativeVerticalPosition = wdRelativeVerticalPositionMargin
      .Top = wdShapeTop
      .Fill.Visible = False
      With .TextFrame.TextRange
        .ParagraphFormat.SpaceAfter = 0
        .ParagraphFormat.SpaceBefore = 0
        .Font.Bold = True
        .Font.Size = 8
        .Font.ColorIndex = wdBlue
        .Text = "Ref. No.: " & StrPfx & (i + iStart - 1) & vbCr & StrSig & " " & Format(Now, "D MMM YYYY")
        .Paragraphs.First.Range.Font.ColorIndex = wdRed
      End With
    End With
  Next
End With
End Sub
Note that I've made a few extra changes because I'm not using a userform and to control the textbox formatting, but that doesn't affect the underlying process.

PS: Please don't quote entire posts in your reply - only quote what's actually necessary.
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #9  
Old 01-11-2014, 02:44 PM
jakeic jakeic is offline Add text box to each page in a word doc Windows 7 32bit Add text box to each page in a word doc Office 2010 32bit
Novice
Add text box to each page in a word doc
 
Join Date: Jan 2014
Posts: 5
jakeic is on a distinguished road
Default

Hi Paul, apologies for quoting the entire code, was my first time to this forum.
I tested the code in a blank document, it works perfectly, but for some weird reason it was working like it suppose to in another one with contents. But after i convert that document from .doc to .docx, the code works great again!
Not too sure why that happened, but it works, that is what's important!!
A million thanks for your help!
Im marking this as resolved now.
Reply With Quote
  #10  
Old 01-11-2014, 03:35 PM
macropod's Avatar
macropod macropod is offline Add text box to each page in a word doc Windows 7 32bit Add text box to each page in a word doc Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,963
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

The only thing I can think of is that your (in-line?) images were pushing their paragraph breaks to the next page, with the result that everything until the last one was physically offset one page from the one they were logically attached to.
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
Reply

Tags
pages, referencing, text box



Similar Threads
Thread Thread Starter Forum Replies Last Post
Text disappears (but headings and text boxes ok) when printing 1 page of a document msfordummies Word 1 02-21-2013 10:28 PM
Add text box to each page in a word doc Macro to Add Text and Page Number to Top of Each Page Within Text eslight Word VBA 10 12-07-2012 08:18 PM
Add text box to each page in a word doc Selection of all Text for a specific page in word is spanning selection across pages ramsgarla Word VBA 9 12-05-2012 03:23 AM
2 page document printing problem, text from page 1 in layout of page 2 when printed laurawether45 Word 1 08-02-2012 07:03 AM
Add text box to each page in a word doc MS Word, page goes to next page when entering data on previous page munna94 Word 2 12-30-2010 08:12 AM

Other Forums: Access Forums

All times are GMT -7. The time now is 09:29 AM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2024 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft