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 / April 2005

Tip: Looking for answers? Try searching our database.

VBA question, can't get this macro to work

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
John C Brainard - 29 Apr 2005 13:54 GMT
i can't get the replacement text area to work.  It finds the right
information, but i can't get the insert section break to work.  PLEASE
HELP!!!!

   With Selection.Find
       .Text = "  APR  1-2005           "
       .Replacement.Text = Selection.InsertBreak wdSectionBreakNextPage & "
APR  1-2005           "
       .Forward = True
       .Wrap = wdFindStop
       .Format = False
       .MatchCase = False
       .MatchWholeWord = False
       .MatchWildcards = False
       .MatchSoundsLike = False
       .MatchAllWordForms = False
   End With
Jay Freedman - 29 Apr 2005 20:44 GMT
Hi John,

I'm sorry, but that is just so wrong that it left me shaking my head and
wondering where to start explaining.

In VBA, as in any programming language, there are various kinds of "things".
Part of the language is a set of rules that define how you can put the
"things" together, and any arrangement that doesn't match the rules is
"wrong".

In this case, .Replacement.Text is a String quantity. That is, it's a
sequence of bytes of memory, each of which can hold a character. You can't
put anything in it except strings of characters -- that's one of the rules.

The expression Selection.InsertBreak wdSectionBreakNextPage is a command to
perform an action. It isn't a String -- not even close. Using the & operator
to attach a string to the end of it doesn't help. In fact, I can't even make
Word run this code; it stops with the message "Compile error: Syntax error",
meaning the line doesn't obey the rules of VBA.

Besides that, even if you make the lines agree with the rules, your code
snippet won't do anything because you never called the .Execute method of
the .Find object. It's as if you filled in all the boxes on the Replace
dialog but never clicked any of the buttons to start the search.

In fact, there is no way to do what you're trying to do by a replacement.
Instead, you need to find the text of interest (and I suspect you don't
really *always* want to find the date APR  1-2005, do you? but that's a
separate issue) and then insert the break before it, something like this:

With Selection.Find
  .Text = "  APR  1-2005           "
  .Forward = True
  .Wrap = wdFindStop
  .Format = False
  .MatchCase = False
  .MatchWholeWord = False
  .MatchWildcards = False
  .MatchSoundsLike = False
  .MatchAllWordForms = False

  If .Execute Then
     Selection.Collapse wdCollapseStart
     Selection.InsertBreak wdSectionBreakNextPage
  End If
End With

Signature

Regards,
Jay Freedman
Microsoft Word MVP          FAQ: http://word.mvps.org

> i can't get the replacement text area to work.  It finds the right
> information, but i can't get the insert section break to work.  PLEASE
[quoted text clipped - 13 lines]
>         .MatchAllWordForms = False
>     End With
Helmut Weber - 29 Apr 2005 20:50 GMT
Hi John,

like this, perhaps.

Sub test55555()
ResetSearch
With Selection.Find
  .Text = " und "
  While .Execute
     Selection.Collapse
     Selection.InsertBreak Type:=wdSectionBreakNextPage
     .Execute
     Selection.Collapse direction:=wdCollapseEnd
  Wend
End With
ResetSearch
End Sub

Public Sub ResetSearch()
With Selection.Find
  .ClearFormatting
  .Replacement.ClearFormatting
  .Text = ""
  .Replacement.Text = ""
  .Forward = True
  .Wrap = wdFindContinue
  .Format = False
  .MatchCase = False
  .MatchWholeWord = False
  .MatchWildcards = False
  .MatchSoundsLike = False
  .MatchAllWordForms = False
  ' plus some more if needed
  .Execute
End With
End Sub

You might like to set
  .Wrap = wdFindContinue
to another value.

Greetings from Bavaria, Germany

Helmut Weber, MVP
"red.sys" & chr(64) & "t-online.de"
Word XP, Win 98
http://word.mvps.org/
 
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.