From: johnnykunst on
Thanks for your help- the problem seems to have been caused by having an identical document on the desktop- now I've deleted it the original runs fine.

---
frmsrcurl: http://msgroups.net/microsoft.public.word.vba.general/Code-only-works-fully-when-run-from-VBA-editor
From: johnnykunst on
Turned out to be because I had set the module name the same as the macro it contained.

I then had the problem of the document saving as un recognised format in some instances, but I worked out this was because of illegal characters such as \/|.:?" being entered into the text form field and ending up in the filename, so I inserted an on-exit macro from the fileds to remove the illegal characters. Now works perfectly, macro is:

Sub KeywordsCleanUp()
Dim s As String
s = ActiveDocument.FormFields("Keywords").Result


ActiveDocument.FormFields("Keywords").Result = RemoveIllegalCharacters(s)

End Sub


Which uses the following function:


Function RemoveIllegalCharacters(s As String) As String
Dim e As Variant
e = Array("\", "/", ":", "*", "?", """", "<", ">", ".", "|", "[]", Chr$(13))
Dim temp As String
temp = s
Dim c As Variant
For Each c In e
temp = Replace(temp, c, "")
Next
RemoveIllegalCharacters = temp


End Function


---
frmsrcurl: http://msgroups.net/microsoft.public.word.vba.general/Code-only-works-fully-when-run-from-VBA-editor