Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
DiscussionsAccessExcelInfoPathOutlookPowerPointPublisherWord
DirectoryUser Groups
Related Topics
Outlook ExpressInternet ExplorerWindowsMS Server ProductsMore Topics ...

MS Office Forum / Word / Programming / February 2008

Tip: Looking for answers? Try searching our database.

VB Error?

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Scott R - 05 Feb 2008 21:44 GMT
Hi Guys, I have a sign in sheet template that I use and have the following
code in it. It is supposed to print my template consecutive dates at the top
of each sheet as per my instructions in the vb code. Im sure this was working
correctly but now when I run it, it brings up random dates. If I enter 10
days from 01/01/08 I get dates in February and so on. Is there something I
may have done to the coding? Can someone have a look at ot and see what they
get? Any help on this would be appreciated.
Thanks
Scott
(PS I currently have the printing disabled in the code)

Sub PrintNumberedCopies()
Dim i As Long
Dim d As Date
Dim pStr As String
Dim oRng As Range
Dim NumCopies As Long
If MsgBox("The date will appear at the insertion point." _
   & " Is the cursor at the correct position?", _
   vbYesNo, "Placement") = vbNo Then End
If ActiveDocument.Saved = False Then
   If MsgBox("Do you want to save any changes before" & _
       " printing?", vbYesNoCancel, "Save document?") _
       = vbYes Then
       ActiveDocument.Save
   End If
End If
d = CDate(InputBox("Enter the starting date.", _
   "Date", "1 / 1 / 2008"))
NumCopies = Val(InputBox("Enter the number of copies that" & _
   " you want to print", "Copies", 1))
ActiveDocument.Bookmarks.Add Name:="Date", Range:=Selection.Range
Set oRng = ActiveDocument.Bookmarks("Date").Range
i = 0
If MsgBox("Are you sure that you want to print " _
   & NumCopies & " dated " & " copies of this document", _
   vbYesNoCancel, "On your mark, get set ...?") = vbYes Then
   While i < NumCopies
       oRng.Delete
       d = DateAdd("d", i, d)
       pStr = Format(d, "dddd dd MMMM, yyyy")
       oRng.Text = pStr
       oRng.Font.Size = "24"
   ' ActiveDocument.PrintOut
       i = i + 1
   Wend
End If
End Sub
David Sisson - 06 Feb 2008 14:44 GMT
In this line
>         d = DateAdd("d", i, d)

You're adding the loop counter to the starting date then replacing the
starting date with the future date, so it increments exponentially
instead of incrementally.

Add one more variable

Dim dTmp as Variant

and replace these lines
>         d = DateAdd("d", i, d)
>         pStr = Format(d, "dddd dd MMMM, yyyy")

with this
       dTmp = DateAdd("d", i, d)
       pStr = Format(dTmp, "dddd dd MMMM, yyyy")
Scott R - 06 Feb 2008 20:56 GMT
Anyone told you your a legend lately!!
Thankyou!

> In this line
> >         d = DateAdd("d", i, d)
[quoted text clipped - 14 lines]
>         dTmp = DateAdd("d", i, d)
>         pStr = Format(dTmp, "dddd dd MMMM, yyyy")
David Sisson - 06 Feb 2008 21:08 GMT
Glad I could help.

David
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.