Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 08-18-2022, 01:06 PM
wordfieldcode wordfieldcode is offline Selecting and returning text from a find search of a Word field using Excel VBA Windows 10 Selecting and returning text from a find search of a Word field using Excel VBA Office 2010
Novice
Selecting and returning text from a find search of a Word field using Excel VBA
 
Join Date: Aug 2022
Posts: 3
wordfieldcode is on a distinguished road
Default Selecting and returning text from a find search of a Word field using Excel VBA

Hello,

I am currently trying to iterate through a massive word document with many fields, and to delete fields whose code don't match a specified format, and then to extract a number from each of the remaining fields to check against another file. My problem is that I understand with Selection.Find, you can access the text using Selection.Text. When I use field.code.find, I cannot figure out how to save the matching text to a variable for further work.

I have excluded the previous portions of the code for privacy reasons, but the code works entirely, I just don't know what to change the "tenum =" line to make this work.



Code:
    'first let's open the report
    wrdApp.Documents.Open (reportstr)
    Set analystreport = wrdApp.Documents(reportfile)
    analystname = "D"
    analystreport.SaveAs2 Filename:=reportpath & analystname
    Set anactiwindow = analystreport.ActiveWindow
    'Now let's delete the pages from the end of the expenditures to the end of the book
    pageCount = analystreport.ComputeStatistics(wdStatisticPages)
    anactiwindow.Selection.GoTo What:=wdGoToPage, Which:=wdGoToAbsolute, Count:=421   ''''''insert page after the last expenditure to delete all toward the end of the document
    Set rgepages = anactiwindow.Selection.Range
    anactiwindow.Selection.GoTo What:=wdGoToPage, Which:=wdGoToAbsolute, Count:=pageCount
    rgepages.End = anactiwindow.Selection.Bookmarks("\Page").Range.End
    rgepages.Delete
    anactiwindow.Selection.TypeBackspace
    anactiwindow.Selection.Delete Unit:=wdCharacter, Count:=1
    
    'Now let's get rid of beginning of book up to first expenditure
    anactiwindow.Selection.GoTo What:=wdGoToPage, Which:=wdGoToAbsolute, Count:=1
    Set rgepages = anactiwindow.Selection.Range
    anactiwindow.Selection.GoTo What:=wdGoToPage, Which:=wdGoToAbsolute, Count:=24 ''''''insert page before the first expenditure to delete beginning of book
    rgepages.End = anactiwindow.Selection.Bookmarks("\Page").Range.End
    rgepages.Delete

    'Now we will loop through the fields. If the field is a chapter, delete it, if the field is an expenditure we need to check to see if it is one of the expenditures for this analyst and keep it
    For Each wrdfield In analystreport.Fields
        wrdfield.Select
        With wrdfield.Code.Find
            .MatchWildcards = True
            .text = "[0-9]{1,2}.[0-9]{3}"
            .Execute
            If .Found = True Then
                tenum = wrdfield.Code.text
            Else
                wrdfield.Delete
            End If
        End With
    Next
Attached Images
File Type: png Screenshot 2022-08-18 130421.png (59.1 KB, 9 views)
Reply With Quote
  #2  
Old 08-22-2022, 03:50 PM
Guessed's Avatar
Guessed Guessed is offline Selecting and returning text from a find search of a Word field using Excel VBA Windows 10 Selecting and returning text from a find search of a Word field using Excel VBA Office 2016
Expert
 
Join Date: Mar 2010
Location: Canberra/Melbourne Australia
Posts: 3,932
Guessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant future
Default

Field.Code is a string and refers to the field code not the field result. I think perhaps you are wanting the field result (the text that shows on the page) which is Field.Range.Text

Eg a field like this {Seq Table} would have a Field.Code = "Seq Table" but a Field.Range.Text = "1"

You probably don't need to run a find on a tiny string like this. Perhaps a Like comparison is more efficient.

Working through a loop from the front while deleting some of them is problematic. If you delete an item, all the remaining items move forward one position so the loop then skips an item. You solve this by working backwards through the fields.
Code:
Dim i as integer
For i = analystreport.Fields.Count to 1 Step -1
  If analystreport.Fields(i).Range.Text Like "*[0-9].[0-9]*" then
    tenum = analystreport.Fields(i).Range.Text
  Else
    analystreport.Fields(i).Delete
  End If
Next i
__________________
Andrew Lockton
Chrysalis Design, Melbourne Australia
Reply With Quote
Reply

Thread Tools
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Last Post
Content Controls: Whole field selecting upon clicking vs only placing cursor inside the field nicoledey Word 4 06-16-2018 12:37 AM
Returning text, date, time, location from one Excel sheet to another dave57 Excel 17 06-15-2017 10:45 AM
Selecting and returning text from a find search of a Word field using Excel VBA VBA Code to search for field codes with certain text before the Field code and to change style welcometocandyland Word VBA 4 02-08-2017 06:53 PM
Search cell text from Excel into ms-word ShailShin Word VBA 1 04-21-2014 02:12 AM
Selecting and returning text from a find search of a Word field using Excel VBA Selecting merge field based on whether or not text is present amym Mail Merge 1 12-07-2010 05:14 AM

Other Forums: Access Forums

All times are GMT -7. The time now is 01:49 AM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2024 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft