none
Need to unlock multiple Word 2013 documents, and within them, change one word to another word RRS feed

  • Question

  • Hi, I am not very smart or good at things. I know I have to open up hundreds of Word 2013 documents (.docx), unrestrict them for editing (I have the password for this purpose) and within them, change a word to another word. Can help?

    • Moved by KareninstructorMVP Thursday, December 21, 2017 11:20 PM moved from vb.net forum
    Thursday, December 21, 2017 10:39 PM

All replies

  • Hi lionely,

    you had mentioned that,"your requirement is to open many documents, unprotect it and replace the word."

    for that you can keep all the documents with in one folder and loop through the files in folder.

    this thread was moved from VB.Net forum. so I guess that you are working with VB.NET.

    so below examples are in VB.NET.

    below is an example for looping through files in folder.

    Imports System
    Imports System.IO
    Public Class GetFilesTest
        Public Shared Sub Main()
            ' Make a reference to a directory.
            Dim di As New DirectoryInfo("c:\")
            ' Get a reference to each file in that directory.
            Dim fiArr As FileInfo() = di.GetFiles()
            ' Display the names of the files.
            Dim fri As FileInfo
            For Each fri In fiArr
                Console.WriteLine(fri.Name)
            Next fri
        End Sub 'Main
    End Class 'GetFilesTest

    next step is to open file.

    Private Sub OpenWordDocument()
            Dim objWord As Word.Application
            Dim objDoc As Word.Document
            objWord = CreateObject("Word.Application")
            objDoc = objWord.Documents.Open("D:\Test.docx")
            objWord.Visible = True
    End Sub

    code to unprotect it for editing.

    If ActiveDocument.ProtectionType <> wdNoProtection Then 
     ActiveDocument.Unprotect Password:=strPassword 
    End If

    you can also pass the password as an parameter when you open the file.

    code to find and replace text in word document.

    mports Word = Microsoft.Office.Interop.Word  
    
    Public Class Form1  
     
        ' Find / Replace in Word Document  
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click  
            Dim objWordApp As New Word.Application  
            objWordApp.Visible = True 
     
            'Open an existing document.  
            Dim objDoc As Word.Document = objWordApp.Documents.Open("C:\Trydata\1.doc")  
            objDoc = objWordApp.ActiveDocument  
     
            'Find and replace some text  
            'Replace 'VB' with 'Visual Basic'  
            objDoc.Content.Find.Execute(FindText:="VB", ReplaceWith:="Visual Basic Express", Replace:=Word.WdReplace.wdReplaceAll)  
            While objDoc.Content.Find.Execute(FindText:="  ", Wrap:=Word.WdFindWrap.wdFindContinue)  
                objDoc.Content.Find.Execute(FindText:="  ", ReplaceWith:=" ", Replace:=Word.WdReplace.wdReplaceAll, Wrap:=Word.WdFindWrap.wdFindContinue)  
            End While 
     
            'Save and close the document  
            objDoc.Save()  
            objDoc.Close()  
            objDoc = Nothing 
            objWordApp.Quit()  
            objWordApp = Nothing 
     
        End Sub 
    End Class 

    References:

    How to: Programmatically Open Existing Documents

    Document.Unprotect Method (Word)

    How to: Programmatically Search for and Replace Text in Documents

    further , you just need to understand the code and merge it together and make a single code.

    Regards

    Deepak


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.


    Friday, December 22, 2017 5:05 AM
    Moderator
  • Try:

    Sub UpdateDocuments()
    Application.ScreenUpdating = False
    Dim strFolder As String, strFile As String, strDocNm As String, pState As Variant, wdDoc As Document
    strDocNm = ActiveDocument.FullName: Const strPwd As String = "Password"
    strFolder = GetFolder
    If strFolder = "" Then Exit Sub
    strFile = Dir(strFolder & "\*.doc", vbNormal)
    While strFile <> ""
      If strFolder & "\" & strFile <> strDocNm Then
        Set wdDoc = Documents.Open(FileName:=strFolder & "\" & strFile, AddToRecentFiles:=False, Visible:=False)
        With wdDoc
          pState = False
          If .ProtectionType <> wdNoProtection Then
          pState = .ProtectionType
          .Unprotect strPwd
        End If
          With .Range.Find
            .ClearFormatting
            .Text = "Old String"
            .Replacement.Text = "New String"
            .Forward = True
            .Wrap = wdFindContinue
            .Format = False
            .MatchCase = True
            .MatchWholeWord = True
            .MatchWildcards = False
            .MatchSoundsLike = False
            .MatchAllWordForms = False
            .Execute Replace:=wdReplaceAll
          End With
          If pState <> wdNoProtection Then .Protect Type:=pState, NoReset:=True, Password:=strPwd
          .Close SaveChanges:=True
        End With
      End If
      strFile = Dir()
    Wend
    Set wdDoc = Nothing
    Application.ScreenUpdating = True
    End Sub
    
    Function GetFolder() As String
    Dim oFolder As Object
    GetFolder = ""
    Set oFolder = CreateObject("Shell.Application").BrowseForFolder(0, "Choose a folder", 0)
    If (Not oFolder Is Nothing) Then GetFolder = oFolder.Items.Item.Path
    Set oFolder = Nothing
    End Function

    Cheers
    Paul Edstein
    [MS MVP - Word]

    Saturday, December 23, 2017 3:16 AM
  • Hi lionely,

    is your issue solved?

    I find that you did not follow up this thread after posting the issue.

    if your issue is solved then I suggest you to post your solution and mark it as an answer.

    if your issue is still exist then try to refer the solution given by the me.

    if then also you have any further questions then let me know about it.

    I will try to provide further suggestions to solve the issue.

    Thanks for your understanding.

    Regards

    Deepak


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Thursday, December 28, 2017 8:09 AM
    Moderator