[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

Word VBA, Loop through all words in a document

Posted on 2004-10-22
12
Medium Priority
?
3,627 Views
Last Modified: 2012-03-24
Using VBA for Word, how do I loop through all the "Words" in the Active document?

I've used this in Access, but don't know the synatx for Word.

I'm thinking it should look something like this:

Sub Loop()
Dim wd as Word
    for each wd in ActiveDocument
        'Do Something
    Next wd
End Sub

I'm guessing I could use something like "Loop Until End of Document"

but I still nee th correct Word syntax fo a "Word"

I hope I have made myself clear
0
Comment
Question by:Jeffrey Coachman
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 7
  • 5
12 Comments
 
LVL 76

Expert Comment

by:GrahamSkan
ID: 12383485
You're so close

Sub Loop()
Dim wd as Range
    for each wd in ActiveDocument.words
        'Do Something
    Next wd
End Sub
0
 
LVL 76

Expert Comment

by:GrahamSkan
ID: 12383559
Just be aware that the extracted range might include a trailing space or a non-printing character.
0
 
LVL 74

Author Comment

by:Jeffrey Coachman
ID: 12389663
GrahamSkan,

Well... It did not work as I expected.

Let me give you the whole story.

I get a word document every month with scheduling information. The schedule first has the "Day", (ex. 11/20/04) then underneath that day is a Table listing all of the people working that day. This is repeated for the remaining 29 days.

The day is always formatted dd/mm/yy, I would like to see the Full (Long) date. For example: Saturday, November 20 2004.

I would like the code to loop through all words in the document and change all the Short Dates to Full (Long) dates.

I got this code from a previous post a while ago:

If IsDate(Selection.Text) Then
    Selection.Text = Format$(Selection.Text, "dddd, mmmm, dd yyyy")
End If

I tested the code, and it worked fine.

Then I inserted it into your code to get:

Sub Loop()
Dim wd as Range
    For each wd in ActiveDocument.words
        If IsDate(Selection.Text) Then
            Selection.Text = Format$(Selection.Text, "dddd, mmmm, dd yyyy")
        End If
    Next wd
End Sub

It did not work as expected.

I then substituted "wd" for "Selection.Text" and it still did not work.

Any ideas?

Thanks

0
On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

 
LVL 76

Expert Comment

by:GrahamSkan
ID: 12391372
Yes Word will split a date like that into five words.: 24, /, 10, /, 04
0
 
LVL 76

Expert Comment

by:GrahamSkan
ID: 12391535
Try this:

Sub FormatDates()
Dim rng As Range

Set rng = ActiveDocument.Range
    Do While True
        With rng.Find
            .Text = "[0-9]{1,2}/[0-9]{1,2}/[0-9]{1,2}"
            .Forward = True
            .Format = True
            .MatchWildcards = True
            .Execute
            If .Found = False Then
                Exit Sub
            End If
        End With
        With rng
            If IsDate(.Text) Then
                .Text = Format$(.Text, "dddd, mmmm, dd yyyy")
            End If
        .Collapse wdCollapseEnd
        .End = ActiveDocument.Range.End
        End With
    Loop
End Sub
0
 
LVL 74

Author Comment

by:Jeffrey Coachman
ID: 12394286
GrahamSkan,

Good news and bad news.

The code works, except that I mistakenly gave you the incorrect original date format. The date format in my schedule is 10/25/2005 (mm,dd,yyyy) , not 10/25/04 (mm,dd,yy), sorry.

So when I ran your code it put the characters "20" in front of all the years like this:
Monday, October 25, 202004 (even if the year was 1997, it would show "201997")

So after several attempts at modifying your code, I just simply changed the date format to "yy", and it looks like what I want.

If you notice, because of your help, I have doubled the points of this question.

I would like to know:

Why did it put the "20" in front of all the years?
Was my code modification acceptable?
Can you "lightly" comment the code, please?
And ultimately: Why does Word NOT provide this functionality? You can do WITHOUT VBA in Excel and Access, but not in Word? I looked all over the web and could not find a solution! Am I the only person on the Planet who needs to reformat dates in Word???

(OK, so the last question was really a rant)

Thanks a million (I mean 250!)

:)
0
 
LVL 76

Expert Comment

by:GrahamSkan
ID: 12394521
Q1
Yes that would be expected. The find stops at what is, in fact the century digits, but the date conversion, using only two digits, interprets them as 2020 or 2019. The other two digits are left in place.

Q2
You haven't actually shown your modification, but if it works, I guess you've got the idea.

Q3

Sub FormatDates()
Dim rng As Range

Set rng = ActiveDocument.Range
    Do While True
        With rng.Find
       'in the following find string:
        '[0-9] any character from 0 to 9
       '{1,2} one or two of those characters
        '/ the "/" character          
        .Text = "[0-9]{1,2}/[0-9]{1,2}/[0-9]{1,2}"


            .Forward = True
            .Format = True
            .MatchWildcards = True 'the find string is not plain text, but uses wildcards
            .Execute
            If .Found = False Then
                Exit Sub
            End If
        End With
        With rng
            If IsDate(.Text) Then
                .Text = Format$(.Text, "dddd, mmmm, dd yyyy")
            End If
          'change range to current position to the end of the document
        .Collapse wdCollapseEnd
        .End = ActiveDocument.Range.End
        End With
    Loop
End Sub

Q4
Access has a field or column type. A date is stored as a number. It can be displayed as the user requires. Changing the format does not change the underlying data. Excel is in a similar position.

In Word, everything is text, and it is not divided up into cells/fields, and while you can put a date in using some special fields, dates are mostly undistinguished.
0
 
LVL 74

Author Comment

by:Jeffrey Coachman
ID: 12396452
GrahamSkan,

I also noticed that the full day ("dddd") is off by one. For example 10/25/2004 is a Monday (Monday, October 25, 2004). The code shows it as a Sunday (Sunday, October 25, 2004). It's OK when it is formatted as "dd" (25), but when changed to "ddd" it displays "Sun". When changed to "dddd" it displays "Sunday". Is there any way to fix this? (add 1 to the first "dddd"?)

BTW, here is my code, I just changed your format form "dddd, mmmm dd, yyyy" to  "dddd, mmmm dd, yy" to get rid of the Century.

Sub FormatDates7()
Dim rng As Range

Set rng = ActiveDocument.Range
    Do While True
        With rng.Find
       'in the following find string:
        '[0-9] any character from 0 to 9
       '{1,2} one or two of those characters
        '/ the "/" character
        .Text = "[0-9]{1,2}/[0-9]{1,2}/[0-9]{1,2}"


            .Forward = True
            .Format = True
            .MatchWildcards = True 'the find string is not plain text, but uses wildcards
            .Execute
            If .Found = False Then
                Exit Sub
            End If
        End With
        With rng
            If IsDate(.Text) Then
                .Text = Format$(.Text, "dddd, mmmm, dd yy")
            End If
          'change range to current position to the end of the document
        .Collapse wdCollapseEnd
        .End = ActiveDocument.Range.End
        End With
    Loop
End Sub


P.S. Points bumped to 350
0
 
LVL 76

Accepted Solution

by:
GrahamSkan earned 2000 total points
ID: 12401541
Done it again. Typed it all in a few hours ago. Come back to check & find that I haven't submitted it.

I don't know why your weekday name is one day out. To get around you could do this.

.Text = Format$(DateAdd("d", 1, .Text), "dddd, ") & Format$(.Text, "mmmm, dd yy")
0
 
LVL 74

Author Comment

by:Jeffrey Coachman
ID: 12416994
GrahamSkan,

Thanks, works fine now.

500 points!
0
 
LVL 76

Expert Comment

by:GrahamSkan
ID: 12419039
Thanks for the extra points, and the grade.

I wasn't explicit about it, but I'm still a bit bothered about the need to adjust the day name. Be aware that a system change could correct the problem and your program would then be wrong.
You might like to test the result for a known day before actually doing the DataAdd bit:

If Format$(#10/27/2004#, "dddd") = "Tuesday" Then
    .Text = Format$(DateAdd("d", 1, .Text), "dddd, ") & Format$(.Text, "mmmm, dd yy")
Else
    .Text = Format$(.Text, "dddd, mmmm, dd yy")
End If

Cheers, Graham
0
 
LVL 74

Author Comment

by:Jeffrey Coachman
ID: 12421415
GrahamSkan  ,

Funny thing though, when I used the modified code (add 1 day) it works fine for the current schedule. But when I entered my birthday (11/20/1964) it was 2 days off!

Instead of bugging you about it. I am running my own tests (Changing the first day of the week and year of the Format)

I also generated a file in Excel with sequential dates from 01/01/1960 to 12/31/2010, the first column is the date the second column is the date formatted for full date.

I'll copy this range and pasted it into a word doc and run the code.  and compare the Full dates. Perhaps I will see some sort of trend?

You have helped me out a lot here so I did not want to bother you.

Thanks agailn, I'll try to keep you posted
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Shortcuts in Word Just the other day I had a training for Microsoft and they wanted me to show how well the new Windows and Office behaved on a touch device, which by the way is great, but it was only then that I realized that using keyboard shortc…
Microsoft Word is a program we have all encountered at some point, but very few of us have dug deep into its full scope of features, let alone customized it to suit our needs. Luckily making the ribbon (aka toolbar, first introduced in Word 2007) wo…
This video teaches the viewer how to align pictures around text while keeping the text properly aligned in the document.
This Experts Exchange video Micro Tutorial shows how to tell Microsoft Office that a word is NOT spelled correctly. Microsoft Office has a built-in, main dictionary that is shared by Office apps, including Excel, Outlook, PowerPoint, and Word. When …

656 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question