Fromlostdays1
New Member
- Joined
- Jul 18, 2022
- Messages
- 14
- Office Version
- 365
- Platform
- Windows
I've moderately altered some code I found online for my purposes, as follows.
From Within Excel the code does the follow:
Everything works perfectly. However, what I need to do now is beyond me, and has everything to do with number 6 in that list: "Paste that text into a specific cell in Excel"
Instead of just pasting the text that's selected in Word at this stage, I need Excel to somehow read it and paste not that specific text, but a text I define based on it.
Example
The Search term be "Elevators" and below that in Word either the word "Yes" or the word "No" appears.
So I know I need and If Then statement, but I also need it to, instead of copying "yes" or "no" directly, put the words "Required" if Yes is in Word, or "Not Required" if No is in Word.
This is what I came up with, and remember this is just replacing the last paragraph of code:
' grab and put into excel
But this is not working. Do I need to somehow Dim the highlighted text in Word as a string?
Any help is greatly appreciated.
Thanks!
From Within Excel the code does the follow:
- Use a command button to open a file explorer
- Allow me to select a Word document (manually)
- Open Word Document and Make it Visible
- Automatically Find Specific Text
- Move Cursor below that text and highlight all of the text on that line
- Paste that text into a specific cell in Excel
VBA Code:
Dim FName As String, FD As FileDialog
Dim WApp As Object, WDoc As Object, WDR As Object
Dim ExR As Range
Dim colnum1 As String
colnum1 = Range("H4").Value
Set ExR = Selection ' current location in Excel Sheet
'let's select the WORD doc
Set FD = Application.FileDialog(msoFileDialogOpen)
FD.Show
If FD.SelectedItems.Count <> 0 Then
FName = FD.SelectedItems(1)
Else
Exit Sub
End If
' open Word application and load doc
Set WApp = CreateObject("Word.Application")
WApp.Visible = True
Set WDoc = WApp.Documents.Open(FName)
' go home and search
WApp.Selection.HomeKey Unit:=6
WApp.Selection.Find.ClearFormatting
WApp.Selection.Find.Execute "(SEARCH TERM HERE)"
' move cursor from find to final data item
WApp.Selection.MoveDown Unit:=5, Count:=1
' the miracle happens here
ActiveDocument.Bookmarks("\line").Select
' grab and put into excel
Set WDR = WApp.Selection
Range(colnum1 & "12") = WDR 'place at Excel cursor
End Sub
Everything works perfectly. However, what I need to do now is beyond me, and has everything to do with number 6 in that list: "Paste that text into a specific cell in Excel"
Instead of just pasting the text that's selected in Word at this stage, I need Excel to somehow read it and paste not that specific text, but a text I define based on it.
Example
The Search term be "Elevators" and below that in Word either the word "Yes" or the word "No" appears.
So I know I need and If Then statement, but I also need it to, instead of copying "yes" or "no" directly, put the words "Required" if Yes is in Word, or "Not Required" if No is in Word.
This is what I came up with, and remember this is just replacing the last paragraph of code:
' grab and put into excel
VBA Code:
Set WDR = WApp.Selection
If WDR = Yes Then 'where Yes is the text that has been highlighted in Word
Range(colnum1 & "12") = Required 'place at Excel cursor
End If
If WDR = No Then 'where No is the text that has been highlighted in Word
Range(colnum1 & "12") = Required 'place at Excel cursor
End If
But this is not working. Do I need to somehow Dim the highlighted text in Word as a string?
Any help is greatly appreciated.
Thanks!