I recent was asked the following question in an UtterAccess forum and thought the solution could be useful to someone else down the line.
I have an access database with hyperlinks to many protected word documents (write protection only).
The target is : user enters a search string in access and selects specific protected document.
In the thread I developed 2 possible solutions: (1) Open the document and highlight the search term throughout the document, (2) Open the document and the start the Find dialog and allow the user the control of what they do from that point on.
Open the document and highlight the search term throughout the document
'--------------------------------------------------------------------------------------- ' Procedure : OpenWordDocAndSearch ' Author : Daniel Pineault, CARDA Consultants Inc. ' Website : http://www.cardaconsultants.com ' Purpose : Open the document and highlight the search term throughout the document ' Copyright : The following may be altered and reused as you wish so long as the ' copyright notice is left unchanged (including Author, Website and ' Copyright). It may not be sold/resold or reposted on other sites (links ' back to this site are allowed). ' ' Input Variables: ' ~~~~~~~~~~~~~~~~ ' sFileName : Fully qualified path and filename with extension of the word document ' to search through ' sSearchString : The search term to look for ' ' Usage: ' ~~~~~~ ' OpenWordDocAndSearch "c:\demo\Test.docx", "The" ' ' Revision History: ' Rev Date(yyyy/mm/dd) Description ' ************************************************************************************** ' 1 2013-May-15 Initial Release '--------------------------------------------------------------------------------------- Function OpenWordDocAndSearch(sFileName As String, sSearchString As String) On Error GoTo Error_Handler Dim oApp As Object Dim oDoc As Object Const wdYellow = 7 On Error Resume Next Set oApp = GetObject(, "Word.Application") If Err.Number <> 0 Then 'Word isn't running so start it Set oApp = CreateObject("Word.Application") End If On Error GoTo 0 Set oDoc = oApp.Documents.Open(sFileName) oApp.Visible = True oDoc.Content.Find.HitHighlight FindText:=sSearchString Error_Handler_Exit: On Error Resume Next Set oDoc = Nothing Set oApp = Nothing Exit Function Error_Handler: MsgBox "The following error has occured." & vbCrLf & vbCrLf & _ "Error Number: " & Err.Number & vbCrLf & _ "Error Source: OpenWordDoc" & vbCrLf & _ "Error Description: " & Err.Description, _ vbCritical, "An Error has Occured!" Resume Error_Handler_Exit End Function |
Open the document and the start the Find dialog and allow the user the control of what they do from that point on
'--------------------------------------------------------------------------------------- ' Procedure : OpenWordDocAndSearch ' Author : Daniel Pineault, CARDA Consultants Inc. ' Website : http://www.cardaconsultants.com ' Purpose : Open the document and the start the Find dialog and allow the user the ' control of what they do from that point on ' Copyright : The following may be altered and reused as you wish so long as the ' copyright notice is left unchanged (including Author, Website and ' Copyright). It may not be sold/resold or reposted on other sites (links ' back to this site are allowed). ' ' Input Variables: ' ~~~~~~~~~~~~~~~~ ' sFileName : Fully qualified path and filename with extension of the word document ' to search through ' sSearchString : The search term to look for ' ' Usage: ' ~~~~~~ ' OpenWordDocAndSearch "c:\demo\Test.docx", "The" ' ' Revision History: ' Rev Date(yyyy/mm/dd) Description ' ************************************************************************************** ' 1 2013-May-15 Initial Release '--------------------------------------------------------------------------------------- Function OpenWordDocAndSearch(sFileName As String, sSearchString As String) On Error GoTo Error_Handler Dim oApp As Object Dim oDoc As Object Dim dlgFind As Object Const wdDialogEditFind = 112 On Error Resume Next Set oApp = GetObject(, "Word.Application") If Err.Number <> 0 Then 'Word isn't running so start it Set oApp = CreateObject("Word.Application") End If On Error GoTo 0 Set oDoc = oApp.Documents.Open(sFileName) oApp.Visible = True Set dlgFind = oApp.Dialogs(wdDialogEditFind) With dlgFind .Find = sSearchString .Show End With Error_Handler_Exit: On Error Resume Next Set dlgFind = Nothing Set oDoc = Nothing Set oApp = Nothing Exit Function Error_Handler: MsgBox "The following error has occured." & vbCrLf & vbCrLf & _ "Error Number: " & Err.Number & vbCrLf & _ "Error Source: OpenWordDocAndSearch" & vbCrLf & _ "Error Description: " & Err.Description, _ vbCritical, "An Error has Occured!" Resume Error_Handler_Exit End Function |







