ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Open a Word document from Excel and change its password (https://www.excelbanter.com/excel-programming/347795-open-word-document-excel-change-its-password.html)

RosH

Open a Word document from Excel and change its password
 
Hi everyone,
Could anybody tell me how to open a word document from within an
Excel macro and put a read only password, save and close the document,
return focus back to excel. I have the following codes in mind....

ActiveDocument.Password = "mypassword"
ActiveDocument.save
ActiveDocuemnt.close


Dave Peterson

Open a Word document from Excel and change its password
 
This worked ok for me:

Option Explicit
Sub testme()

'Dim WDApp As Word.Application
'Dim WDDoc As Word.Document
Dim WDApp As Object
Dim WDDoc As Object
Dim myDocName As String
Dim myPWD As String
Dim WordWasRunning As Boolean
Dim testStr As String

myDocName = "C:\my documents\word\doc10.doc"
myPWD = "mypassword"

testStr = ""
On Error Resume Next
testStr = Dir(myDocName)
On Error GoTo 0
If testStr = "" Then
MsgBox "Word file not found!"
Exit Sub
End If

WordWasRunning = True
On Error Resume Next
Set WDApp = GetObject(, "Word.Application")
If Err.Number < 0 Then
Set WDApp = CreateObject("Word.Application")
WordWasRunning = False
End If

WDApp.Visible = True 'at least for testing!

Set WDDoc = WDApp.documents.Open(Filename:=myDocName)

WDDoc.WritePassword = myPWD
WDDoc.Close savechanges:=True

If WordWasRunning Then
'leave it running
Else
WDApp.Quit
End If

Set WDDoc = Nothing
Set WDApp = Nothing

End Sub



RosH wrote:

Hi everyone,
Could anybody tell me how to open a word document from within an
Excel macro and put a read only password, save and close the document,
return focus back to excel. I have the following codes in mind....

ActiveDocument.Password = "mypassword"
ActiveDocument.save
ActiveDocuemnt.close


--

Dave Peterson

Reva

Open a Word document from Excel and change its password
 
I needed some help on this same issue and found this
while browsing. Iused this and it worked GREAT. Thank-you

"Dave Peterson" wrote:

This worked ok for me:

Option Explicit
Sub testme()

'Dim WDApp As Word.Application
'Dim WDDoc As Word.Document
Dim WDApp As Object
Dim WDDoc As Object
Dim myDocName As String
Dim myPWD As String
Dim WordWasRunning As Boolean
Dim testStr As String

myDocName = "C:\my documents\word\doc10.doc"
myPWD = "mypassword"

testStr = ""
On Error Resume Next
testStr = Dir(myDocName)
On Error GoTo 0
If testStr = "" Then
MsgBox "Word file not found!"
Exit Sub
End If

WordWasRunning = True
On Error Resume Next
Set WDApp = GetObject(, "Word.Application")
If Err.Number < 0 Then
Set WDApp = CreateObject("Word.Application")
WordWasRunning = False
End If

WDApp.Visible = True 'at least for testing!

Set WDDoc = WDApp.documents.Open(Filename:=myDocName)

WDDoc.WritePassword = myPWD
WDDoc.Close savechanges:=True

If WordWasRunning Then
'leave it running
Else
WDApp.Quit
End If

Set WDDoc = Nothing
Set WDApp = Nothing

End Sub



RosH wrote:

Hi everyone,
Could anybody tell me how to open a word document from within an
Excel macro and put a read only password, save and close the document,
return focus back to excel. I have the following codes in mind....

ActiveDocument.Password = "mypassword"
ActiveDocument.save
ActiveDocuemnt.close


--

Dave Peterson


Dave Peterson

Open a Word document from Excel and change its password
 
Google is nice!

REVA wrote:

I needed some help on this same issue and found this
while browsing. Iused this and it worked GREAT. Thank-you

"Dave Peterson" wrote:

This worked ok for me:

Option Explicit
Sub testme()

'Dim WDApp As Word.Application
'Dim WDDoc As Word.Document
Dim WDApp As Object
Dim WDDoc As Object
Dim myDocName As String
Dim myPWD As String
Dim WordWasRunning As Boolean
Dim testStr As String

myDocName = "C:\my documents\word\doc10.doc"
myPWD = "mypassword"

testStr = ""
On Error Resume Next
testStr = Dir(myDocName)
On Error GoTo 0
If testStr = "" Then
MsgBox "Word file not found!"
Exit Sub
End If

WordWasRunning = True
On Error Resume Next
Set WDApp = GetObject(, "Word.Application")
If Err.Number < 0 Then
Set WDApp = CreateObject("Word.Application")
WordWasRunning = False
End If

WDApp.Visible = True 'at least for testing!

Set WDDoc = WDApp.documents.Open(Filename:=myDocName)

WDDoc.WritePassword = myPWD
WDDoc.Close savechanges:=True

If WordWasRunning Then
'leave it running
Else
WDApp.Quit
End If

Set WDDoc = Nothing
Set WDApp = Nothing

End Sub



RosH wrote:

Hi everyone,
Could anybody tell me how to open a word document from within an
Excel macro and put a read only password, save and close the document,
return focus back to excel. I have the following codes in mind....

ActiveDocument.Password = "mypassword"
ActiveDocument.save
ActiveDocuemnt.close


--

Dave Peterson


--

Dave Peterson


All times are GMT +1. The time now is 07:27 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com