Hi guys,
Having a bit of trouble with some code I have been doing to allow the user to load up a word form that will then give them the option to enter a document file location.
The code then checks through the document in the location entered and grabs all of the fonts that are used within that document and populates a drop down list. The same code also does this process for font sizes and also whether there is bold formatted text, italic formatted text.
Once this stage has been complete, which despite a few bugs that I will investigate and perhaps raise here at a later date this stage is completing successfully, the user will then select the font details they wish to locate and click a command button.
This then calls the sub routine, the code of which is shown below.
Private Sub SearchForFont_Click()
Set wrdApp = GetObject(, "Word.Application")
If Err Then
Set wrdApp = New Word.Application
WordWasNotRunning = True
End If
Set FormDoc = ActiveDocument
strFileLocation = FormDoc.FormFields("Document_Name").Result
FontNameSelectedIndex = FormDoc.FormFields("FontName").DropDown.Value
strFontToFind = FormDoc.FormFields("FontName").DropDown.ListEntries(FontNameSelectedIndex).Name
FontSizeSelectedIndex = FormDoc.FormFields("FontSize").DropDown.Value
strFontSizeToFind = FormDoc.FormFields("FontSize").DropDown.ListEntries(FontSizeSelectedIndex).Name
If FormDoc.FormFields("BoldFontTrue").CheckBox.Value = True And FormDoc.FormFields("BoldFontFalse").CheckBox.Value = False Then
strFontBoldIndicator = "True"
End If
If FormDoc.FormFields("BoldFontTrue").CheckBox.Value = False And FormDoc.FormFields("BoldFontFalse").CheckBox.Value = True Then
strFontBoldIndicator = "False"
End If
If FormDoc.FormFields("BoldFontTrue").CheckBox.Value = True And FormDoc.FormFields("BoldFontFalse").CheckBox.Value = True Then
End If
If FormDoc.FormFields("ItalicFontTrue").CheckBox.Value = True And FormDoc.FormFields("ItalicFontFalse").CheckBox.Value = False Then
strFontItalicIndicator = "True"
End If
If FormDoc.FormFields("ItalicFontTrue").CheckBox.Value = False And FormDoc.FormFields("ItalicFontFalse").CheckBox.Value = True Then
strFontItalicIndicator = "False"
End If
If FormDoc.FormFields("ItalicFontTrue").CheckBox.Value = True And FormDoc.FormFields("ItalicFontFalse").CheckBox.Value = True Then
End If
If strFontToFind = "" Then
MsgBox ("Please select the name of the font that you are looking for using the drop-down list on the Font Search Form.")
End If
If strFontSizeToFind = "" Then
MsgBox ("Please select the size of the font that you are looking for using the drop-down list on the Font Search Form.")
End If
Select Case strFontItalicIndicator
Case "True"
FontItalicIndicator = True
Case "False"
FontItalicIndicator = False
Case Else
MsgBox ("Please select one of the checkboxes to indicate whether the font you are looking for is Italic Formatted.")
Exit Sub
End Select
Select Case strFontBoldIndicator
Case "True"
FontBoldIndicator = True
Case "False"
FontBoldIndicator = False
Case Else
MsgBox ("Please select one of the checkboxes to indicate whether the font you are looking for is Bold Formatted.")
Exit Sub
End Select
wrdApp.Documents(strFileLocation).Activate
'Set SearchDoc = ActiveDocument
wrdApp.Documents(strFileLocation).Content.Find.Font.Name = strFontToFind
wrdApp.Documents(strFileLocation).Content.Find.Font.Size = strFontSizeToFind
wrdApp.Documents(strFileLocation).Content.Find.Font.Bold = FontBoldIndicator
wrdApp.Documents(strFileLocation).Content.Find.Font.Italic = FontItalicIndicator
wrdApp.Documents(strFileLocation).Content.Find.Replacement.ClearFormatting
wrdApp.Documents(strFileLocation).Content.Find.Replacement.Highlight = True
With wrdApp.Documents(strFileLocation).Content.Find
.Text = ""
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = True
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
wrdApp.Documents(strFileLocation).Content.Find.Execute Replace:=wdReplaceAll
If wrdApp.Documents(strFileLocation).Content.Find.Found = True Then
MsgBox ("The font has been found and is highlighted in yellow, please remember to use the clear search macro before closing the document to clear the highlighting")
Else
MsgBox ("The font could not be found, if you are sure that this font is in the document please retry the search and make sure that you have selected the intended font format details")
End If
End Sub
This is a modification of how I was originally trying to code this problem and I will cover my initial try in a minute. The problem with the code above is that although it doesnt raise any errors, it also does not find a font regardless of the font details entered. It appears that the >Find.Font.Name, Find.Font.Size, Find.Font.Bold, Find.Font.Italic are never being correctly assigned a value, any ideas on why this would be??
As for the other way I had tried coding this problem, the code for this is shown below and the problem I was having that meant I had made the modification above was that I kept getting a command not available error on the line containing Selection.Find.Execute Replace:=wdReplaceAll. So also best to ask does anyone know why this would error on this statement and also how to correct this.
Selection.Content.Find.Font.Name = strFontToFind
Selection.Content.Find.Font.Size = strFontSizeToFind
Selection.Content.Find.Font.Bold = FontBoldIndicator
Selection.Content.Find.Font.Italic = FontItalicIndicator
Selection.Find.Replacement.ClearFormatting
Selection.Find.Replacement.Highlight = True
With Selection.Find
.Text = ""
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = True
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute Replace:=wdReplaceAll
If Selection.Find.Found = True Then
MsgBox ("The font has been found and is highlighted in yellow, please remember to use the clear search macro before closing the document to clear the highlighting")
Else
MsgBox ("The font could not be found, if you are sure that this font is in the document please retry the search and make sure that you have selected the intended font format details")
End If
Sorry for the super long post, any ideas would be great.
many thanks in advance
Having a bit of trouble with some code I have been doing to allow the user to load up a word form that will then give them the option to enter a document file location.
The code then checks through the document in the location entered and grabs all of the fonts that are used within that document and populates a drop down list. The same code also does this process for font sizes and also whether there is bold formatted text, italic formatted text.
Once this stage has been complete, which despite a few bugs that I will investigate and perhaps raise here at a later date this stage is completing successfully, the user will then select the font details they wish to locate and click a command button.
This then calls the sub routine, the code of which is shown below.
Private Sub SearchForFont_Click()
Set wrdApp = GetObject(, "Word.Application")
If Err Then
Set wrdApp = New Word.Application
WordWasNotRunning = True
End If
Set FormDoc = ActiveDocument
strFileLocation = FormDoc.FormFields("Document_Name").Result
FontNameSelectedIndex = FormDoc.FormFields("FontName").DropDown.Value
strFontToFind = FormDoc.FormFields("FontName").DropDown.ListEntries(FontNameSelectedIndex).Name
FontSizeSelectedIndex = FormDoc.FormFields("FontSize").DropDown.Value
strFontSizeToFind = FormDoc.FormFields("FontSize").DropDown.ListEntries(FontSizeSelectedIndex).Name
If FormDoc.FormFields("BoldFontTrue").CheckBox.Value = True And FormDoc.FormFields("BoldFontFalse").CheckBox.Value = False Then
strFontBoldIndicator = "True"
End If
If FormDoc.FormFields("BoldFontTrue").CheckBox.Value = False And FormDoc.FormFields("BoldFontFalse").CheckBox.Value = True Then
strFontBoldIndicator = "False"
End If
If FormDoc.FormFields("BoldFontTrue").CheckBox.Value = True And FormDoc.FormFields("BoldFontFalse").CheckBox.Value = True Then
End If
If FormDoc.FormFields("ItalicFontTrue").CheckBox.Value = True And FormDoc.FormFields("ItalicFontFalse").CheckBox.Value = False Then
strFontItalicIndicator = "True"
End If
If FormDoc.FormFields("ItalicFontTrue").CheckBox.Value = False And FormDoc.FormFields("ItalicFontFalse").CheckBox.Value = True Then
strFontItalicIndicator = "False"
End If
If FormDoc.FormFields("ItalicFontTrue").CheckBox.Value = True And FormDoc.FormFields("ItalicFontFalse").CheckBox.Value = True Then
End If
If strFontToFind = "" Then
MsgBox ("Please select the name of the font that you are looking for using the drop-down list on the Font Search Form.")
End If
If strFontSizeToFind = "" Then
MsgBox ("Please select the size of the font that you are looking for using the drop-down list on the Font Search Form.")
End If
Select Case strFontItalicIndicator
Case "True"
FontItalicIndicator = True
Case "False"
FontItalicIndicator = False
Case Else
MsgBox ("Please select one of the checkboxes to indicate whether the font you are looking for is Italic Formatted.")
Exit Sub
End Select
Select Case strFontBoldIndicator
Case "True"
FontBoldIndicator = True
Case "False"
FontBoldIndicator = False
Case Else
MsgBox ("Please select one of the checkboxes to indicate whether the font you are looking for is Bold Formatted.")
Exit Sub
End Select
wrdApp.Documents(strFileLocation).Activate
'Set SearchDoc = ActiveDocument
wrdApp.Documents(strFileLocation).Content.Find.Font.Name = strFontToFind
wrdApp.Documents(strFileLocation).Content.Find.Font.Size = strFontSizeToFind
wrdApp.Documents(strFileLocation).Content.Find.Font.Bold = FontBoldIndicator
wrdApp.Documents(strFileLocation).Content.Find.Font.Italic = FontItalicIndicator
wrdApp.Documents(strFileLocation).Content.Find.Replacement.ClearFormatting
wrdApp.Documents(strFileLocation).Content.Find.Replacement.Highlight = True
With wrdApp.Documents(strFileLocation).Content.Find
.Text = ""
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = True
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
wrdApp.Documents(strFileLocation).Content.Find.Execute Replace:=wdReplaceAll
If wrdApp.Documents(strFileLocation).Content.Find.Found = True Then
MsgBox ("The font has been found and is highlighted in yellow, please remember to use the clear search macro before closing the document to clear the highlighting")
Else
MsgBox ("The font could not be found, if you are sure that this font is in the document please retry the search and make sure that you have selected the intended font format details")
End If
End Sub
This is a modification of how I was originally trying to code this problem and I will cover my initial try in a minute. The problem with the code above is that although it doesnt raise any errors, it also does not find a font regardless of the font details entered. It appears that the >Find.Font.Name, Find.Font.Size, Find.Font.Bold, Find.Font.Italic are never being correctly assigned a value, any ideas on why this would be??
As for the other way I had tried coding this problem, the code for this is shown below and the problem I was having that meant I had made the modification above was that I kept getting a command not available error on the line containing Selection.Find.Execute Replace:=wdReplaceAll. So also best to ask does anyone know why this would error on this statement and also how to correct this.
Selection.Content.Find.Font.Name = strFontToFind
Selection.Content.Find.Font.Size = strFontSizeToFind
Selection.Content.Find.Font.Bold = FontBoldIndicator
Selection.Content.Find.Font.Italic = FontItalicIndicator
Selection.Find.Replacement.ClearFormatting
Selection.Find.Replacement.Highlight = True
With Selection.Find
.Text = ""
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = True
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute Replace:=wdReplaceAll
If Selection.Find.Found = True Then
MsgBox ("The font has been found and is highlighted in yellow, please remember to use the clear search macro before closing the document to clear the highlighting")
Else
MsgBox ("The font could not be found, if you are sure that this font is in the document please retry the search and make sure that you have selected the intended font format details")
End If
Sorry for the super long post, any ideas would be great.
many thanks in advance