Copying part of a header from active word document and pasting in excel using VBA

Apr 15, 2017
Reaction score
My code is running fine for the first time, but when i close the document and open new one, it's showing error 462,
Sub headeradding()

Dim applWord As Object
Dim docWord As Object
Dim test As String
Dim claim As String

On Error Resume Next
Set applWord = GetObject(, "Word.Application")
If Err.Number <> 0 Then
Set applWord = CreateObject("Word.Application")
End If
On Error GoTo 0

'make the Word window visible:
applWord.Visible = True

'maximize Word window:
'replaced the Word's built-in constant wdWindowStateMaximize with its numerical value 1.
applWord.WindowState = 1

test = ActiveDocument.Sections(1).Headers(wdHeaderFooterPrimary).Range.Text

claim = ActiveCell.Value

openPos = InStr(test, "f")
closepos = InStr(test, claim)
midBit = Mid(test, openPos + 1, closepos - openPos - 1)

MsgBox midBit

docWord.Close SaveChanges:=-1

'quit the word application:

'clear the object variables:
Set docWord = Nothing
Set applWord = Nothing

End Sub

Showing error 91 (sometimes)

Please help
Last edited:

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question