Macro NOT working as intended in Word 2007

I recorded a macro in Word 2007 but it is not working as intended.

The macro is intended to add  the word Remote at a certain line in the document  then Protect the Word document with a password against any editing and then save it as 'using the same original name' of the open document into a DIFFERENT folder on the same hard drive.  Every document has a unique name that needs to be maintained while 'saved as' to the new folder after protection is added.

The macro works as intended to the extent that it adds the protection to the document but saves it in the same folder where the original copy of the document exists and NOT in the intended folder which is different (C:\Users\pas\Desktop\Uploadable\)

Here is the macro:

Sub ProtectANDSave()
    Selection.MoveDown Unit:=wdLine, Count:=10
    Selection.TypeText Text:="Remote "
    Selection.MoveRight Unit:=wdCharacter, Count:=22
    Selection.MoveLeft Unit:=wdCharacter, Count:=1
    Selection.TypeText Text:="  "
    Selection.Delete Unit:=wdCharacter, Count:=1
    ActiveDocument.Protect Password:="lotus", NoReset:=False, Type:= _
        wdAllowOnlyReading, UseIRM:=False, EnforceStyleLock:=False
        ChangeFileOpenDirectory "C:\Users\pas\Desktop\Uploadable\"
        strName = ActiveDocument.FullName
        ActiveDocument.SaveAs FileName:=strName, _
        FileFormat:=wdFormatRTF, LockComments:=True, Password:="", _
        AddToRecentFiles:=True, WritePassword:="", ReadOnlyRecommended:=False, _
        EmbedTrueTypeFonts:=False, SaveNativePictureFormat:=False, SaveFormsData _
        :=False, SaveAsAOCELetter:=False
        
        ActiveDocument.Close

End Sub

Any help will be greatly appreciated. 

On another note, I have no knowledge of Visual Basic at all.

Matt

Answer
Answer

OK - it was a different type of protection. The following adds the same protection your original macro added and saves as RTF

Sub ProtectANDSave()
Dim strName As String
Const strPath As String = "C:\Users\pas\Desktop\Uploadable\"
Const strPassword As String = "lotus"
    ActiveDocument.Save
    strName = ActiveDocument.name
    strName = Left(strName, InStrRev(strName, Chr(46))) & "rtf"
    With Selection
        .HomeKey wdStory
        .MoveDown Unit:=wdLine, Count:=10
        .TypeText Text:="Remote "
        .MoveRight Unit:=wdCharacter, Count:=22
        .MoveLeft Unit:=wdCharacter, Count:=1
        .TypeText Text:="  "
        .Delete Unit:=wdCharacter, Count:=1
    End With
    ActiveDocument.Protect Password:=strPassword, _
                           NoReset:=False, _
                           Type:=wdAllowOnlyReading, _
                           UseIRM:=False, _
                           EnforceStyleLock:=False
    ActiveDocument.SaveAs _
            Filename:=strPath & strName, _
            FileFormat:=wdFormatRTF
    ActiveDocument.Close 0
End Sub

Graham Mayor (Microsoft Word MVP 2002-2019)
For more Word tips and downloads visit my web site
https://www.gmayor.com/Word_pages.htm

Was this reply helpful?

Sorry this didn't help.

Great! Thanks for your feedback.

How satisfied are you with this reply?

Thanks for your feedback, it helps us improve the site.

How satisfied are you with this reply?

Thanks for your feedback.

 
 

Question Info


Last updated October 5, 2021 Views 199 Applies to: