Consulting

Results 1 to 2 of 2

Thread: Protecting Word document from Excel

  1. #1

    Protecting Word document from Excel

    Hi

    I have a macro in Excel, that is replacing values in Word with data stored in Worksheet. Then this changed Word is saved as new document. What is struggle is a command in VBA in Excel putting a password on Word document restricting it from edition.

    Code i have now is as below:
    Option Explicit
    
    Public Sub INDEMNITY()
        Dim ws As Worksheet, msWord As Object
        Dim wbA As Workbook
        Dim strPath As String
        Dim itm As Range
        Dim customer_name As String
        
        
        Set wbA = ActiveWorkbook
        
        strPath = wbA.Path
        If strPath = "" Then
        strPath = Application.DefaultFilePath
        End If
        strPath = strPath & "\"
    
    
        Set ws = ActiveSheet
        Set msWord = CreateObject("Word.Application")
        customer_name = Range("f10").Value
    
    
        With msWord
            .Visible = True
            .Documents.Open strPath & "E-Commerce_Indemnity.docx"
            .Activate
    
    
            With .ActiveDocument.Content.Find
                .ClearFormatting
                .Replacement.ClearFormatting
    
    
            For Each itm In ws.UsedRange.Columns("A").Cells
             .Text = itm.Value2                          'Find all strings in col A
    
    
                    .Replacement.Text = itm.Offset(, 1).Value2  'Replacements from col B
    
    
                    .MatchCase = False
                    .MatchWholeWord = False
    
    
                    .Execute Replace:=2     'wdReplaceAll (WdReplace Enumeration)
                Next
                
            End With
            
            With msWord
            .ActiveDocument.SaveAs Filename:=strPath & customer_name & "_E-commerce_Indemnity_letter.docx"
            
            .Quit
            End With
    
    
            msWord.DisplayAlerts = True
      
        End With
    End Sub
    I was trying command
    Application.ActiveDocument.Protect wdAllowOnlyRevisions, password:="password"
    But returns me error that Variable is not defined, for wdAllowOnlyRevisions

  2. #2
    VBAX Guru Kenneth Hobs's Avatar
    Joined
    Nov 2005
    Location
    Tecumseh, OK
    Posts
    4,956
    Location
    When using late binding, object constant/enumeration values have to be set as you did elsewhere.

    wdAllowOnlyRevisions = 0
    OF course Application.ActivieDocument means Word Application, not Excel. Use:
    msWord.ActiveDocument.Protect 0, password:="password"

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •