MS Word 2003 Macro, Styles

I am relatively new to Word macros.  I have created a macro that finds a 
characters, selects severl characters, then changes the style of the 
selection.
I loop through this routine until "Find.Found = False"  When I run the macro 
it will only change every other selection.  If I step through the macro it 
will change every selection.
I am sure that this is an MS bug, but maybe there is a work around.

If I put a break at line 15, then it doesn't skip every other selection.
If I put a break at line 16, then it does.  The selection is executed, but 
the style doesn't get changed.
I have also tried to put in a delay with a 1 to 100000000 for...next loop, 
that doesn't help.

Here is the code, with added line numbers for clarity:

Sub Item_Number_Style()

1  ActiveDocument.Characters(1).Select
2  Selection.MoveLeft unit:=wdCharacter, Count:=1
3  Selection.Find.ClearFormatting

4  With Selection.Find
5      .Text = "["
6      .Replacement.Text = ""
7      .Forward = True
8      .Wrap = wdFindStop
9  End With

10  Selection.Find.Execute

11  Do While Selection.Find.Found = True
    
12      Do Until Selection.Characters.Last.Text = "]"
13          Selection.MoveRight unit:=wdCharacter, Count:=1, Extend:=wdExtend
14      Loop
15      Selection.Style = ActiveDocument.Styles(wdStyleHeading2)
16      Selection.MoveRight unit:=wdCharacter, Count:=1
17      Selection.Find.Execute
    
18  Loop
19  Selection.Find.ClearFormatting

End Sub

-- 
Macro Newbie
0
Utf
12/3/2009 10:15:01 PM
word.application.errors 617 articles. 0 followers. Follow

1 Replies
912 Views

Similar Articles

[PageSpeed] 8

Hi Ken,

I haven't tried to analyze why your macro has the problem you
describe, because even if it worked correctly it would be a very
inefficient way of programming.

In fact, you don't need a macro at all unless this is something that
you repeat on many documents. To do it just once, open the Replace
dialog, click the More button to expand it, and check the Use
Wildcards option. In the Find What box, enter the code

\[[!^13]@\]

In the Replace With box, enter the code

^&

and then click the Format button, choose Style, and select the Heading
2 style from the popup. Finally, click the Replace All button, and
you're done.

If you do want a macro, use this one, which does the same thing:

Sub Item_Number_Style_2()
    Dim oRg As Range
    Set oRg = ActiveDocument.Range
    
    With oRg.Find
        .ClearFormatting
        .Replacement.ClearFormatting
        .Replacement.Style = ActiveDocument.Styles(wdStyleHeading2)
        .Text = "\[[!^13]@\]"
        .Replacement.Text = "^&"
        .Wrap = wdFindContinue
        .Format = True
        .MatchWildcards = True
        .Execute Replace:=wdReplaceAll
    End With
End Sub

Using a Range object instead of the Selection object has a number of
advantages, one being that Word doesn't have to scroll the screen and
move the cursor around, so it's quicker and doesn't lose the user's
place in the document.

See http://www.gmayor.com/replace_using_wildcards.htm and
http://www.word.mvps.org/FAQs/MacrosVBA/ModifyRecordedMacro.htm for
explanations and some additional tips.

--
Regards,
Jay Freedman
Microsoft Word MVP        FAQ: http://word.mvps.org
Email cannot be acknowledged; please post all follow-ups to the
newsgroup so all may benefit.


On Thu, 3 Dec 2009 14:15:01 -0800, KenDude
<KenDude@discussions.microsoft.com> wrote:

>I am relatively new to Word macros.  I have created a macro that finds a 
>characters, selects severl characters, then changes the style of the 
>selection.
>I loop through this routine until "Find.Found = False"  When I run the macro 
>it will only change every other selection.  If I step through the macro it 
>will change every selection.
>I am sure that this is an MS bug, but maybe there is a work around.
>
>If I put a break at line 15, then it doesn't skip every other selection.
>If I put a break at line 16, then it does.  The selection is executed, but 
>the style doesn't get changed.
>I have also tried to put in a delay with a 1 to 100000000 for...next loop, 
>that doesn't help.
>
>Here is the code, with added line numbers for clarity:
>
>Sub Item_Number_Style()
>
>1  ActiveDocument.Characters(1).Select
>2  Selection.MoveLeft unit:=wdCharacter, Count:=1
>3  Selection.Find.ClearFormatting
>
>4  With Selection.Find
>5      .Text = "["
>6      .Replacement.Text = ""
>7      .Forward = True
>8      .Wrap = wdFindStop
>9  End With
>
>10  Selection.Find.Execute
>
>11  Do While Selection.Find.Found = True
>    
>12      Do Until Selection.Characters.Last.Text = "]"
>13          Selection.MoveRight unit:=wdCharacter, Count:=1, Extend:=wdExtend
>14      Loop
>15      Selection.Style = ActiveDocument.Styles(wdStyleHeading2)
>16      Selection.MoveRight unit:=wdCharacter, Count:=1
>17      Selection.Find.Execute
>    
>18  Loop
>19  Selection.Find.ClearFormatting
>
>End Sub
0
Jay
12/4/2009 12:00:28 AM
Reply:

Similar Artilces:

MS Outlook 2007
I am running MS Outlook 2007 on a Vista home lap top. When trying to delete files (or folders) in the Deleted Items folder, I get a message that "the Folder is full" and the items remain. I "right click" on Deleted Items, select "Empty 'Deleted Items' Folder", and I get the usueal warning msg that states "Are you sure you want to permanently delete all the items and subfolders from the 'Deleted Items' folder?" I click on "Yes". I then get a second warning msg that says "The folder is full." The ite...

Links in Outlook 2003 open Save As... dialog box
I'm not sure exactly when this started happening. Whenever I click a link in Outlook 2003 now, it opens the Save As... dialog box as well as the Website connected to the link. It doesn't matter what type of link it is; the dialog always shows up with the .exe file type selected. Any ideas? Does it oen the Save As dialog or does it open the Locate Link Browser dialog? In case of the last direct it to iexplore.exe -- Robert Sparnaaij [MVP-Outlook] www.howto-outlook.com Tips of the month: -Backup and Restore -Create an Office XP CD slipstreamed with Service Pack 3 ----- "S...

Unable To Record Macro Message
I have recorded macros in the past and now and receiving an error message. Background: I assign a keystroke to the macro and begin the macro recorder, access the cell a wanted to write a formula in, hit ENTER, then receive a microsoft GUI with an exclamation point and message "Unable To Record.". The formula writes to the cell, and returns the desired result, so there are no problems with the formula itself. I stop the reocording and the code within the macro just shows the navigation in the worksheet and not the formula. I'm not in design mode. There were no prev...

Exchange 2003 Require a /3GB Switch with More Than 1 Gig RAM
Does Exchange 2003 require the /3GB Switch if you are running Exchange 2003 on Windows Server 2003 Standard Edition. I know for Exchange 2000 they said you needed the switch if you are running Advanced or Datacenter but not Server. Just wondering what they recommend for 2003. 291988 A Description of the 4 GB RAM Tuning Feature and the Physical Address http://support.microsoft.com/?id=291988 Thanks, Fitz Crittle This posting is provided "AS IS" with no warranties, and confers no rights. Please do not send email to this address, post a reply to this newsgroup "Kevin&quo...

Use of MS Dynamics CRM in Utility Billing
Hello Friendz, I was wondering if we can easily use Microsoft Dynamics CRM 3.0 for 'Utility Billing System and Print Presentment'. This also involves use of handheld devices for reading meters and bill presentment. In case you know about a better solution for this requirement, please share that with me. -- Regards, Dave Banerjee Cell: 0060 17331 4676 | email: IfThenElseEndIf@msn.com | IfThenElseEndIf@GMail.com That depends on what you mean by "easily" ;-) CRM out of the box will provide a great, flexible platform to quickly develop a robust business solution. Fro...

migrate 5.5 mailbox to 2003
i'm trying to use migration wizard to migrte mailboxses fro 5.5(NT) to 2003 but i'm getting this error .Please if any one can help Failed to logon to the target mailbox with the address 'x500:/o=GOV/ou=EDUC/cn=Recipients/cn=test23'. Have you used ADC to replicate from 5.5 to 2003? "drabee" <drabee@discussions.microsoft.com> wrote in message news:8E5DCA60-13B9-47BD-B153-D3E3C13E8A23@microsoft.com... > i'm trying to use migration wizard to migrte mailboxses fro 5.5(NT) to > 2003 > but i'm getting this error .Please if any one can help >...

Outlook 2003 Junk E-mail Filter Update: KB832333
http://www.microsoft.com/downloads/details.aspx?FamilyID=ad3699f7-5cc3-4604-a768-32c4d044b630&DisplayLang=en Note: While the update is live, as of this moment the corresponding KB article isn't. Hopefully it'll have some technical detail, but I'm not counting on it. "Milhouse Van Houten" <btvs@myrealbox.com> wrote in message news:ehCzlR6wDHA.2452@tk2msftngp13.phx.gbl... > http://www.microsoft.com/downloads/details.aspx?FamilyID=ad3699f7-5cc3-4604-a768-32c4d044b630&DisplayLang=en > > Note: While the update is live, as of this moment the correspon...

MS Office 97 to MS Office 2007
I have been running Office 97 pro on Windows XP because I do not want to loose the shortcut bar that came with 97. Thing is that I have been thinking about installing the 2007 version of Office that I have had but never installed yet for above reasons. Question is can I install the 2007 version over the 97 version and have it update the programs but keep the shortcut bar?? I know there are probably other shortcut bars or whatever out there but as I asked I would rather keep the 97 one if possible. TIA Microsoft has several Knowledge Base articles on running multiple...

Difference between MS EPM and MS Projects
What is the difference between MS EPM and MS projects Is it just project server and sharepoint added to MS projects together called MS EPM? Yes. EPM is Enterprise Project Management. Microsoft's solution is Project Professional plus Project Server which in turn uses SharePoint technology. -- Rod Gill Microsoft MVP for Project - http://www.project-systems.co.nz Author of the only book on Project VBA, see: http://www.projectvbabook.com "Project Mgt Noob" <Project Mgt Noob @discussions.microsoft.com> wrote in message news:A565E3DF-A7FC-4D2A-B2F1...

MS Exange connection
It deals with Outlook 2003, running under Win XP home: A friend has two ISPs, one with standard POP and SMTP servers, no problem there. The second is a remote MS Exchange server. He connects to both via Internet using ADSL connection. Question: how to configure the Exchange server connection such that it remembers his ID and PW? I could not find anything under just about any thinkable tab in Outlook, email accounts, etc. Any help will be greatly appreciated. -- JanAdam ...

How do I do word count in MS Publisher?
Mike, Publisher doesn't have a word count; the work-around wound be manually count the words or you could try copy the text and paste into Word. -- Don - Publisher 2000� Vancouver, USA "Mike Finn" <Mike Finn@discussions.microsoft.com> wrote in message news:9EBF3083-D7EC-46E9-ADA2-E4616499399E@microsoft.com... > Why not just do a "Edit Story in Microsoft Word" instead of copy and paste? -- JoAnn Paules MVP Microsoft [Publisher] Tech Editor for "Microsoft Publisher 2007 For Dummies" "Don Schmidt" <Don Engineer@PNB.Retired_...

Re: Style sheet gives me an error
"Vikesh Arora" <arorav@whosea.org> wrote in message news:... > Hi > > I have tried running it. And it runs smoothly without giving any > encoding issues. XSL File <?xml version="1.0" ?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" > <xsl:template match="/"> <HTML> <BODY> <TABLE> <xsl:for-each select="CONAME/DipReceived"> <TR> <TD><xsl:value-of select="DateAndTime...

Address Word Letter X for Office 2004?
I've been unable to get the Address Word Letter X script to work on my new version of Office 2004. The Prefs script won't recognize my stationery file (it's grayed out). Did the revisions for scripting in Office 2004 break the script? Is there a workaround? Thanks, Steven Hi Steven, What script is this? Did you check macscripter.net to see if a new version is out? If not, your best bet is probably to go back to the ReadMe on the script and see if you can email the script writer and ask them about it. Or you could try opening up the script looking for anything obviously ...

receiving mails errors on Exchange 2003
Hi, I've installed an MS SBS 2003, I've configured the exchange 2003 to my domain, but the problem is that I can send email's to some domain (there is one domain that I can't send to) but I can't receive from any domain. (MTA, POP3, exchange routing engine, management, exchange system attendant and exchange information store services are running) Can this be a problem of any default security (firewalls) settings that is installed by default with SBS 2003??? if yes, what I need to do? If anyone of you had this problem before or know the resolution of that, I'll be very ...

adding a border to a text box in word
Hi, I'm trying to put a border around a text box, each time I try the border is applied to the whole page. where it says apply to: my only choice is whole document, how can i add apply to text box?? Select the box and look for things about "Outlines" rather than "Borders." (Version of Word?) On Feb 20, 11:38=A0pm, Arch <A...@discussions.microsoft.com> wrote: > Hi, I'm trying to put a border around a text box, each time I try the bor= der > is applied to the whole page. where it says apply to: my only choice is w= hole > document, how ca...

MS Money 2005 Basics
I am new to MS Money after many years with Quicken. Money help offline and online, seems woefully short of detailed explanations o user scenarios. . Searching help for "Epay" yields nothing. Ca someone either direct me to a better help area or answer this In Bills what is the difference between Epay, APay, and Direct debit I see Apay is how i use the bill pay service of my bank. Is direc debit just my manual entry? What then is Epay? What might "Other payment type then be used for? Thanks The difference between them is this Epay is for electronic bill pay. You must have Bi...

SBS 2003 and Outlook 2003 requesting data, freeze
I have two separate clients that are running the new SBS 2003 with XP workstations running Outlook 2003. Both randomly receive the messages: Outlook is requesting data from the server Outlook is trying to retrieve data from the Microsoft Exchange Server Client 1 Running 2 servers that each has a Dual GHz Xeon w/hyperthreading on with 4 gigs of ram. One server is running the SBS 2k3 and the other server is running a Win 2k3 as a terminal server. They are connected on a 1Gig backplane. 10 users total. Client 2 Running 2 servers. One server has a 2.GHz P4 w/hyperthreading on running SBS ...

Outlook 2003 / Exchange 5.5 Prompting Password
Our user and computer accounts are in an AD domain and our Exchange 5.5 Servers are in an NT domain with a two way trust to the AD user domain. Intermittently, Outlook 2003 will prompt for a username and password for the mailbox as though the trust is lost, though users are still able to access file servers in the NT domain which proves that the trust is OK. I have used AD Domains and Trusts to validate that the trust is fine both ways. Most of the time, Outlook 2003 opens fine, but when the prompts start appearing, it seems I need to restart the netlogon service on the AD domain contro...

can I make a empty priv1.edb database in Ech 2003 ??
need help new can I make a empty priv1.edb database in Ech 2003 ?? THANKS Sure you "can", but the question is why? If you want to create a new empty database, then you would dismount the Mailbox store, then delete, move or rename the existing files. Next, simply re-mount the Mailbox store and you will be presented with a message box indicating that new blank databases will be created and do you want to proceed. That's really all there is to it. If you have any mail in the old databases, I'd seriously recommend that you either rename or move them instead of dele...

MS CRM Certification question
Hi all, I have a question regarding the CRM certification? What, if any, certifications are there available with MS CRM? Is there anything like CRM Professional? Have I earned any certification if I passed the Applications Professional exam? T.I.A -- Kuba Skalbania [MCAD] ...

Sent items downloading problem in MS office outlook 2007
Hi all, In my client pc's i have configured the Microsoft office outlook 2007 after the configuration inbox mails are visible in inbox but sent items and other folders are not getting downloaded, my mail server is squirrel mail ,so please let me know the reason . POP3 or IMAP? "xpmate" <sreekanth.chn@gmail.com> wrote in message news:41cb29fe-c615-44b3-aa86-ea3eb607e2a5@g22g2000prf.googlegroups.com... > Hi all, > > In my client pc's i have configured the Microsoft office outlook > 2007 after the configuration inbox mails are visible in in...

Copying an Entire Outlook Email in 2007 (Ctrl-F in Outlook 2003)
I recently upgraded from Office 2003 to Office 2007. When I created an email message in Office 2003, I could hit Ctrl-F and it would copy the entire email and create a duplicate of the original email. In Office 2007, when I hit Ctrl-F the find function opens. Is there any way to copy an existing email and create a duplicate but separate email in Office 2007? The alternative is to create a new email, copy the text in the original email and paste it all into the second email. This is much slower. Using a mail merge is not appropriate because I have to put a unique attached fi...

Outlook 2003 (Creating new email)
I have been creating new email for several months now using Outlook 2003. While I was creating some new rules for emails, I must have changed a setting. Now when I attempt to create a new email, it pulls up a template with wierd fonts, some of my typing doesn't show up on my screen as I am inputting, my signature I had created doesn't show up, and it has a different feel than the before? Any idea what I have done and what I should do to restore my previous settings? ...

Inserting Photos into a blank Publisher 2003 Document
When I go to insert & select a photo to insert, I can NOt see the photo but I can resize it. The graphics manager task pane shows all my pictures as "embedded"!? How can I stop photos from being embedded? They never used to be? Read the fourth FAQ here: http://ed.mvps.org/Static.aspx?=Publisher/FAQs -- Mary Sauer MSFT MVP http://office.microsoft.com/ http://msauer.mvps.org/ news://msnews.microsoft.com "Teresa K" <Teresa K@discussions.microsoft.com> wrote in message news:14E724C7-EA35-496C-A657-3DF6AB723B0C@microsoft.com... > When I go to insert &...

Access 2003 >> Access 2007 (Run-time error 2467)
Hi. I'm trying out how our access 2003 applications work in access 2007. Unfortunately, lots of forms refuse to open. Where the error occures is Form_Current event, which contains something like std = "SELECT ....." Me.Subform_Name![Field Name].RowSource = std The program reports an error: "Run-time error '2467': The expression you entered refers to an object that is closed or doesn't exist." Anyone has any ideas how to fix this? Technically, that shouldn't work in Access 2003 either, but it wasn't quite as sticky about the syntax. To refer t...