Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 12-17-2014, 12:47 PM
ljd108 ljd108 is offline List specific text in excel Windows Vista List specific text in excel Office 2010 32bit
Novice
List specific text in excel
 
Join Date: Oct 2014
Posts: 24
ljd108 is on a distinguished road
Default List specific text in excel

Hi,



I would like to create a macro that creates a list of all the text which have specific styles in word.

For example, I would like excel columns for 'style 1', 'style 2'... etc and then listed within the rows below the text that is in this style.

Is this possible at all?

Thank you
Reply With Quote
  #2  
Old 12-18-2014, 07:22 PM
macropod's Avatar
macropod macropod is offline List specific text in excel Windows 7 64bit List specific text in excel Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 22,340
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

Try the following code:
Code:
Sub ExportStyles()
Application.ScreenUpdating = True
Dim i As Long, j As Long, StrWkBkNm As String, StrWkSht As String
Dim xlApp As Object, xlWkBk As Object, xlWkSht As Object
Dim StrStyles As String, bStrt As Boolean, bFound As Boolean
StrStyles = StrStyles & ",Style1,Style2,Style3,Style4,Style5,"
StrStyles = StrStyles & "Style6,Style7,Style8,Style9,Style10,"
StrStyles = StrStyles & "Style11,Style12,Style13,Style14,Style15"
StrWkBkNm = "C:\Users\" & Environ("Username") & "\Documents\Styles.xls"
StrWkSht = "Sheet1": bStrt = False: bFound = False
If Dir(StrWkBkNm) = "" Then
  MsgBox "Cannot find the designated workbook: " & StrWkBkNm, vbExclamation
  Exit Sub
End If
' Test whether Excel is already running.
On Error Resume Next
Set xlApp = GetObject(, "Excel.Application")
'Start Excel if it isn't running
If xlApp Is Nothing Then
  Set xlApp = CreateObject("Excel.Application")
  If xlApp Is Nothing Then
    MsgBox "Can't start Excel.", vbExclamation
    GoTo ErrExit
  End If
  bStrt = True
End If
On Error GoTo 0
'Check if the workbook is open.
With xlApp
  For Each xlWkBk In .Workbooks
    If xlWkBk.FullName = StrWkBkNm Then ' It's open
      Set xlWkBk = xlWkBk
      bFound = True
      Exit For
    End If
  Next
  ' If not open by the current user.
  If bFound = False Then
    ' Check if another user has it open.
    If IsFileLocked(StrWkBkNm) = True Then
      ' Report and exit if true
      MsgBox "The Excel workbook is in use." & vbCr & "Please try again later.", vbExclamation, "File in use"
      If bStrt = True Then .Quit
      GoTo ErrExit
    End If
    ' The file is available, so open it.
    Set xlWkBk = .Workbooks.Open(FileName:=StrWkBkNm)
    If xlWkBk Is Nothing Then
      MsgBox "Cannot open:" & vbCr & StrWkBkNm, vbExclamation
      If bStrt = True Then .Quit
      GoTo ErrExit
    End If
  End If
End With
Set xlWkSht = xlWkBk.Worksheets(StrWkSht)
' Output the Styles.
For j = 1 To UBound(Split(StrStyles, ","))
  i = 0
  With ActiveDocument.Range
    With .Find
      .ClearFormatting
      .Format = True
      .Text = ""
      .Replacement.Text = ""
      .Wrap = wdFindStop
      .Style = Split(StrStyles, ",")(j)
      .Execute
    End With
    Do While .Find.Found
      i = i + 1
      xlWkSht.Cells(i, j).Value = .Text
      .Collapse wdCollapseEnd
      .Find.Execute
    Loop
  End With
Next
xlWkBk.Save
xlApp.Visible = True
ErrExit:
' Release Excel object memory
Set xlWkSht = Nothing: Set xlWkBk = Nothing: Set xlApp = Nothing
Application.ScreenUpdating = True
End Sub
 
Function IsFileLocked(strFileName As String) As Boolean
  On Error Resume Next
  Open strFileName For Binary Access Read Write Lock Read Write As #1
  Close #1
  IsFileLocked = Err.Number
  Err.Clear
End Function
Note that the macro assumes an Excel workbook named 'Styles.xls' exists in your Documents folder and that the output goes to Sheet1. You can change the details in the code.
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #3  
Old 12-18-2014, 07:48 PM
ljd108 ljd108 is offline List specific text in excel Windows Vista List specific text in excel Office 2010 32bit
Novice
List specific text in excel
 
Join Date: Oct 2014
Posts: 24
ljd108 is on a distinguished road
Default

That is fantastic thank you.
Reply With Quote
Reply



Similar Threads
Thread Thread Starter Forum Replies Last Post
List specific text in excel How to import list from Excel into drop-down list into word ahw Word VBA 43 02-28-2020 08:11 PM
List specific text in excel copy a specific words to excel list romanticbiro Word VBA 12 12-03-2014 05:12 AM
List specific text in excel Creating a list of all text in a specific style ljd108 Word VBA 15 10-09-2014 02:35 AM
Drop Down List that takes you to specific slides sep324 PowerPoint 1 07-11-2014 11:03 PM
Returning a specific value when item is selected from a drop-down list J Press Excel 4 09-10-2012 06:12 AM

Other Forums: Access Forums

All times are GMT -7. The time now is 10:51 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