using ordinal switches in a macro

Hi!

 

I am using the following macro and everything works fine except for the ordinal switches. The VBA code checker is not accepting the asterik used in the normal switch for ordinals.

 

Sub AutoNew()
'
'
Selection.GoTo What:=wdGoToBookmark, Name:="TimeA"
Selection.InsertBefore Format((Date + 1), "MMMM d" \ ordinal)
Selection.GoTo What:=wdGoToBookmark, Name:="TimeB"
Selection.InsertBefore Format((Date + 3), "MMMM d" \ ordinal)

End Sub

 

Can you help me?

 

Thanks for any help that you can give me!

 

Audrey

* Please try a lower page number.

* Please enter only numbers.

* Please try a lower page number.

* Please enter only numbers.

Hope this helps,
Doug Robbins - MVP Office Apps & Services (Word)
dougrobbinsmvp@gmail.com
It's time to replace ‘Diversity, Equity & Inclusion’ with ‘Excellence, Opportunity & Civility’ - V Ramaswamy

Was this reply helpful?

Sorry this didn't help.

Great! Thanks for your feedback.

How satisfied are you with this reply?

Thanks for your feedback, it helps us improve the site.

How satisfied are you with this reply?

Thanks for your feedback.

The Format function in VBA doesn't use switches like \* ordinal that you can put into field codes.

The code that Doug pointed to will work, although it's a bit clumsy. The function at http://misterslimm.wordpress.com/2009/02/13/ordinal-suffixes-for-all-dates-and-numbers-show-3rd-instead-of-3  produces the same result but is easier to understand. You can leave out all the comment lines if you want to. Look at the next-to-last of the examples (just above the code) to see how to call the function from your macro.

In either case, you need to separate your InsertBefore into two parts, one for the month and one for the day. For example, if Date is January 21, then

Selection.InsertBefore Format((Date + 1), "MMMM") & " " & FormatOrdinal(Format((Date + 1), "d"))

will produce January 22nd.
_____________________________
https://jay-freedman.info

Was this reply helpful?

Sorry this didn't help.

Great! Thanks for your feedback.

How satisfied are you with this reply?

Thanks for your feedback, it helps us improve the site.

How satisfied are you with this reply?

Thanks for your feedback.

Thank you! I am still trying to work things out. Problem is that I try to do things that are way above my abilities and STRUGGLE. But, eventually, I guess that I will figure it out. I do appreciate your input!

 

Audrey

aleeherself, inventive problem solver

Was this reply helpful?

Sorry this didn't help.

Great! Thanks for your feedback.

How satisfied are you with this reply?

Thanks for your feedback, it helps us improve the site.

How satisfied are you with this reply?

Thanks for your feedback.

Jay,

 

I definitely appreciate your help as well, but am still struggling to make the document do what I want it to do. I am very appreciative for all the help you kind folks give me. But, I guess that what I really need, is to noodle out what it is that I really need to ask because even with both of your suggestions, the document is still not behaving the way that I want. Guess that is what always happens when I try to venture into waters that I not only have no oars for, but am "missing the boat as well!"

 

Thanks, again! When I figure out what exactly I need to ask, I will return.

 

Audrey

aleeherself, inventive problem solver

Was this reply helpful?

Sorry this didn't help.

Great! Thanks for your feedback.

How satisfied are you with this reply?

Thanks for your feedback, it helps us improve the site.

How satisfied are you with this reply?

Thanks for your feedback.

Audrey,


While your noodling ;-) here is another approach at inserting ordinal dates:

Sub InsertOrdinalDate()
Dim strDate As String, strDate2, strMonth As String
Dim lngMonth As Long
Dim oRng As Word.Range
   strDate = CStr(Now)
   lngMonth = Month(strDate)
   strMonth = Choose(lngMonth, "January", "February", "March", "April", "May", "June", _
                     "July", "August", "September", "October", "November", "December")
   strDate2 = " day of " & strMonth & " " & Year(CDate(strDate))
   Set oRng = Selection.Range
     With oRng
       .Text = Day(CDate(strDate))
       .Start = .End
       .InsertAfter Ordinal(Day(CDate(strDate)))
       .Font.Superscript = True
       .Start = .End
       .InsertAfter strDate2
       .Font.Superscript = False
     End With
End Sub
Function Ordinal(lngNum As Long) As String
'Adapted from function posted Microsoft Answers by Paul Edstien.
Dim strOrd As String
  If (lngNum Mod 100) < 11 Or (lngNum Mod 100) > 13 Then
    Select Case lngNum Mod 10
      Case 1: strOrd = "st"
      Case 2: strOrd = "nd"
      Case 3:  strOrd = "rd"
      Case Else: strOrd = "th"
    End Select
  End If
  Ordinal = IIf(strOrd = vbNullString, "th", strOrd)
End Function

Greg Maxey
***
Death smiles at us all, but all a man can do is smile back.


For more help with Word visit:
http://gregmaxey.com/word_tips.html

Was this reply helpful?

Sorry this didn't help.

Great! Thanks for your feedback.

How satisfied are you with this reply?

Thanks for your feedback, it helps us improve the site.

How satisfied are you with this reply?

Thanks for your feedback.

Somewhat simpler (and complete for Audrey's purposes):

 

Sub Demo()
With ActiveDocument
  Call InsertOrdinalDate(Date + 1, .Bookmarks("TimeA").Range)
  Call InsertOrdinalDate(Date + 3, .Bookmarks("TimeB").Range)
End With
End Sub

 

Sub InsertOrdinalDate(DtTm As Date, Rng As Range)
With Rng
  .Text = Format(DtTm, "D")
  .Start = .End
  .InsertAfter Ordinal(Format(DtTm, "D"))
  .Font.Superscript = True
  .Start = .End
  .InsertAfter " day of " & Format(DtTm, "MMMM YYYY")
  .Font.Superscript = False
End With
End Sub
 
Function Ordinal(l As Long) As String
Dim strOrd As String
If (l Mod 100) < 11 Or (l Mod 100) > 13 Then strOrd = Choose(l Mod 10, "st", "nd", "rd") & ""
Ordinal = IIf(strOrd = "", "th", strOrd)
End Function

 

Audrey: If your dates are liable to require updating, you'll need to use slightly different code, as the current code, like your own, doesn't update the bookmarks - it merely inserts the dates after them. Consequently, if you were to run the code a second time, you'd get two dates at each location. And, since you appear to be using only the month & day, you may prefer to use a shorter version of InsertOrdinalDate:

 

Sub InsertOrdinalDate(DtTm As Date, Rng As Range)
With Rng
  .Text = Format(DtTm, "MMMM, D")
  .Start = .End
  .InsertAfter Ordinal(Format(DtTm, "D"))
  .Font.Superscript = True
End With
End Sub

Cheers
Paul Edstein
(Fmr MS MVP - Word)

Was this reply helpful?

Sorry this didn't help.

Great! Thanks for your feedback.

How satisfied are you with this reply?

Thanks for your feedback, it helps us improve the site.

How satisfied are you with this reply?

Thanks for your feedback.

Paul,


Like me (sometimes), you seem to continually come up with better and better ways to express your code.  Your mastery of MOD never ceases to amaze.  When I think of it, I think "Oh, that is the mystery that Paul Edstein routinely dazzles me with.


As litle more complicated, but If we introduce a bookmark don' you think it would be better to write to vice at the bookmark?


Sub DemoII()
  With ActiveDocument
    Call InsertOrdinalDate2(Date + 1, .Bookmarks("TimeA"))
    Call InsertOrdinalDate2(Date + 3, .Bookmarks("TimeB"))
  End With
End Sub
 
Sub InsertOrdinalDate2(DtTm As Date, oBm As Bookmark)
Dim lngStart As Long
Dim strName As String
Dim oRng As Range
  strName = oBm.Name
  Set oRng = oBm.Range
  With oRng
    .Text = Format(DtTm, "D")
    lngStart = .Duplicate.Start
    .Start = .End
    .InsertAfter Ordinal(Format(DtTm, "D"))
    .Font.Superscript = True
    .Start = .End
    .InsertAfter " day of " & Format(DtTm, "MMMM YYYY")
    .Font.Superscript = False
    .Start = lngStart
    ActiveDocument.Bookmarks.Add strName, oRng
  End With
End Sub
 
Function Ordinal(l As Long) As String
Dim strOrd As String
If (l Mod 100) < 11 Or (l Mod 100) > 13 Then strOrd = Choose(l Mod 10, "st", "nd", "rd") & ""
Ordinal = IIf(strOrd = "", "th", strOrd)
End Function


Greg Maxey
***
Death smiles at us all, but all a man can do is smile back.


For more help with Word visit:
http://gregmaxey.com/word_tips.html

Was this reply helpful?

Sorry this didn't help.

Great! Thanks for your feedback.

How satisfied are you with this reply?

Thanks for your feedback, it helps us improve the site.

How satisfied are you with this reply?

Thanks for your feedback.

Greg,

 

Thak you so very much, but right now, that one will "noodle" me into the funny farm! I am SO new to VBA that I still have to keep things REALLY simple. So the first step was to get an agreement from the user that she doesn't really need the ordinal. However, she wants to be able to input the date from which to start calculating TimeA and TimeB. The below macro works great but is based on the current date:

 

Selection.GoTo What:=wdGoToBookmark, Name:="TimeA"
Selection.InsertBefore Format((Date + 1), "MMMM d")
Selection.GoTo What:=wdGoToBookmark, Name:="TimeB"
Selection.InsertBefore Format((Date + 3), "MMMM d")
ActiveDocument.Fields.Update

End Sub

 

I have inserted an "ASK" field: {ASK  StartDate " " \d 15  \* MERGEFORMAT}.

 

However, when I tried to work the above VBA code into using the bookmark "StartDate" and adding 1 and then 3 days, I kept getting 1 and 3, even though I bookmarked the default of 15 as "StartDate."

 

Now, what would REALLY be sublime is if you could tell me how I could manage to do that AND get it to insert the ordinal as well. You see, I try to do things that are WAY beyond my level of understanding! I KNOW that it can be done, just don't know how to do it!

 

I went to your website and will bookmark it because there is scads of information there. Thank you for adding to the post here!

 

Audrey

aleeherself, inventive problem solver

Was this reply helpful?

Sorry this didn't help.

Great! Thanks for your feedback.

How satisfied are you with this reply?

Thanks for your feedback, it helps us improve the site.

How satisfied are you with this reply?

Thanks for your feedback.

Paul, sorry that I didn't scroll down far enough to see your post!  Will try this right away, but please see my most recent post to Greg as I have been given new parameters by which to work this task. You folks have been so awesome. :-)

 

Audrey

aleeherself, inventive problem solver

Was this reply helpful?

Sorry this didn't help.

Great! Thanks for your feedback.

How satisfied are you with this reply?

Thanks for your feedback, it helps us improve the site.

How satisfied are you with this reply?

Thanks for your feedback.

Hi Audrey,

 

I See you're using an ASK field to solicit a starting date. As useful as that may be, you can't use it to trigger a macro. For that, you might do better to use a MACROBUTTON field; otherwise, you'll need to manually run the macro after updating the ASK field. And, given the prospect of the macro running multiple times and outputting new dates, you'll need to use code like:

 

Sub Update()
Dim StartDate As Date
StartDate = CDate(InputBox("What is the StartDate?", "StartDate", Format(Date, "MMM D")) & Format(Date, " YYYY"))
With ActiveDocument
  Call InsertOrdinalDate(StartDate + 1, .Bookmarks("TimeA"))
  Call InsertOrdinalDate(StartDate + 3, .Bookmarks("TimeB"))
End With
End Sub
 
Sub InsertOrdinalDate(DtTm As Date, BkMk As Bookmark)
Dim Rng As Range, StrNm As String
Set Rng = BkMk.Range
StrNm = BkMk.Name
With BkMk.Range
  .Text = Format(DtTm, "MMMM, D")
  .Start = .End
  .Text = Ordinal(Format(DtTm, "D"))
  .Font.Superscript = True
  Rng.End = .End
  ActiveDocument.Bookmarks.Add StrNm, Rng
End With
End Sub

 

Function Ordinal(l As Long) As String
Dim strOrd As String
If (l Mod 100) < 11 Or (l Mod 100) > 13 Then strOrd = Choose(l Mod 10, "st", "nd", "rd") & ""
Ordinal = IIf(strOrd = "", "th", strOrd)
End Function

 

With the above, your MACROBUTTON field would call the 'Update' macro. With a bit more tweaking, the code could also update the MACROBUTTON field's display to show, for example, the input date.

Cheers
Paul Edstein
(Fmr MS MVP - Word)

Was this reply helpful?

Sorry this didn't help.

Great! Thanks for your feedback.

How satisfied are you with this reply?

Thanks for your feedback, it helps us improve the site.

How satisfied are you with this reply?

Thanks for your feedback.

* Please try a lower page number.

* Please enter only numbers.

* Please try a lower page number.

* Please enter only numbers.

 
 

Question Info


Last updated October 1, 2021 Views 725 Applies to: