+ Reply to Thread
Results 1 to 2 of 2

Problem changing text in Word using wildcards

  1. #1
    Registered User
    Join Date
    03-05-2013
    Location
    London
    MS-Off Ver
    Excel 2010
    Posts
    9

    Problem changing text in Word using wildcards

    The aim of my macro is to detect the first instance of numbers to 2 decimal places in a Word doc using wildcards, and then to paste a number copied from an Excel spreadsheet to replace that number.

    The original number can be positive or negative and I am struggling with this as it changes the number of characters. My workaround was to assume the original number was negative and then search to see whether the resulting text after I had pasted the new number needed an additional space: the occasions where this was true were 1) "f-" and 2) "f" follwed by any number from 0 to 9.

    For 1) the macro works fine. However for 2) the code finds the text "f[0-9]" using a wildcard, but when I try to replace the text, adding a space, the wildcard is not recognised and it just copies the actual characters, i.e. "f [0-9]"

    This is what I have so far - be grateful for any help

    Please Login or Register  to view this content.
    Sub nexttry()
    'Copy new number from excel spreadsheet
    Worksheets("5945").Range("A1").Offset(1, 5).Copy

    Set appWD = CreateObject("Word.Application.14")
    appWD.Documents.Open "P:\Steve\the rest\automation\word automation\Jan 14 template.doc"
    appWD.Visible = True
    Dim wdDoc As Word.Document
    Set wdDoc = appWD.ActiveDocument
    Dim wdSln As Selection
    Set wdSln = appWD.Selection

    'Find first number to 2 dec places in Word doc and select space before in case it is negative
    Application.ScreenUpdating = False
    With wdSln.Find
    .MatchWildcards = True
    .Text = "?[0-9]{1,}.[0-9]{2}"
    End With

    'Paste new number
    If wdSln.Find.Execute Then
    wdSln.PasteSpecial Placement:=wdInLine, DataType:=wdPasteText
    End If

    'Check if space needed by highlighting new number and preceding word
    wdSln.MoveLeft Unit:=wdCharacter, Count:=6, Extend:=wdExtend

    'Check for "f-"
    With wdSln.Find
    .ClearFormatting
    .Replacement.ClearFormatting
    .Text = "f-"
    .Replacement.Text = "f -"
    .Forward = True
    .Wrap = wdFindContinue
    .Format = False
    .MatchCase = False
    .MatchWholeWord = False
    .MatchAllWordForms = False
    .MatchSoundsLike = False
    .MatchWildcards = True
    End With
    wdSln.Find.Execute Replace:=wdReplaceAll

    'Check for "f" follwed by any number from 0 to 9
    With wdSln.Find
    .Text = "f[0-9]"
    .Replacement.Text = "f [0-9]"
    End With
    wdSln.Find.Execute Replace:=wdReplaceAll

    End Sub
    Please Login or Register  to view this content.

  2. #2
    Forum Expert macropod's Avatar
    Join Date
    12-22-2011
    Location
    Canberra, Australia
    MS-Off Ver
    Word, Excel & Powerpoint 2003 & 2010
    Posts
    3,726

    Re: Problem changing text in Word using wildcards

    You could use a wildcard Find with:
    .Text = "<[\-0-9,]{@}.[0-9]{2}>"
    That should be enough find any positive or negative number with 2 decimal places. Your replacement text is whatever the Excel range holds.

    So:
    Please Login or Register  to view this content.
    PS: When posting code, please use the code tags. They're on the 'Go Advanced' tab.
    Cheers,
    Paul Edstein
    [Fmr MS MVP - Word]

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Print word doc from excel with wildcards on another partition.
    By mightybhwk in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 10-14-2013, 05:00 PM
  2. Having problem with text to column split on a particular word
    By alicia ch in forum Excel Formulas & Functions
    Replies: 6
    Last Post: 11-01-2012, 12:29 PM
  3. 2 Questions Changing Pivot source and wildcards.
    By kennydies in forum Excel General
    Replies: 3
    Last Post: 10-04-2011, 08:37 AM
  4. Make Excel changing text case as easy as in Word!!
    By Altrusan in forum Excel General
    Replies: 0
    Last Post: 01-25-2006, 05:20 PM
  5. problem: changing a text frame's contents from an outside applicat
    By aParks in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 06-01-2005, 10:05 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1