PDA

View Full Version : Save Macro in word



sadsfan
06-06-2006, 08:58 AM
I have created a template for a form. I have also created a custom tollbar, this has on it a custom 'new document' button (to open a new document automatically, based on the template).I have also made a custom 'save' button, which sets the filename from information in a formfield. When I save using the custom 'save' word automatically sets the file type as a word document, but after I have opened a new document using the custom 'new document' and then saved it using the custom 'save button, word sets the file type as a web-page. Can anyone help I have included my code


Sub New_Report()
'Save old document and assign it to a variable
Dim ofile As Document
Set ofile = ActiveDocument
'Open a new document and close old
Documents.Add Template:=ActiveDocument.AttachedTemplate.FullName, _
DocumentType:=wdDocument, Visible:=True
ofile.Close
End Sub


Sub Save_Report()
' save the report file as a word document
With Application.Dialogs(wdDialogFileSaveAs)
.Name = ActiveDocument.FormFields("Text16").Result
.Show
End With
End Sub

fumei
06-06-2006, 06:57 PM
Sub New_Report()
'Save old document and assign it to a variable
Dim ofile As Document
Set ofile = ActiveDocument
'Open a new document and close old
Documents.Add Template:=ActiveDocument.AttachedTemplate.FullName, _
DocumentType:=wdDocument, Visible:=True
ofile.Close
End Sub does not do what you state it is doing. You state "save old document and assign it to a variable.

1. it is not saved
2. it is closed, and once this Sub terminates, the document variable is too. See Scope.

NOTE that while the document variable is no longer in scope, the memory address is NOT destroyed, as there is no Set ofile = Nothing.

Your second code piece also does not do what you state it is doing. Yes, the FileSaveAs dialog is displayed. But you state the document IS saved by this Sub. No, it is not. The user must click OK, and the user may change the name. In fact...the user can easily Cancel - and the document is NOT saved at all. If you want the document saved with the explicit name from the formfield...then do that. Why involve the user?

As for the other thing, it opening as a web-page. Hmmmm. I have not been able to duplicate this. What version are you using?

fumei
06-06-2006, 07:03 PM
Well, I'll be darned. Something odd here. I just noticed - after I tried to duplicate this - and testing on your code - that the New button on the toolbar has changed from a New Document....to a New Web Page!

How odd.

sadsfan
06-07-2006, 05:15 AM
Thanks Gerry Okay, my code is bad, I'm only just beginning! I left the user able to change the filename etc, so that they could change the directory they save in, rather than setting a directory. Any suggestions for help with the code, it would be very much appreciated!

fumei
06-07-2006, 12:40 PM
No, you code is NOT bad. Your code simply does not do what you think it is doing. It is a very good ideas to write out what you want to happen. In the smallest steps you can. Make decisions about what you want to happen.

Like the user saving the document. If it is fine that the user can do whatever they want...then that is fine. But always try and be clear about what IS happening. Do not get into the habits of assuming that because YOU would do something, that someone else will do the same thing. If you think your code is saving the document, then make the code save the document. Your code does not save the document. A fine hair...but very real.

Determine exactly what you want. And when. This is design. The coding part comes a lot easier if you have that down solidly.

Still trying to figure out that web page thingie. Haven't had time to look at it...

Have a question though. In your first code, you are assigning the current document to a document variable. OK. Sure. WHY are you doing this? As pointed out, it does not do anything afterwards, and can not.

sadsfan
06-08-2006, 03:50 AM
A friend of mine helped me out with the code, when I told him I wanted to create a 'new document' button to open a document based on the template. Thanks for your continued help! David