Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Wanet Telecoms Ltd on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

word template macro

Status
Not open for further replies.

bw2601

Technical User
Dec 16, 2004
29
US
I am trying to Create a Macro For a template that will auto update every time you open the document, the problem i am having so far is that i would like to have the dates in this macro be placed at the top of every page, and whn you run the try this macro, it puts all of the dates at the top of the first page. Please can someone help me! this is the code that I am using.

Sub calculateDate0()
'
' calculateDate1 Macro
'
'
Selection.TypeText Text:=Format(Date + 3, "DDDD, mmmm d, yyyy")

End Sub
Sub calculateDate1()
'
' CalculateDate2 Macro

'
Selection.TypeText Text:=Format(Date + 4, "DDDD, mmmm d, yyyy")
End Sub
Sub calculateDate2()
'
' calculateDate0 Macro

'
Selection.TypeText Text:=Format(Date + 5, "DDDD, mmmm d, yyyy")
End Sub
Sub calculateDate3()
'
' calculateDate3 Macro

'
Selection.TypeText Text:=Format(Date + 6, "DDDD, mmmm d, yyyy")
End Sub
Sub calculateDate4()
'
' calculateDate4 Macro

'
Selection.TypeText Text:=Format(Date + 7, "DDDD, mmmm d, yyyy")
End Sub
Sub TryThis()
'
' TryThis Macro

'
Application.Run MacroName:="calculateDate0"
Application.Run MacroName:="calculateDate1"
Application.Run MacroName:="calculateDate2"
Application.Run MacroName:="calculateDate3"
Application.Run MacroName:="calculateDate4"
End Sub
 
This inserts your date in the current selection. Now when you open a document, your cursor is automatically at the first page (aka the selection).

Either change your cursor`s place by using the select method or use headers to type your dates in...

Does this make sense to you?
 
Of course it will put it at the top of the first page.

The document opens. Where is the Selection? At the top of the first page. You have some design elements that you need to consider.

1. You state your objective is:
auto update every time you open the document
Your code does not do that. Your macros will add a day to the first date the macro runs. There is no relation to the act of opening the document.

2. If you want to have it to the top of every page, then you may have to consider putting it in the header. This can cause other issues; if you have an existing header; if you have different headers for first page or odd/even pages.

3. Depending on how you are doing page breaks ( derived or manually inserted) there may be a way to do what you seem to want.

The more important question is - WHY are you doing this? Do you want a listing of text for the dates the document is opened? Right now t6hose dates are sequential - what about weekends?

Please clarify exactly what you want to happen.

Gerry
 
This template is supposed to be a Schedule for the work week. I would like to print it on fridays which is why the weekends are not included. I would like a different consecutive date to be inserted at the top of every new page. i am not very concerned with it auto updating at this time.
 
Ok well why dont you try this :

Sub TryThis()
'
' TryThis Macro

'
Application.Run MacroName:="calculateDate0"
Selection.GoToNext (wdGoToPage)
Application.Run MacroName:="calculateDate1"
Selection.GoToNext (wdGoToPage)
Application.Run MacroName:="calculateDate2"
End Sub

Let me know if it works
 
oops, that paste was my test for only 3 pages... keep adding the Selection.GoToNext (wdGoToPage) before each function call.
 
I would like a different consecutive date to be inserted at the top of every new page.

This can be done. But answer this...consecutive starting WHEN????? And you have to be clear about another thing...are there going to be new pages added? You have to think about the design elements here.

The file is created and saved Thursday Jan 20th. It is the schedule for the following week - Jan 24th to 28th. So far, so good. It has five pages ALREADY WRITTEN.

1. are the pages hard page breaks, or pages breaks made from the expansion of text into new pages? This is crucial!

If they are hard page breaks, and the document is not going to change (VERY important), then:

Code:
Sub PageDates()
Dim var
Dim i As Integer
i = 1
On Error Resume Next
Selection.HomeKey Unit:=wdStory
Selection.TypeText Text:=Format(Date, "DDDD, mmmm d, yyyy")
For var = 1 to 4
  Selection.GoTo What:=wdGoToPage, _
       Which:=wdGoToNext, Count:=1, Name:=""
  Selection.TypeText _
       Text:=Format(Date + i, "DDDD, mmmm d, yyyy")
  i = i + 1
Next
End Sub

So, OK... the above will type in the date incremented by 1 for five pages. No sweat.

Problems:

1. If you remove text anywhere, the date will be moved as well. text flows along according to the amount present, yes? So sure, the day for the second page may show the correct second date, but if you removed some text on the pervious page, that date could very easily be moved back into the wrong page.

My point being is that TYPING in the dates is the wrong way to go about it...that is if there is ever coing to be the possibility that the document could ever be edited.

The second problem is that your original post stated you wanted the dates to reflect when the document was opened. Is this no longer an issue?: Because the code above, not the code you posted covers this.

It certainly can be done, but you have not answered if this is required.

Gerry
 
Sylve

Your suggestion worked out perfectly. Thank you so much for your help I really appreciate it.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top