PDA

View Full Version : Solved: Saving a doc as read only and removing all code!?



Simon Lloyd
09-17-2005, 03:28 PM
Hiya all,

I'm fairly new to VBA and very new here i have done some excel vba (limited) i have a word document (currently saved as .dot) and when it is closed i would like it to save as "mydocument - 17092005".doc where the number is the date in ddmmyyyy format and vbreadonly i would also like all the code to be disabled in the doc so the read only does not make another copy of itself when opened and closed, the doc will be used by people on a network but a simple SaveAs will save it in My Documents on their desktop so they can mail the read only file to others.

Is there a way to do this?.......hope so!

All suggestions welcome.

Regards,

Simon.

MWE
09-17-2005, 03:58 PM
Hiya all,

I'm fairly new to VBA and very new here i have done some excel vba (limited) i have a word document (currently saved as .dot) and when it is closed i would like it to save as "mydocument - 17092005".doc where the number is the date in ddmmyyyy format and vbreadonly i would also like all the code to be disabled in the doc so the read only does not make another copy of itself when opened and closed, the doc will be used by people on a network but a simple SaveAs will save it in My Documents on their desktop so they can mail the read only file to others.

Is there a way to do this?.......hope so!

All suggestions welcome.

Regards,

Simon.
Simon, welcome to the VBAX forum.

The initial "saveas" with the name you want will create a new file with the original left alone. Do you want to disable the VBA code in the new .doc file or do you want to remove the VBA code? If you just disable it, it could be enabled by some future owner.

Simon Lloyd
09-17-2005, 04:01 PM
I would like to remove the code as it will never be needed in the read only copy, and you are correct in thinking i want the original left in tact so the original stays .dot and the readonly copy is .doc.

Thanks for your speedy reply!!!!

Simon

MOS MASTER
09-17-2005, 05:37 PM
Hi and Welcome to VBAX Simon! :hi:

Could you tell me which types of codemodules you are using in your project?

I mean only code modules or also Forms, classmodules?

This so I know which type of code cleaning you need.

To get the readOnly attribute you must use something like:
SetAttr PathName:=sPath, Attributes:=vbReadOnly


Important: the file must be close to apply attributes!

Simon Lloyd
09-18-2005, 12:17 AM
I think i only have a normal module i inserted and added an AutoOpen and AutoClose in that module the module is the default name given when adding a module, when im back at work 3pm GMT i will post what i have mashed together in that module (even though it doesnt work entirely!) and hopefully i will be some way on to achieving what for you guru's must be relatively simple!

Simon

In being humble we simplify our lives

TonyJollans
09-18-2005, 02:26 AM
Hi Simon,

Welcome to VBAX!

You have a template (.dot file) which you want to save as a document (.doc file)? This is completely the wrong way of doing things. You should base a (new) document on a template - that way Word creates a copy of the template for you, without the code.

Can you explain a bit more about what you are doing - or trying to do?

Simon Lloyd
09-18-2005, 02:35 AM
Sorry..........does it show i dont know what i'm doing?, i created a .doc file and saved it as .dot where i then worked on the code in the .dot.

Here's what i want to do......i want users to be able to type in the boxes ive created on the document when they have finished and close the document i want it to save as "thisfilename - 19092005" where each time the document is saved it changes the date 19092005 to the current date (Format(Now, ddmmyyyy)like this in excel) i also want the saved copy to be VbReadOnly and have no VBA in it, so the original will remain unchanged with the VBA project still in tact.

Probably as clear as mud to you but i will post my novice attempt at around 3pm GMT.

Regards,

Simon

TonyJollans
09-18-2005, 04:45 AM
You create your Form (when you say 'boxes' what do you mean - is it a Word Form?), with code, as a (.dot) template. You then base (new) documents on that template - the documents do not, themselves, include the code but they are linked to the template which does. Exactly what you do could depend on the content and the ultimate disposition of the documents.

Simon Lloyd
09-18-2005, 04:53 AM
Hi Tony, it's a bit of a cobble together, i created text boxes in Powerpoint to make some sort of form that contained may boxes in 3 columns then transfered it to word and resized it.........i then (thinking this was correct) saved it as a template and tried to create AutoOpen and AutoClose subs with code to do the things i wanted (as stated in this thread) the aim is this........users use the template to type in the boxes, close it and it makes a copy named the name of the document plus todays date, readonly and code removed so that they now have a document which has been automatically dated in the file name that they can e-mail to everyone who needs to see their entries but not edit them. so my original should remain unchanged.

I'm probably not explaining very well what is going on in my head and what i'm trying to achieve, of course i can mail the document to anyone who wishes to see it as it isn't sensitive!

Hope the haze is a little clearer.
Simon

Simon Lloyd
09-18-2005, 06:41 AM
This is what i currently have in Module 1 (it doesnt work entirely), i dont suppose im even half way to achieving what i need with it.


Sub AutoOpen()
'
' Header1 Macro
If ActiveDocument = vbReadOnly Then
Exit Sub
If ActiveWindow.View.SplitSpecial <> wdPaneNone Then
ActiveWindow.Panes(2).Close
End If
If ActiveWindow.ActivePane.View.Type = wdNormalView Or ActiveWindow. _
ActivePane.View.Type = wdOutlineView Then
ActiveWindow.ActivePane.View.Type = wdPrintView
End If
ActiveWindow.ActivePane.View.SeekView = wdSeekCurrentPageHeader
NormalTemplate.AutoTextEntries("Created on").Insert Where:=Selection.Range _
, RichText:=True
ActiveWindow.ActivePane.View.SeekView = wdSeekMainDocument
End If

End Sub
Sub AutoClose()
If ActiveDocument = vbReadOnly Then
Exit Sub
ElseIf ActiveDocument = "contract & Date" Then
ActiveDocument.SaveAs "Contract" & " " & Format(Now, "ddmmyyyy") & ".doc", wdFormatDocument
ThisDocument.Close (savechanges = False)

SaveCopyAs sStr
SetAttr sStr, vbReadOnly
End If
End Sub

gmaxey
09-18-2005, 07:06 AM
Simon,

I think I follow what you are trying to do with the AutoOpen, but the AutoClose I don't follow. I am inserting a modification to your code with the AutoClose stetted out:

Sub AutoOpen()
If ActiveDocument.ReadOnly Then Exit Sub
If ActiveWindow.View.SplitSpecial <> wdPaneNone Then
ActiveWindow.Panes(2).Close
End If
With ActiveWindow.ActivePane.View
.Type = wdPrintView
.SeekView = wdSeekCurrentPageHeader
NormalTemplate.AutoTextEntries("Created on").Insert _
Where:=Selection.Range, RichText:=True
.SeekView = wdSeekMainDocument
End With
End Sub
Sub AutoClose()
If ActiveDocument.vbReadOnly Then End Sub
'ActiveDocument = "contract & Date" Then
'ActiveDocument.SaveAs "Contract" & " " & Format(Now, "ddmmyyyy") & ".doc", wdFormatDocument
''ThisDocument.Close (savechanges = False)
'
'SaveCopyAs sStr
'SetAttr sStr, vbReadOnly
'End Sub


I don't follow the first stetted line and after.

Simon Lloyd
09-18-2005, 07:34 AM
Cheers Greg!,

With the AutoClose i am trying to get the file to save as the filename plus the date it was saved so when lookin at the filename it would appear something like thisfilename - 19092005.doc this file must be read only and have all code removed or disabled, so the original stays the same and the edited one that has been closed takes on the above attributes..........they only need it to be read only once saved, it will then be e-mailed to other members and archived.

Simon

Simon Lloyd
09-18-2005, 07:51 AM
This works up to now, but i now need the AutoClose to make the saved file read only and everything should be fine!.........i hope!

Simon

Sub AutoClose()
If ThisDocument.ReadOnly Then
Exit Sub
Else
ActiveDocument.SaveAs "Contract1" & " " & Format(Now, "ddmmyyyy") & ".doc", wdFormatDocument

TonyJollans
09-18-2005, 10:31 AM
Hi Simon,

I have knocked this up rather quickly but the attached does what I think you want - there is a description in the document. If it's basically right we can tweak it for what you want; if it's way off beam, just say so :)

Just remembered after uploading it that it uses the time (not the date) in the file name - it made testing easier!

Also I don't think I said in the waffle that it is a template, so double click it to get a new document based on it - the template never changes, each document is new.

Anyway let me know.

fumei
09-19-2005, 06:20 AM
You could just as easily - if not having the code modules in the new file is important - not do a SaveAs, but contents copy. With just copying (and saving of course), no code as poassed to the new file.

May be a little easier that way.

MOS MASTER
09-20-2005, 03:45 PM
What's the status on this one? :yes

Simon Lloyd
09-20-2005, 11:55 PM
Thanks to all that replied, ive had a small accident in my car which has kept me from replying.......ok now! you have all been of great help.......Tony thanks for a great example.....taught me quite a lot......i am now recreating the document using word and creating form feilds with the rest of the document locked, it will probably look a lot smarter when ive done!

Great tips.................once again thanks guys!

Regards,

Simon.

TonyJollans
09-21-2005, 02:51 AM
Good stuff. Glad I could help.

There is so much that can be done in Word - but much of it isn't obvious. If my example has pushed you in the right direction then I'm happy :)

MOS MASTER
09-21-2005, 03:07 PM
Glad to here it's solved Simon! :yes

fumei
09-21-2005, 11:46 PM
Be sure to post again with what you've done.

MOS MASTER
09-22-2005, 02:46 PM
O..I thought he used the method showed by Tony..:yes

If not please do post your code...We are always willing to learn stuff. :whistle:

fumei
09-23-2005, 08:25 AM
Well, possibly, but he said that he was "recreating" the document. This, to me, a rather large scale change. I am always interested in how this process goes.

MOS MASTER
09-23-2005, 04:02 PM
Well, possibly, but he said that he was "recreating" the document. This, to me, a rather large scale change. I am always interested in how this process goes.

Me too Gerry ... but I forgot to ask it this time. I always like to see code that brought the sollution to the OP.

Its nice to have that in the same thread as the original question. :yes

fumei
09-24-2005, 05:33 AM
Quite so.