I have different scores in an excel sheet directly extracted from a database that have certain values. I want to extract these scores from excel and be able to change texts in a word document with if statements (e.g. if score in cell (2, 2) > 60 (in excel) = delete/replace a certain text (in word)) with VBA. Does anyone know how I would compile this macro?
The code I tried:
Private Sub CommandButton8_Click()
Dim objExcel As New Excel.Application
Dim exWb As Excel.Workbook
Set exWb = objExcel.Workbooks.Open("C:\Desktop\Testdoc.xlsx")
If score.Cells(2, 2) >= 60 Then
Function FnOpeneWordDoc()
Dim objWord
Dim objDoc
Set objWord = CreateObject("Word.Application")
Set objDoc = objWord.Documents.Open("C:\Desktop\TestDoc.docm")
objWord.Visible = True
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = "text here"
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
End Function
End If
exWb.Close
Set exWb = Nothing
End Function
Edit: New code:
Private Sub CommandButton7_Click()
Dim objExcel As New Excel.Application
Dim exWb As Excel.Workbook
Set exWb = objExcel.Workbooks.Open("C:\UsersDesktop\Testdoc.xlsx")
If exWb.Sheets(1).Cells(2, 2).Value >= 60 Then Run [BLA]
If Not exWb.Sheets(1).Cells(2, 2).Value >= 60 Then Run [BLA2]
End If
exWb.Close
Set exWb = Nothing
End Sub
Function BLA()
With Selection.Find
.ClearFormatting
.Text = "Old text"
.Replacement.ClearFormatting
.Replacement.Text = ""
.Execute Replace:=wdReplaceAll, Forward:=True, _
Wrap:=wdFindContinue
End With
End Function
Function BLA2()
With Selection.Find
.ClearFormatting
.Text = "Old text"
.Replacement.ClearFormatting
.Replacement.Text = "Old text"
.Execute Replace:=wdReplaceAll, Forward:=True, _
Wrap:=wdFindContinue
End With
End Function