Page 1 of 2 12 LastLast
Results 1 to 15 of 25
  1. #1
    Silver Lounger
    Join Date
    Jun 2001
    Location
    Morden, Surrey, United Kingdom
    Posts
    1,838
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Find String in VB (WinNT/Word97)

    Does anyone know of a sub/procedure/function I could use to find all the Word files in a directory (including subdirectories?) where a particular string exists in a VB component? We're migrating servers and although references to virtual directories will be fine (o:common etc), those with absolute references will need changing (eg fleetvol1...), so I need to find them.

    I've got an XL spreadsheet with a sub that will do this (although not including subdirectories) which I can attach if it will help, but I'm finding it too difficult to convert it to Word!

    Can anyone help, please?
    Beryl M


  2. #2
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Find String in VB (WinNT/Word97)

    You can use Application.FileSearch to find all .doc files in a folder including subfolders:

    Dim i As Long
    Dim doc As Document
    With Application.FileSearch
    .LookIn = "F:"
    .SearchSubFolders = True
    .Execute
    For i = 1 To .FoundFiles.Count
    Set doc = Documents.Open(.FoundFiles(i))
    ' do your stuff here
    doc.Close SaveChanges:=wdSaveChanges
    Next i
    End With

    Added: you need to set a reference to the Microsoft Office 8.0 Object Library for this code to work.

    I assume that your Excel code has a reference to the Visual Basic for Applications Extensibility library. I don't think looping through the modules of a Word document would be very different from looping through the modules in an Excel workbook. Can you indicate where you have problems converting the code, or post the code?

  3. #3
    Silver Lounger
    Join Date
    Jun 2001
    Location
    Morden, Surrey, United Kingdom
    Posts
    1,838
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Re: Find String in VB (WinNT/Word97)

    Hi Hans, yes, I must admit that wasn't the bit I was finding so difficult - it's the bit where it produces a list of the results in a worksheet! I was thinking of a table in Word (I was told it would have to be converted to Word since the code opens the documents to look at them) but my XL is not too strong.

    I'm beginning to think I'd have done better to write it from scratch myself - I understand Word VBA much better than XL - but I'm only a few stages above a newbie even in Word so it would take me too long (I'm running short of time to get it finished!).

    I'm attaching the spreadsheet as requested (I had to remove most of the resulting sheets in order to get it under the 100Mb limit but I left one to show how it displays it) and I would really be grateful if you could show me how to get it organised!

    <img src=/S/grovel.gif border=0 alt=grovel width=31 height=23> <img src=/S/thankyou.gif border=0 alt=thankyou width=40 height=15>
    Attached Files Attached Files
    Beryl M


  4. #4
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Find String in VB (WinNT/Word97)

    Beryl,

    I'm looking at it but it's longish code, so I won't have an answer very soon.

  5. #5
    Silver Lounger
    Join Date
    Jun 2001
    Location
    Morden, Surrey, United Kingdom
    Posts
    1,838
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Re: Find String in VB (WinNT/Word97)

    <img src=/S/sigh.gif border=0 alt=sigh width=15 height=15> Tell me about it! <img src=/S/sigh.gif border=0 alt=sigh width=15 height=15>

    My preferred deadline is end of day Friday (allowing a couple of hours to actually run it on the directories concerned) but if it's not possible by then it's not possible - I'm just grateful for anything you do manage to do!

    <img src=/S/thankyou.gif border=0 alt=thankyou width=40 height=15>
    Beryl M


  6. #6
    Super Moderator jscher2000's Avatar
    Join Date
    Feb 2001
    Location
    Silicon Valley, USA
    Posts
    23,112
    Thanks
    5
    Thanked 93 Times in 89 Posts

    Re: Find String in VB (WinNT/Word97)

    This is a very interesting project, but I'm not very familiar with the Excel object model. To spray data into a table in Word, I would either create that table (at least the heading row and first data row) in the body of the template or save it as AutoText. You then can select the first cell in the second row of the table (below your headings) and use the Selection.TypeText string and Selection.MoveRight wdCell methods to continuously fill in the table. But you knew that. Anyway, that's a lot easier than mimicking the "direct addressing" of cells used in the Excel version.

  7. #7
    Super Moderator
    Join Date
    May 2002
    Location
    Canberra, Australian Capital Territory, Australia
    Posts
    5,055
    Thanks
    2
    Thanked 417 Times in 346 Posts

    Re: Find String in VB (WinNT/Word97)

    Hi Beryl,

    Attached is an old DOS program, with instructions, that might help you out. If you run this from the command prompt and pipe the results out to a file, you'll then have a list of all files containing the string you're after, eg:
    SEARCH [filespec] [string][/P][/C][/b] > FOUND.TXT

    Cheers
    PS: The DOS 'FIND' program could be use to produce similar results, but it only works on one folder/directory at a time.
    Attached Files Attached Files
    Cheers,

    Paul Edstein
    [MS MVP - Word]

  8. #8
    Silver Lounger
    Join Date
    Jun 2001
    Location
    Morden, Surrey, United Kingdom
    Posts
    1,838
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Re: Find String in VB (WinNT/Word97)

    Hi, Jefferson, thanks for the input - the only phrase I have a problem with is "But you knew that."

    I don't think I do ... <img src=/S/confused.gif border=0 alt=confused width=15 height=20>
    Beryl M


  9. #9
    Silver Lounger
    Join Date
    Jun 2001
    Location
    Morden, Surrey, United Kingdom
    Posts
    1,838
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Re: Find String in VB (WinNT/Word97)

    Thanks for the idea, but I'm not sure if a DOS find will find a string in the VBA code? I'll have a look and see what happens ...
    Beryl M


  10. #10
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Find String in VB (WinNT/Word97)

    Beryl,

    The macro in the attached zipped doc will create a Word table with all occurrences of a string in the code modules in Word docs in a folder.

    It is very rough, I paid no attention to formatting at all, so it isn't meant as a finished product, but hopefully, you can use it and adapt it to your needs.
    Attached Files Attached Files

  11. #11
    Silver Lounger
    Join Date
    Jun 2001
    Location
    Morden, Surrey, United Kingdom
    Posts
    1,838
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Re: Find String in VB (WinNT/Word97)

    Hans, you're an angel!! <img src=/S/clapping.gif border=0 alt=clapping width=19 height=23> Bravo! <img src=/S/clapping.gif border=0 alt=clapping width=19 height=23>

    Just a couple of queries -
    <UL><LI>I notice it doesn't pick up the string if it's in ThisDocument - is that not considered to be a VBComponent? And if not, what is it and is it possible to include it?
    <LI>If the document is a mail merge master (regardless of whether there is any code behind it!) Word stops and insists it can't go on - is there a specific switch somewhere that states 'this document is a mail merge master' so I can tell it to ignore these?[/list]I had been trying to work out how to do this while you actually did it and I hadn't got anywhere near it, so thank you, thank you, thank you!!
    Beryl M


  12. #12
    Silver Lounger
    Join Date
    Jun 2001
    Location
    Morden, Surrey, United Kingdom
    Posts
    1,838
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Re: Find String in VB (WinNT/Word97)

    Hans, actually, ignore my last post - I've discovered that

    (1) it does normally look in ThisDocument, because it did so on a run I did - why it didn't on the test I really don't know!
    (2) it's only some mail merge masters that make it fall over - the ones that don't know where their data docs are! And even then, some will just let me hit 'cancel' in the find directory box and will carry on. The ones that don't, I just take the o out of .do* in their name and run it again (it ignores them then).

    Many thanks again!
    Beryl M


  13. #13
    Silver Lounger
    Join Date
    Jun 2001
    Location
    Morden, Surrey, United Kingdom
    Posts
    1,838
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Re: Find String in VB (WinNT/Word97)

    Just thought you might like to know - I set this going on one of my network drives, or at least a small part of it, yesterday lunchtime (once I finally worked out how to get to do it!) - and it decided it was going to do the *entire* network drive! It has been running ever since and the txt file is currently ~35Mb and growing!

    I don't think it's too accurate though - I checked a couple of the documents it stated included the text, and couldn't find it anywhere ...

    Thanks for your help, though!
    Beryl M


  14. #14
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Find String in VB (WinNT/Word97)

    Do you mean that the macro reports finding the search string (FindString) in code while it isn't there? That's strange, I have never seen that happen. But then, I haven't tried searching an entire network drive. <img src=/S/grin.gif border=0 alt=grin width=15 height=15>

    Or do you mean that FileSearch reports too many FoundFiles? That is quite possible, FileSearch is not perfect.

  15. #15
    Silver Lounger
    Join Date
    Jun 2001
    Location
    Morden, Surrey, United Kingdom
    Posts
    1,838
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Re: Find String in VB (WinNT/Word97)

    Well, I didn't actually try to search an entire network drive, either - it just decided it was going to do so!

    I was assuming that the documents listed in the result (of the DOS program Macropod attached, not your VBA doc, by the way) were all the ones it was saying included the search string (fleeet), and that being the case I opened one or two of the docs and they didn't seem to include it.
    Beryl M


Page 1 of 2 12 LastLast

Posting Permissions

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