PDA

View Full Version : Error Message 4605



austenr
06-30-2005, 07:40 AM
While attempting to run the marco below I get the following error on the line bolded in the macro:

4605 This command is not available

Sub replace_client(sPath As String)
Dim oDoc As Word.Document

With Application
Set oDoc = .Documents.Open(sRoot & sPath)

With oDoc.Content.Find
.ClearFormatting
.Text = "Student"
With .Replacement
.ClearFormatting
.Text = "Client"
End With
.Execute Replace:=wdReplaceAll
End With

oDoc.Close True
End With

Set oDoc = Nothing
End Sub


While looking for this error explaination on the net it looks like one of the possibilities is a bad file name. The files I am trying to loop through have special characters in the file names (-). Could this be causing the problem?
If so is there a way to loop through the files and remove the spaces. There are around 300 files all named this way.

In previous tests I was using files with straight data. The files I am going through, have tables, and other forms in them.
Thanks

fumei
06-30-2005, 08:48 AM
Please clarify. Are you stateing that if you do a test run with folders and files, normally named, you do NOT get this error?

Killian
06-30-2005, 08:54 AM
Hiya,

I'm a bit confused... I would expect any issues relating to bad file names to cause an error on the document.Open command, but your error is on the Execute of Find, correct?

Word can be a little tempermental about things sometimes, it might be that "the command is not available" because Word or the target document's window isn't active.

MOS MASTER
06-30-2005, 09:39 AM
Indeed a very strange error.

For the record what version of Word are you running?

Sometimes left over instances of Word can cause this strange error. Close Word and see in taskmanager if an instance of Winword.exe is running.

If it is kill it (Make sure Outlook isn't running as well if Word is default maileditor).
Or Reboot your system and run the code again.

But I'm very interested in the result of the question Gerry asked you. :whistle:

austenr
06-30-2005, 09:44 AM
Well I figured it out. Should have looked at this first. Stupid oversight. :banghead: All the documents were Protected as they were meant to be templates so no one can chaget the format. After unprotecting one works fine. A bit of a pain in the butt because now I have 300+ documents to unprotect, find and replace then protect again. MOS your code from the other day works great. Can you suggest where to dothe unprotect and protect in the code. Thanks

MOS MASTER
06-30-2005, 10:02 AM
Hi Austen, :yes

You're welcome and glad it works fine.

You say you have protected documents but you don't say which type of protection? There are many types.

I've arranged in the code for all Protectiontypes by reading which one applies and then remember it and put it back if the document had protection to begin with.

I do wonder however where the optional parameters are from the sub? Yesterday there where three....

Well I've put them back let it have a spin:
Sub replace_client(sPath As String, sSearch As String, sReplace As String)
Dim oDoc As Word.Document
Dim bProtected As Boolean
Dim lProtect As Long

With Application
Set oDoc = .Documents.Open(sRoot & sPath)
With oDoc
If .ProtectionType <> wdNoProtection Then
bProtected = True
lProtect = .ProtectionType
.Unprotect Password:=""
End If

With .Content.Find
.ClearFormatting
.Text = sSearch
With .Replacement
.ClearFormatting
.Text = sReplace
End With
.Execute Replace:=wdReplaceAll
End With

If bProtected Then
.Protect Type:=lProtect, NoReset:=True, Password:=""
End If
.Close True
End With
End With

Set oDoc = Nothing
End Sub


Later..:whistle:

fumei
06-30-2005, 10:26 AM
If I understand correctly, these are templates. Do you mean real .DOT files? Not .DOC files that are being used as templates.

As Joost asked, what KIND of protection? Are they using formfields? Or are they Document protection?

If they are formfields (and you need forms protection for them to function), I would suggestion having text NOT in the formfields placed in UNprotected sections. That way you can edit them easier.

To expand this, if you have 300+ documents (templates), if possible, you may want to consider consolidating them into fewer documents. Then have a UserForm that determines which larger document is needed, then depending on the logic of the user input, builds a document for them. This can be dome through a removal process (a document that originally starts with five or six document structures, and removes the parts not needed; or a document control document, that inserts parts that are needed.

austenr
06-30-2005, 10:41 AM
Well if you go to Tools>Unprotect Document it will allow you to make the changes. however the areas that you are able to type in are not able to expand then such as a text box. I am doing this for my wife for her work so I am a little fuzzy as to what they did to the whole thing. Oh well, it works as I need it to. Thanks MOS. Solved.

MOS MASTER
06-30-2005, 10:44 AM
You're Welcome! :beerchug: