Microsoft Office Forums

Go Back   Microsoft Office Forums > >

 
 
Thread Tools Display Modes
Prev Previous Post   Next Post Next
  #7  
Old 01-12-2022, 11:35 PM
Guessed's Avatar
Guessed Guessed is offline Looking for VBA code that can FIND and SELECT any text based on its properties Windows 10 Looking for VBA code that can FIND and SELECT any text based on its properties Office 2016
Expert
 
Join Date: Mar 2010
Location: Canberra/Melbourne Australia
Posts: 4,176
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

This code is an entire module that you should put into your Excel workbook. You will need to add References to:
Microsoft Scripting Runtime
Microsoft Word x.x Object Library

Then run the Test macro after changing the filepath to point at the folder where your individual Word documents are.

NOTE: The vast majority of this code came from a post on StackOverflow by Ryan
Code:
Option Explicit

Private mobjWordApp As Word.Application

Sub Test()
  ProcessDirectory "C:\temp\"
End Sub

Property Get WordApp() As Word.Application
  If mobjWordApp Is Nothing Then
    Set mobjWordApp = CreateObject("Word.Application")
    mobjWordApp.Visible = True
  End If
  Set WordApp = mobjWordApp
End Property

Sub CloseWordApp()
  If Not (mobjWordApp Is Nothing) Then
    On Error Resume Next
    mobjWordApp.Quit
    Set mobjWordApp = Nothing
  End If
End Sub

Function GetWordDocument(FileName As String) As Word.Document
  On Error Resume Next
  Set GetWordDocument = WordApp.Documents.Open(FileName)
  If Err.Number = &H80010105 Then
    CloseWordApp
    On Error GoTo 0
    Set GetWordDocument = WordApp.Documents.Open(FileName)
  End If
End Function

Sub ProcessDirectory(PathName As String)
  Dim fso As New FileSystemObject, objFile As File
  Dim objFolder As Folder, objWordDoc As Object, aSheet As Worksheet
  Const aSheetName As String = "DocTitles"
  
  On Error Resume Next
    Set aSheet = ActiveWorkbook.Sheets(aSheetName)
  On Error GoTo Err_Handler
  If aSheet Is Nothing Then
    Set aSheet = ActiveWorkbook.Sheets.Add
    aSheet.Name = aSheetName
  End If
  aSheet.Range("A1").Value = "Title"
  aSheet.Range("B1").Value = "Filename"

  Set objFolder = fso.GetFolder(PathName)
  For Each objFile In objFolder.Files
    If objFile.Name Like "*.doc*" Then
      Set objWordDoc = GetWordDocument(objFile.Path)
      ProcessDocument objWordDoc, aSheet
      objWordDoc.Close 0, 1
      Set objWordDoc = Nothing
    End If
  Next

Exit_Handler:
  CloseWordApp
  Exit Sub

Err_Handler:
  MsgBox "Error " & Err.Number & ": " & Err.Description
  Resume Exit_Handler
  'Resume Next ' or as above
End Sub

Sub ProcessDocument(objWordDoc As Document, aSheet As Worksheet)
  Dim aRng As Word.Range, sFound As String, iRow As Integer
  Set aRng = objWordDoc.Content
  With aRng.Find
    .ClearFormatting
    .Style = "Title"
    .Text = ""
    If .Execute = True Then
      sFound = aRng.Text
      iRow = aSheet.UsedRange.Rows.Count + 1
      sFound = Trim(Replace(sFound, vbCr, " ")) 'replace paragraph marks with a space
      aSheet.Cells(iRow, 1).Value = sFound
      aSheet.Cells(iRow, 2).Value = objWordDoc.Name
    End If
  End With
End Sub
I don't think this code actually handles the multiple title paragraphs correctly but your sample doc didn't have any so I didn't bother fixing this. If we loop through your template there are other Title-styled paragraphs which certainly aren't titles.
__________________
Andrew Lockton
Chrysalis Design, Melbourne Australia

Last edited by Guessed; 01-13-2022 at 03:50 PM.
Reply With Quote
 

Tags
macro find text, select text, vba



Similar Threads
Thread Thread Starter Forum Replies Last Post
Macro/VBA code to select ALL text in a textbox in microsoft excel and add a new row jyfuller Excel Programming 11 06-01-2015 08:49 PM
Macro to select an { includepicture } field code and format the picture behind text and 100% scale sanpedro Word VBA 3 03-30-2015 10:50 PM
Microsoft Word macro to find text, select all text between brackets, and delete helal1990 Word VBA 4 02-05-2015 03:52 PM
Looking for VBA code that can FIND and SELECT any text based on its properties VBA code for Microsoft Word macro — select text and insert footnote ndnd Word VBA 10 01-06-2015 01:47 PM
Looking for VBA code that can FIND and SELECT any text based on its properties How to find and select text in a document? mkhuebner Word VBA 8 02-04-2014 08:04 PM

Other Forums: Access Forums

All times are GMT -7. The time now is 06:06 PM.


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