Old macro suddenly throwing compile error

J

jmdowning

A macro I have been running for some time (on the same version of
Word--2007--on the same computer), now throws a compile error. I haven't
changed it since Dec-5th-2009, and hadn't used it for a couple of months
(likely since early December).
When I try to run it now, it highlights the "OpenFile" variable in the
following line

OpenFile = Dir$(PathToUse & "*.doc")

and gives the error message "Compile Error: Invalid Use of Property."

I double-checked my backups and previous versions of this macro, and they
all do the same thing now. They are run from "Normal," rather than any
particular word document. The code:


'This will go through all of the Bills of Material and either:
'1: do a search/replace
'2: do a wildcard search
'3: do an ordinary search.
'It will leave all found/modified files open for further changes.
'Last modified on 12/5/08

Public Sub ChangeBOMs()
Dim PathToUse As String
Dim OpenFile As Document
Dim i As Long
Dim wildcards As Boolean
Dim replaceme As String
Dim replacement As String
Dim answer As Integer

'Enter the directory you want to search here.
'Use this directory when testing
'PathToUse = "C:\Documents and Settings\Jenniferd\My Documents\Test\"
PathToUse = "P:\Quality System\Bill of Materials"


'Closes all open documents before beginning
'New 12/04: make sure documents are open before trying to close
'No longer used:
'On Error Resume Next
If Documents.Count > 0 Then
Documents.Close Savechanges:=wdPromptToSaveChanges
End If

'Get the text to be replaced and the replacement
'New 12/04: added ability to cancel out of macro
replaceme = InputBox("Text to be replaced:")
'End if no text entered OR if cancel button pused
If replaceme = "" Then
Exit Sub
End If

replacement = InputBox("Replacement text: (leave blank for search only)")
If replacement = "" Then
'if no text entered -- this is the same as 'cancel' state, unfortunately
answer = MsgBox("Use Wildcard search? Click 'No' to open docs w/standard
search", vbYesNoCancel, "Wildcards?")
If answer = vbCancel Then
Exit Sub
End If
ElseIf answer = vbYes Then wildcards = True
'this is the 'no' state, will do search & only search--no replacemnt, no
wildcards
Else: wildcards = False
End If


OpenFile = Dir$(PathToUse & "*.doc")

'Most likely option: find/replace
If replacement <> "" Then
While OpenFile <> ""

'Open document
Set OpenFile = Documents.Open(PathToUse & OpenFile)

'Find and Replace

With Selection.Find
'Search and replace text here
.Text = replaceme
.replacement.Text = replacement
End With
Selection.Find.Execute Replace:=wdReplaceAll

'close unchanged docs
If Selection.Find.Found = False Then OpenFile.Close

OpenFile = Dir$()
Wend

'Wildcard search, no replacement (too chancy)
ElseIf wildcards = True Then

While OpenFile <> ""

'Open document
Set OpenFile = Documents.Open(PathToUse & OpenFile)

With Selection.Find
.Text = replaceme
.replacement.Text = ""
.MatchWildcards = True
End With
'solves the 'must replace to open files' problem
Selection.Find.Execute

'close unchanged docs
If Selection.Find.Found = False Then OpenFile.Close

OpenFile = Dir$()
Wend

'Search only, no replace, no wildcards
Else

While OpenFile <> ""

'Open document
Set OpenFile = Documents.Open(PathToUse & OpenFile)

With Selection.Find
.Text = replaceme
.replacement.Text = ""
End With
'solves the 'must replace to open files' problem
Selection.Find.Execute

'close unchanged docs
If Selection.Find.Found = False Then OpenFile.Close

OpenFile = Dir$()
Wend

End If
End Sub
 
M

macropod

Hi jmdowning,

There is an error here:
PathToUse = "P:\Quality System\Bill of Materials"

It should read:
PathToUse = "P:\Quality System\Bill of Materials\"
 
J

jmdowning

Hi,

I changed this, but it's still throwing the same error.

--
J. Downing


macropod said:
Hi jmdowning,

There is an error here:
PathToUse = "P:\Quality System\Bill of Materials"

It should read:
PathToUse = "P:\Quality System\Bill of Materials\"

--
Cheers
macropod
[MVP - Microsoft Word]


jmdowning said:
A macro I have been running for some time (on the same version of
Word--2007--on the same computer), now throws a compile error. I haven't
changed it since Dec-5th-2009, and hadn't used it for a couple of months
(likely since early December).
When I try to run it now, it highlights the "OpenFile" variable in the
following line

OpenFile = Dir$(PathToUse & "*.doc")

and gives the error message "Compile Error: Invalid Use of Property."

I double-checked my backups and previous versions of this macro, and they
all do the same thing now. They are run from "Normal," rather than any
particular word document. The code:


'This will go through all of the Bills of Material and either:
'1: do a search/replace
'2: do a wildcard search
'3: do an ordinary search.
'It will leave all found/modified files open for further changes.
'Last modified on 12/5/08

Public Sub ChangeBOMs()
Dim PathToUse As String
Dim OpenFile As Document
Dim i As Long
Dim wildcards As Boolean
Dim replaceme As String
Dim replacement As String
Dim answer As Integer

'Enter the directory you want to search here.
'Use this directory when testing
'PathToUse = "C:\Documents and Settings\Jenniferd\My Documents\Test\"
PathToUse = "P:\Quality System\Bill of Materials"


'Closes all open documents before beginning
'New 12/04: make sure documents are open before trying to close
'No longer used:
'On Error Resume Next
If Documents.Count > 0 Then
Documents.Close Savechanges:=wdPromptToSaveChanges
End If

'Get the text to be replaced and the replacement
'New 12/04: added ability to cancel out of macro
replaceme = InputBox("Text to be replaced:")
'End if no text entered OR if cancel button pused
If replaceme = "" Then
Exit Sub
End If

replacement = InputBox("Replacement text: (leave blank for search only)")
If replacement = "" Then
'if no text entered -- this is the same as 'cancel' state, unfortunately
answer = MsgBox("Use Wildcard search? Click 'No' to open docs w/standard
search", vbYesNoCancel, "Wildcards?")
If answer = vbCancel Then
Exit Sub
End If
ElseIf answer = vbYes Then wildcards = True
'this is the 'no' state, will do search & only search--no replacemnt, no
wildcards
Else: wildcards = False
End If


OpenFile = Dir$(PathToUse & "*.doc")

'Most likely option: find/replace
If replacement <> "" Then
While OpenFile <> ""

'Open document
Set OpenFile = Documents.Open(PathToUse & OpenFile)

'Find and Replace

With Selection.Find
'Search and replace text here
.Text = replaceme
.replacement.Text = replacement
End With
Selection.Find.Execute Replace:=wdReplaceAll

'close unchanged docs
If Selection.Find.Found = False Then OpenFile.Close

OpenFile = Dir$()
Wend

'Wildcard search, no replacement (too chancy)
ElseIf wildcards = True Then

While OpenFile <> ""

'Open document
Set OpenFile = Documents.Open(PathToUse & OpenFile)

With Selection.Find
.Text = replaceme
.replacement.Text = ""
.MatchWildcards = True
End With
'solves the 'must replace to open files' problem
Selection.Find.Execute

'close unchanged docs
If Selection.Find.Found = False Then OpenFile.Close

OpenFile = Dir$()
Wend

'Search only, no replace, no wildcards
Else

While OpenFile <> ""

'Open document
Set OpenFile = Documents.Open(PathToUse & OpenFile)

With Selection.Find
.Text = replaceme
.replacement.Text = ""
End With
'solves the 'must replace to open files' problem
Selection.Find.Execute

'close unchanged docs
If Selection.Find.Found = False Then OpenFile.Close

OpenFile = Dir$()
Wend

End If
End Sub
 
D

Doug Robbins - Word MVP

Is the Path correct?

--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP

jmdowning said:
Hi,

I changed this, but it's still throwing the same error.

--
J. Downing


macropod said:
Hi jmdowning,

There is an error here:
PathToUse = "P:\Quality System\Bill of Materials"

It should read:
PathToUse = "P:\Quality System\Bill of Materials\"

--
Cheers
macropod
[MVP - Microsoft Word]


jmdowning said:
A macro I have been running for some time (on the same version of
Word--2007--on the same computer), now throws a compile error. I
haven't
changed it since Dec-5th-2009, and hadn't used it for a couple of
months
(likely since early December).
When I try to run it now, it highlights the "OpenFile" variable in the
following line

OpenFile = Dir$(PathToUse & "*.doc")

and gives the error message "Compile Error: Invalid Use of Property."

I double-checked my backups and previous versions of this macro, and
they
all do the same thing now. They are run from "Normal," rather than any
particular word document. The code:


'This will go through all of the Bills of Material and either:
'1: do a search/replace
'2: do a wildcard search
'3: do an ordinary search.
'It will leave all found/modified files open for further changes.
'Last modified on 12/5/08

Public Sub ChangeBOMs()
Dim PathToUse As String
Dim OpenFile As Document
Dim i As Long
Dim wildcards As Boolean
Dim replaceme As String
Dim replacement As String
Dim answer As Integer

'Enter the directory you want to search here.
'Use this directory when testing
'PathToUse = "C:\Documents and Settings\Jenniferd\My Documents\Test\"
PathToUse = "P:\Quality System\Bill of Materials"


'Closes all open documents before beginning
'New 12/04: make sure documents are open before trying to close
'No longer used:
'On Error Resume Next
If Documents.Count > 0 Then
Documents.Close Savechanges:=wdPromptToSaveChanges
End If

'Get the text to be replaced and the replacement
'New 12/04: added ability to cancel out of macro
replaceme = InputBox("Text to be replaced:")
'End if no text entered OR if cancel button pused
If replaceme = "" Then
Exit Sub
End If

replacement = InputBox("Replacement text: (leave blank for search
only)")
If replacement = "" Then
'if no text entered -- this is the same as 'cancel' state,
unfortunately
answer = MsgBox("Use Wildcard search? Click 'No' to open docs
w/standard
search", vbYesNoCancel, "Wildcards?")
If answer = vbCancel Then
Exit Sub
End If
ElseIf answer = vbYes Then wildcards = True
'this is the 'no' state, will do search & only search--no
replacemnt, no
wildcards
Else: wildcards = False
End If


OpenFile = Dir$(PathToUse & "*.doc")

'Most likely option: find/replace
If replacement <> "" Then
While OpenFile <> ""

'Open document
Set OpenFile = Documents.Open(PathToUse & OpenFile)

'Find and Replace

With Selection.Find
'Search and replace text here
.Text = replaceme
.replacement.Text = replacement
End With
Selection.Find.Execute Replace:=wdReplaceAll

'close unchanged docs
If Selection.Find.Found = False Then OpenFile.Close

OpenFile = Dir$()
Wend

'Wildcard search, no replacement (too chancy)
ElseIf wildcards = True Then

While OpenFile <> ""

'Open document
Set OpenFile = Documents.Open(PathToUse & OpenFile)

With Selection.Find
.Text = replaceme
.replacement.Text = ""
.MatchWildcards = True
End With
'solves the 'must replace to open files' problem
Selection.Find.Execute

'close unchanged docs
If Selection.Find.Found = False Then OpenFile.Close

OpenFile = Dir$()
Wend

'Search only, no replace, no wildcards
Else

While OpenFile <> ""

'Open document
Set OpenFile = Documents.Open(PathToUse & OpenFile)

With Selection.Find
.Text = replaceme
.replacement.Text = ""
End With
'solves the 'must replace to open files' problem
Selection.Find.Execute

'close unchanged docs
If Selection.Find.Found = False Then OpenFile.Close

OpenFile = Dir$()
Wend

End If
End Sub
 

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

Top