PDA

View Full Version : Solved: Naming a document



davidboutche
09-18-2009, 04:04 AM
I'm using the following code to protect a document. The problem is it's trying to activate "document1". This works fine in most instances but where word has more than one document open it's no longer naming it document1. The document is created from a template. Is there a way I can give it a unique name so I can reference it in my protect code.

Sub formprotect()

Windows("Document1").Activate
ActiveDocument.Protect Password:="qwerty", NoReset:=False, Type:= _
wdAllowOnlyFormFields
End Sub

lucas
09-18-2009, 05:51 AM
If the clone is the only document open why do you have to activate it at all?

Did you try just commenting the first line out and try to let it operate on the activedocument?

davidboutche
09-18-2009, 06:11 AM
Because the project opens up several applications and does a fair bit of jumping between them so i need to have them specifically named.

I'm quite happy for it to save the document using the value of field in order to name it. I've tried this:

filesavename = (UserForm1.PNDbox)
ActiveDocument.SaveAs Filename:="W:INTERGRP\" < filesavename > ".doc"

But this is returning a type mismatch error 13.

Not quite sure where i'm going wrong?

davidboutche
09-18-2009, 06:21 AM
I think I've solved the file naming issue like this:

filesavename = "W:\INTERGRP\" & UserForm1.PNDbox & ".doc"
ActiveDocument.SaveAs Filename:=filesavename

Now I'm hoping that I will be able to use this as a reference when calling the protect sub.

Correct?

lucas
09-18-2009, 06:33 AM
maybe something like:
ActiveDocument.SaveAs FileName:="C:\Temp\" & TextBox1.Value

lucas
09-18-2009, 06:34 AM
Yes, youshould be able to reference it using the variable...

davidboutche
09-18-2009, 06:44 AM
Thanks Steve, yes, both seem to work.

The only problem I have now is referencing it out of that procedure.

If I try:

Windows(filesavename).Activate

it returns member not part of the collection (or something similar). I'm guess that's because the variable "filesavename" can't bee seen out of it's own procedure?

If I try:

Windows(UserForm1.PNDbox).Activate

it does seem to work. Is that the correct way? or is there a way to make the variable filesavename available globally?

lucas
09-18-2009, 07:14 AM
You can declare it publicly. You need to do that in a standard module.

davidboutche
09-18-2009, 07:29 AM
That would make sense

I've opened a new module and called it declarations and inserted:
Dim filesavename As String

Hoping that does the trick.

Let me know if there's any more I should add.

Thanks Steve

lucas
09-18-2009, 07:41 AM
Public filesavename As String

Instead of dim.

Another kinda off topic point. You should not need to activate the document to unprotect it unless the user needs to edit the file. Once you know you are able to activate it just change:


filesavename.Protect Password:="qwerty", NoReset:=False, Type:= _
wdAllowOnlyFormFields



to something like:

ActiveDocument.Protect Password:="qwerty", NoReset:=False, Type:= _
wdAllowOnlyFormFields

Tinbendr
09-18-2009, 07:47 AM
The document is created from a template. Is there a way I can give it a unique name so I can reference it in my protect code?
If you're creating the document from code the you reference the document as a object.

Dim aDoc as document
'Other code here
set aDoc = documents.add MyTemplate

Now, when you reference aDoc, it will always refer to this document, regardless of its visible property.

If the document is already open, then you will have to active it, then set the object.

set aDoc = activedocument

If you've saved the document, you can refer to it's filename specifically.

Set aDoc = Documents("MyDocument.doc")

Now that you've got an object, you can work with it.


With aDoc
.range.paragraphs(2).font.bold = true
'etc
end with

lucas
09-18-2009, 07:53 AM
That makes sense and I'm glad a real Word person came along before I got David too confused.

davidboutche
09-18-2009, 08:51 AM
Thanks to one and all!

lucas
09-18-2009, 08:56 AM
David, you can mark your thread solved using the thread tools at the top of the page.