Search in Header (.DOC) (Please help convert VBA to AHK)

Get help with using AutoHotkey and its commands and hotkeys
ahklearner
Posts: 285
Joined: 23 Jan 2015, 01:49

Search in Header (.DOC) (Please help convert VBA to AHK)

20 Aug 2015, 04:04

Hi Community :wave:

pls help searching in header of word docs (.DOC)

Thanks in advance for your help and support.


http://stackoverflow.com/questions/9186 ... d-document

Code: Select all

Dim rng As Range
Dim intSecCount As Integer
Dim intHFType As Integer
intSecCount = ActiveDocument.Sections.Count
For intSection = 1 To intSecCount
    With ActiveDocument.Sections(intSection)
        For intHFType = 1 To 3
            Set rng = ActiveDocument.Sections(1).Headers(intHFType).Range
            rng.Find.Execute findtext:="This is the text to find", Forward:=True
            If rng.Find.Found = True Then
                Debug.Print "Match" 
            End If
        Next intHFType
    End With
Next intSection

;~ //========== [ ] ==========\\ --------------------------------------------- []

I found the answer on this site and it's a lot more complex than initially thought: http://word.mvps.org/faqs/customization ... ywhere.htm
The following code is from the site above, in addition to searching the entire document it includes text replace functionality:

Code: Select all

Public Sub FindReplaceAnywhere()
    Dim rngStory As Word.Range
    Dim pFindTxt As String
    Dim pReplaceTxt As String
    Dim lngJunk As Long
    Dim oShp As Shape

    pFindTxt = InputBox("Enter the text that you want to find.", "FIND" )

    If pFindTxt = "" Then
        MsgBox "Cancelled by User"
        Exit Sub
    End If

    TryAgain:
        pReplaceTxt = InputBox( "Enter the replacement." , "REPLACE" )

        If pReplaceTxt = "" Then
            If MsgBox( "Do you just want to delete the found text?", vbYesNoCancel) = vbNo Then
                GoTo TryAgain
            ElseIf vbCancel Then
                MsgBox "Cancelled by User."
            Exit Sub
        End If
    End If

    'Fix the skipped blank Header/Footer problem
    lngJunk = ActiveDocument.Sections( 1 ).Headers( 1 ).Range.StoryType

    'Iterate through all story types in the current document
    For Each rngStory In ActiveDocument.StoryRanges

        'Iterate through all linked stories
        Do
            SearchAndReplaceInStory rngStory, pFindTxt, pReplaceTxt
            On Error Resume Next
            Select Case rngStory.StoryType
                Case 6 , 7 , 8 , 9 , 10 , 11
                    If rngStory.ShapeRange.Count > 0 Then
                        For Each oShp In rngStory.ShapeRange
                            If oShp.TextFrame.HasText Then
                                SearchAndReplaceInStory oShp.TextFrame.TextRange, pFindTxt, pReplaceTxt
                            End If
                        Next
                    End If
                Case Else
                    'Do Nothing
                End Select
                On Error GoTo 0

                'Get next linked story (if any)
                Set rngStory = rngStory.NextStoryRange
            Loop Until rngStory Is Nothing
        Next
End Sub

Public Sub SearchAndReplaceInStory(ByVal rngStory As Word.Range, ByVal strSearch As String , ByVal strReplace As String)
    With rngStory.Find
        .ClearFormatting
        .Replacement.ClearFormatting
        .Text = strSearch
        .Replacement.Text = strReplace
        .Wrap = wdFindContinue
        .Execute Replace:=wdReplaceAll
    End With
End Sub
http://word.mvps.org/faqs/customization ... ywhere.htm


;~ //========== [ ] ==========\\ --------------------------------------------- []


this is another example i got, but dont know how to convert to AHK

Code: Select all

Sub SearchHeader()
Dim rng As Range
Dim intSecCount, intSection As Integer
Dim intHFType As Integer
intSecCount = ActiveDocument.Sections.Count
For intSection = 1 To intSecCount
    With ActiveDocument.Sections(intSection)
        For intHFType = 1 To 3
            Set rng = ActiveDocument.Sections(1).Headers(intHFType).Range
            rng.Find.Execute findtext:="Procedure No.", Forward:=True
            If rng.Find.Found = True Then
                Debug.Print "Match"
            End If
        Next intHFType
    End With
Next intSection
End Sub
kon
Posts: 1756
Joined: 29 Sep 2013, 17:11

Re: Search in Header (.DOC) (Please help convert VBA to AHK)

20 Aug 2015, 11:57

Code: Select all

; ==============================================================================================
F7::FindReplaceAnywhere(ComObjActive("Word.Application"))
; ==============================================================================================
; Translation of: http://word.mvps.org/faqs/customization/ReplaceAnywhere.htm
FindReplaceAnywhere(wdApp)
{
  InputBox, pFindTxt, FIND, Enter the text that you want to find.
  if (ErrorLevel)
  {
    MsgBox, Cancelled by User.
    return
  }
  TryAgain:
  InputBox, pReplaceTxt, REPLACE, Enter the replacement.
  if (pReplaceTxt = "")
  {
    MsgBox, 3,, Do you just want to delete the found text?
    IfMsgBox, No
      goto, TryAgain
    else IfMsgBox, Cancel
    {
      MsgBox Cancelled by User.
      return
    }
  }
  ; Fix the skipped blank Header/Footer problem
  lngJunk := wdApp.ActiveDocument.Sections(1).Headers(1).Range.StoryType
  ; Iterate through all story types in the current document
  stryRanges := wdApp.ActiveDocument.StoryRanges
  Loop, % stryRanges.Count
  {
    try rngStory := stryRanges.Item[A_Index]
    catch
      continue
    Loop  ; Iterate through all linked stories
    {
      SearchAndReplaceInStory(rngStory, pFindTxt, pReplaceTxt)
      stryType := rngStory.StoryType
      if stryType in 6,7,8,9,10,11
        Loop, % rngStory.ShapeRange.Count
        {
          oShp := rngStory.ShapeRange.Item[A_Index]
          if (oShp.TextFrame.HasText)
            SearchAndReplaceInStory(oShp.TextFrame.TextRange, pFindTxt, pReplaceTxt)
        }
      if (!rngStory := rngStory.NextStoryRange) ; Get next linked story (if any)
        break
    }
  }
}
; ==============================================================================================
SearchAndReplaceInStory(rngStory, strSearch, strReplace)
{
  static wdFindContinue := 1, wdReplaceAll := 2
  Find := rngStory.Find
  Find.ClearFormatting
  Find.Replacement.ClearFormatting
  Find.Text := strSearch
  Find.Replacement.Text := strReplace
  Find.Wrap := wdFindContinue
  Find.Execute(,,,,,,,,,, wdReplaceAll)
}
; ==============================================================================================
ahklearner
Posts: 285
Joined: 23 Jan 2015, 01:49

Re: Search in Header (.DOC) (Please help convert VBA to AHK)

21 Aug 2015, 00:56


Thanks a lot kon :clap:
you are a lifesaver, as always, :dance:


I actually need it to search an entire folder, working in background without opening word doc
something like: wdApp.Visible := false

need to FileAppend all the A_ScriptFullPath where the text is found.

it would be more beneficial if I could search in a particular line, for a particular word OR statement,
and if could not find it then collect those doc names.

I tried a lot but without any successful.

It would be more beneficial for me if you could show me a way to copy entire word content to Clipboard including header.
and I would do the rest.

something like:
file2read = c:\a.doc
MsgBox % ComObjGet(file2read).Range.Text

FilePath := "c:\c\c.doc"
ComObjGet(FilePath).Range.FormattedText.Copy


Thanks in advance for your help and cooperation.
kon wrote:

Code: Select all

; ==============================================================================================
F7::FindReplaceAnywhere(ComObjActive("Word.Application"))
; ==============================================================================================
; Translation of: http://word.mvps.org/faqs/customization/ReplaceAnywhere.htm
FindReplaceAnywhere(wdApp)
{
  InputBox, pFindTxt, FIND, Enter the text that you want to find.
  if (ErrorLevel)
  {
    MsgBox, Cancelled by User.
    return
  }
  TryAgain:
  InputBox, pReplaceTxt, REPLACE, Enter the replacement.
  if (pReplaceTxt = "")
  {
    MsgBox, 3,, Do you just want to delete the found text?
    IfMsgBox, No
      goto, TryAgain
    else IfMsgBox, Cancel
    {
      MsgBox Cancelled by User.
      return
    }
  }
  ; Fix the skipped blank Header/Footer problem
  lngJunk := wdApp.ActiveDocument.Sections(1).Headers(1).Range.StoryType
  ; Iterate through all story types in the current document
  stryRanges := wdApp.ActiveDocument.StoryRanges
  Loop, % stryRanges.Count
  {
    try rngStory := stryRanges.Item[A_Index]
    catch
      continue
    Loop  ; Iterate through all linked stories
    {
      SearchAndReplaceInStory(rngStory, pFindTxt, pReplaceTxt)
      stryType := rngStory.StoryType
      if stryType in 6,7,8,9,10,11
        Loop, % rngStory.ShapeRange.Count
        {
          oShp := rngStory.ShapeRange.Item[A_Index]
          if (oShp.TextFrame.HasText)
            SearchAndReplaceInStory(oShp.TextFrame.TextRange, pFindTxt, pReplaceTxt)
        }
      if (!rngStory := rngStory.NextStoryRange) ; Get next linked story (if any)
        break
    }
  }
}
; ==============================================================================================
SearchAndReplaceInStory(rngStory, strSearch, strReplace)
{
  static wdFindContinue := 1, wdReplaceAll := 2
  Find := rngStory.Find
  Find.ClearFormatting
  Find.Replacement.ClearFormatting
  Find.Text := strSearch
  Find.Replacement.Text := strReplace
  Find.Wrap := wdFindContinue
  Find.Execute(,,,,,,,,,, wdReplaceAll)
}
; ==============================================================================================

Return to “Ask For Help”

Who is online

Users browsing this forum: Google [Bot], Ph03nix89 and 54 guests