Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 07-18-2024, 01:14 PM
B13 B13 is offline find/replace from excel list to word doc not keeping original sentence case from excel list Windows 10 find/replace from excel list to word doc not keeping original sentence case from excel list Office 2019
Novice
find/replace from excel list to word doc not keeping original sentence case from excel list
 
Join Date: Jul 2024
Posts: 3
B13 is on a distinguished road
Cool find/replace from excel list to word doc not keeping original sentence case from excel list


I have a VBA macro Redirecting that pulls scientific names from an excel sheet (sheet1, range A) and finds/replaces the s.names in the word document with the terms in the excel sheet. The code is supposed to change the font to bold, italic, color, and font type (Times new roman) and this portion works well. What doesn't work is changing the scientific names to sentence case, i.e. hyla cinerea or Hyla Cinerea or some other variant should be Hyla cinerea.

I've tried testing with other functions (e.g., wdUpperCase and wdLowerCase, WdCharacterCase enumeration (Word) | Microsoft Learn) which work, but are not the desired output. wdTitleSentence is what I want but when the code is run, it doesn't throw an error and the other formatting is still applied, but nothing changes with the text case unless it's in all caps, and then it goes to all lowercase. I also considered writing a function similar to some of the suggestions here: [RESOLVED] First letter in capital letter-VBForums but I'm not super savvy with VBA, so not sure if there is another existing function that exists that will accomplish this, or something else needs to occur. I did find Excel VBA Sentence Case Function and Converting to sentence case using VBA as potential help, but not sure how to incorporate this into the existing code. I will mention that the text in the excel has the desired case, so if there's a way to inherit the case from the excel file (and/or other formatting for that matter), that would be great. TIA for the help and I'm a first time poster, so apologies if this isn't correctly formatted.

VBA word code that works except for changing the case:
Sub format_scientific_names()
Dim xlapp As Object
Dim xlbook As Object
Dim xlsheet As Object
Dim myarray As Variant
Dim FD As FileDialog
Dim strSource As String
Dim i As Long, lognum As Long
Set FD = Application.FileDialog(msoFileDialogFilePicker)
With FD
.Title = "Select the workbook that contains the terms to be italicized"
.Filters.Clear
.Filters.Add "Excel Workbooks", "*.xlsx"
.AllowMultiSelect = False
If .Show = -1 Then
strSource = .SelectedItems(1)
Else
MsgBox "You did not select the workbook that contains the data"
Exit Sub
End If
End With
On Error Resume Next
Set xlapp = GetObject(, "Excel.Application")
If Err Then
bstartApp = True
Set xlapp = CreateObject("Excel.Application")
End If
On Error GoTo 0
Set xlbook = xlapp.Workbooks.Open(strSource)
Set xlsheet = xlbook.Worksheets(1)
myarray = xlsheet.Range("A1").CurrentRegion.Value
If bstartApp = True Then
xlapp.Quit
End If
Set xlapp = Nothing
Set xlbook = Nothing
Set xlsheet = Nothing
For i = LBound(myarray) To UBound(myarray)
Selection.HomeKey wdStory
Selection.Find.ClearFormatting
With Selection.Find
Do While .Execute(FindText:=myarray(i, 1), Forward:=True, _
MatchWildcards:=True, Wrap:=wdFindStop, MatchCase:=False) = True
Set rng = Selection.Range
Selection.Collapse wdCollapseEnd
rng.Font.Italic = True
rng.Font.Bold = True
rng.Font.Color = RGB(200,187,0)
rng.Font.Name ="Times New Roman"
rng.Case= wdTitleSentence 'wdUpperCase works; this doesn't; something else I could put here?
Loop
End With
Next i
End Sub
Reply With Quote
  #2  
Old 07-19-2024, 02:40 AM
gmaxey gmaxey is offline find/replace from excel list to word doc not keeping original sentence case from excel list Windows 10 find/replace from excel list to word doc not keeping original sentence case from excel list Office 2019
Expert
 
Join Date: May 2010
Location: Brasstown, NC
Posts: 1,617
gmaxey is just really nicegmaxey is just really nicegmaxey is just really nicegmaxey is just really nicegmaxey is just really nice
Default

Hello,


When you post code, it is helpful to simply post a snippet of the part that doesn't work. That way we don't have to try to recreate your entire situation (create and excel file etc.).


Try:


Code:
Sub format_scientific_names()
Dim oRng As Range
Dim strFind As String
  strFind = "Hyla cinerea"
  Set oRng = ActiveDocument.Range
  With oRng.Find
    .Text = strFind
    .Forward = True
    .Wrap = wdFindStop
    .MatchCase = False
    While .Execute
      oRng.Font.Italic = True
      oRng.Font.Bold = True
      oRng.Font.Color = RGB(200, 187, 0)
      oRng.Font.Name = "Times New Roman"
      oRng.Case = wdLowerCase
      oRng.Words(1).Characters(1).Case = wdUpperCase
      oRng.Collapse wdCollapseEnd
    Wend
  End With
  End Sub

Or you could simply use your Excel file "Find" phrase as the replacement text:


Code:
Sub format_scientific_names()
Dim oRng As Range
Dim strFind As String
  strFind = "Hyla cinerea"
  Set oRng = ActiveDocument.Range
  With oRng.Find
    .Text = strFind
    .Forward = True
    .Wrap = wdFindStop
    .MatchCase = False
    While .Execute
      oRng.Text = strFind
      oRng.Font.Italic = True
      oRng.Font.Bold = True
      oRng.Font.Color = RGB(200, 187, 0)
      oRng.Font.Name = "Times New Roman"
      oRng.Collapse wdCollapseEnd
    Wend
  End With
End Sub
__________________
Greg Maxey
Please visit my web site at http://www.gregmaxey.com/
Reply With Quote
  #3  
Old 07-19-2024, 01:30 PM
B13 B13 is offline find/replace from excel list to word doc not keeping original sentence case from excel list Windows 10 find/replace from excel list to word doc not keeping original sentence case from excel list Office 2019
Novice
find/replace from excel list to word doc not keeping original sentence case from excel list
 
Join Date: Jul 2024
Posts: 3
B13 is on a distinguished road
Default

Hi gmaxey, thanks for the tips on submitting and the speedy replies. I've tried the code you provided with a single scientific name, and it works exactly as expected. Thanks! However, in my lack of VBA knowledge, I'm struggling to incorporate it into the excel array that I call earlier in the code. I'm not quite sure what you mean by 'use your Excel file "Find" phrase as the replacement text'. Could you please elaborate a bit on this? Thanks!
Reply With Quote
  #4  
Old 07-20-2024, 05:46 AM
Italophile Italophile is offline find/replace from excel list to word doc not keeping original sentence case from excel list Windows 11 find/replace from excel list to word doc not keeping original sentence case from excel list Office 2021
Expert
 
Join Date: Mar 2022
Posts: 554
Italophile is just really niceItalophile is just really niceItalophile is just really niceItalophile is just really nice
Default

Have you checked that the array contains the values in the correct format? There is a difference between the Value and Text properties of an Excel Range. Text returns the formatted text whereas Value returns the underlying value. You could try changing:

Code:
myarray = xlsheet.Range("A1").CurrentRegion.Value
To:

Code:
myarray = xlsheet.Range("A1").CurrentRegion.Text
Reply With Quote
  #5  
Old 07-22-2024, 04:22 AM
gmaxey gmaxey is offline find/replace from excel list to word doc not keeping original sentence case from excel list Windows 10 find/replace from excel list to word doc not keeping original sentence case from excel list Office 2019
Expert
 
Join Date: May 2010
Location: Brasstown, NC
Posts: 1,617
gmaxey is just really nicegmaxey is just really nicegmaxey is just really nicegmaxey is just really nicegmaxey is just really nice
Default

Change this section of your original code:


Code:
For i = LBound(myarray) To UBound(myarray)
    Selection.HomeKey wdStory
    Selection.Find.ClearFormatting
    With Selection.Find
    Do While .Execute(FindText:=myarray(i, 1), Forward:=True, _
    MatchWildcards:=True, Wrap:=wdFindStop, MatchCase:=False) = True
    Set rng = Selection.Range
    Selection.Collapse wdCollapseEnd
    rng.Font.Italic = True
    rng.Font.Bold = True
    rng.Font.Color = RGB(200,187,0)
    rng.Font.Name ="Times New Roman"
    rng.Case=  wdTitleSentence 'wdUpperCase works; this doesn't; something else I could put here?
     Loop
Next i

to:


Code:
Dim oRng As Range Dim strFind As String
   For i = LBound(myarray) To UBound(myarray)
    strFind = myarray(i)
    Set oRng = ActiveDocument.Range
    With oRng.Find
      .Text = strFind
      .Forward = True
      .Wrap = wdFindStop
      .MatchCase = False
      While .Execute
         oRng.Font.Italic = True
         oRng.Font.Bold = True
         oRng.Font.Color = RGB(200, 187, 0)
         oRng.Font.Name = "Times New Roman"
         oRng.Case = wdLowerCase 
         oRng.Words(1).Characters(1).Case = wdUpperCase
         oRng.Collapse wdCollapseEnd
      Wend
   End With
   Next i

or the other variation I suggested.
__________________
Greg Maxey
Please visit my web site at http://www.gregmaxey.com/
Reply With Quote
  #6  
Old 07-24-2024, 12:42 PM
B13 B13 is offline find/replace from excel list to word doc not keeping original sentence case from excel list Windows 10 find/replace from excel list to word doc not keeping original sentence case from excel list Office 2019
Novice
find/replace from excel list to word doc not keeping original sentence case from excel list
 
Join Date: Jul 2024
Posts: 3
B13 is on a distinguished road
Smile @gmaxey

This worked! I did make minor tweaks to the code you provided in case someone else is looking at this:

Dim oRng As Range
Dim strFind As String
vs
Dim oRng As Range Dim strFind As String 'on one line

and
strFind = myarray(i, 1)
vs
strFind = myarray(i) 'which returns a subscript out of bounds error

Thanks again, this is a lifesaver and will save a lot of formatting time in the future!
Reply With Quote
Reply

Tags
sentencecase



Similar Threads
Thread Thread Starter Forum Replies Last Post
find/replace from excel list to word doc not keeping original sentence case from excel list Find, Replace using Excel List pushpi004 Word VBA 16 02-20-2024 06:50 AM
Find and Replace from Predetermined list of Text for Both Find and Replace dminor Word VBA 1 08-16-2022 03:40 PM
find/replace from excel list to word doc not keeping original sentence case from excel list How to import list from Excel into drop-down list into word ahw Word VBA 43 02-28-2020 08:11 PM
find/replace from excel list to word doc not keeping original sentence case from excel list Advantages of List in Excel vs List in Word? Peace Freak Excel 3 04-08-2017 06:15 PM
find/replace from excel list to word doc not keeping original sentence case from excel list Having a Drop-down list in Word referring to an Excel list celias Word VBA 3 07-11-2016 11:40 PM

Other Forums: Access Forums

All times are GMT -7. The time now is 04:19 AM.


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