PDA

View Full Version : Need help about Copy Text from Word To Excel



yanyan9896
10-10-2023, 01:21 AM
Hi everyone,


I have a VBA code that currently allows me to copy the selected text from Word to a designated Excel file. However, I'm struggling to modify the code to copy specific text from Word to Excel instead. I'm not sure how to write the code to achieve this.


Here's the code I have so far:



Sub CopyTextToExcel()


Dim objExcel As Object
Dim objWorkbook As Object
Dim objWorksheet As Object
Dim strText As String
Dim rngDestination As Object

' Create a new Excel application
Set objExcel = CreateObject("Excel.Application")

' Open an existing workbook or create a new one
Set objWorkbook = objExcel.Workbooks.Open("C:\Users\Desktop\Template.xlsx") ' Replace with the actual path

' Set the destination worksheet
Set objWorksheet = objWorkbook.Worksheets("LIST") ' Replace with the actual sheet name

' Set the destination range
Set rngDestination = objWorksheet.Range("A2") ' Replace with the actual cell range

' Get the specific text from Word
strText = ActiveDocument.Range.Text


' Copy the text to Excel
rngDestination.value = strText

' Save and close the workbook
objWorkbook.Save
objWorkbook.Close

' Quit Excel
objExcel.Quit

' Clean up the objects
Set rngDestination = Nothing
Set objWorksheet = Nothing
Set objWorkbook = Nothing
Set objExcel = Nothing

End With

End Sub


I would like to modify the code to copy specific text from Word to Excel based on certain criteria. For example, Font.Name = "Times New Roman", Font.Size = 14, Font.Bold = True.

Could someone please guide me on how to modify the code to achieve this? Any help or suggestions would be greatly appreciated.


Thank you in advance for your assistance!

jdelano
10-13-2023, 11:12 AM
You can use the Font property of Selection object to check the font name, size, and if it is bold. Selection.Font property (Word) | Microsoft Learn (https://learn.microsoft.com/en-us/office/vba/api/word.selection.font)