Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 40
Default problem protecting word document by macro in excel

I keep a log of rfi's(requests for information) in excel. when i
answer them i open up a document from a template in word. i have been
working on automating this process. so far i have gotten to the point
where i can open a document in word, unprotect it, change some of it's
formfields and bookmarks. but now i am trying to reprotect it. So
here's my code:

Sub OpenTemplate(DocType As String) 'in this case it is "RFI Response"

Dim WordApp As Object
Set WordApp = Nothing
Dim worddoc As Object
Dim TemplateFile As String
Dim TemplateDir As String

'is this code doing what i want it to do, namely checking if there
is an open copy of MS
'word
On Error Resume Next
Set WordApp = GetObject("word.application")
If Err.Number < 0 Then
Set WordApp = CreateObject("word.application")
End If
TemplateDir = "C:\Documents and Settings\My Documents\forms\"
TemplateFile = TemplateDir & DocType & ".dot"
WordApp.Documents.Add Template:=TemplateFile
Set worddoc = WordApp.activedocument

If DocType = "rfi response" Then
Call RFIInfo(worddoc)
End If

WordApp.Visible = True
End Sub


and then:

Public Sub RFIInfo(worddoc As Object)
worddoc.Unprotect
worddoc.Bookmarks("projname").Range.Text = _
ActiveWorkbook.Sheets("project info").Range("a7")
worddoc.Bookmarks("projnum").Range.Text = _
"FILE: " & ActiveWorkbook.Sheets("project
info").Range("b7")
With worddoc.FormFields("dropdown1").DropDown.ListEntri es
.Add ("september")
.Add ("marching orders")
.Add ("Mr. Humboldt")
End With

'everything works exactly as i need until this last line:
worddoc.Protect Type:=wdAllowOnlyFormFields, noreset:=True
End Sub

so i think i may be doing a couple things wrong. I don't think i am
checking for an open copy of word exactly right, in my on error resume
next, but it is working.
i am also not sure i am passing the object variable worddoc with enough
information. Is it a problem that the variable worddoc has the same
name in both subs? I feel like it is not the same thing after it is
passed, maybe i should have a ByRef in there? Anyway, i am an
intermediate newbie at this, just kind of fumbling my way through. Any
suggestions about ways to make my code better, cleaner, more efficient,
etc. would be greatly appreciated.

  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1,588
Default problem protecting word document by macro in excel

What error do you get ?

Tim.


"natanz" wrote in message
oups.com...
I keep a log of rfi's(requests for information) in excel. when i
answer them i open up a document from a template in word. i have been
working on automating this process. so far i have gotten to the point
where i can open a document in word, unprotect it, change some of it's
formfields and bookmarks. but now i am trying to reprotect it. So
here's my code:

Sub OpenTemplate(DocType As String) 'in this case it is "RFI Response"

Dim WordApp As Object
Set WordApp = Nothing
Dim worddoc As Object
Dim TemplateFile As String
Dim TemplateDir As String

'is this code doing what i want it to do, namely checking if there
is an open copy of MS
'word
On Error Resume Next
Set WordApp = GetObject("word.application")
If Err.Number < 0 Then
Set WordApp = CreateObject("word.application")
End If
TemplateDir = "C:\Documents and Settings\My Documents\forms\"
TemplateFile = TemplateDir & DocType & ".dot"
WordApp.Documents.Add Template:=TemplateFile
Set worddoc = WordApp.activedocument

If DocType = "rfi response" Then
Call RFIInfo(worddoc)
End If

WordApp.Visible = True
End Sub


and then:

Public Sub RFIInfo(worddoc As Object)
worddoc.Unprotect
worddoc.Bookmarks("projname").Range.Text = _
ActiveWorkbook.Sheets("project info").Range("a7")
worddoc.Bookmarks("projnum").Range.Text = _
"FILE: " & ActiveWorkbook.Sheets("project
info").Range("b7")
With worddoc.FormFields("dropdown1").DropDown.ListEntri es
.Add ("september")
.Add ("marching orders")
.Add ("Mr. Humboldt")
End With

'everything works exactly as i need until this last line:
worddoc.Protect Type:=wdAllowOnlyFormFields, noreset:=True
End Sub

so i think i may be doing a couple things wrong. I don't think i am
checking for an open copy of word exactly right, in my on error resume
next, but it is working.
i am also not sure i am passing the object variable worddoc with enough
information. Is it a problem that the variable worddoc has the same
name in both subs? I feel like it is not the same thing after it is
passed, maybe i should have a ByRef in there? Anyway, i am an
intermediate newbie at this, just kind of fumbling my way through. Any
suggestions about ways to make my code better, cleaner, more efficient,
etc. would be greatly appreciated.



  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 40
Default problem protecting word document by macro in excel

on the protection part, i actually don't get any error, but the ms word
file does not change to a protected form either.

the code above all works, i don't get any errors.

Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Reading Word document by using Excel Macro Arvind Mane Excel Discussion (Misc queries) 2 September 26th 08 07:55 AM
Printing Problem in Word Document Created from Excel VBA Jim Skrydlak[_2_] Excel Programming 2 January 30th 06 05:56 PM
Problem with linked Excel table in a word Document jcarlos Excel Discussion (Misc queries) 1 August 5th 05 07:41 PM
Printing a Word document from an excel macro mshimandle Excel Programming 0 September 30th 04 04:07 PM
problem editing Word document with VBA from Excel Malone[_2_] Excel Programming 0 August 22nd 03 08:41 PM


All times are GMT +1. The time now is 03:43 AM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"