PDA

View Full Version : prompt and enter text in document, save as different name, change file type



jiggler
08-08-2007, 04:46 PM
Hey there newbie here,

I am quite new to VBA and have been trying to create a macro in word. The macro should always save the open document as its name with the new version number. For example: 'xxx v1', and if i use the macro again: 'xxx v2'. The macro will also save the document as a .htm file but will name it 'xxx', thus replacing the original .htm of the same name. Before it saves i want a form box to appear to prompt the user to type text in a specific area of the document. The text can be typed in the dialogue box and then when 'OK' is pressed the macro proceeds to save the document and name it as previously described.

Below is as far as i've got. The astute amongst you will realise that i have been unable to successfully include the form!
Also, the script i have only works for one document! (named 'test'). I need it to work for any document i open in Word.

Hope this makes sense, really need help:

Sub

ActiveDocument.SaveAs FileName:="test.htm", FileFormat:=wdFormatHTML, _
LockComments:=False, Password:="", AddToRecentFiles:=True, WritePassword _
:="", ReadOnlyRecommended:=False, EmbedTrueTypeFonts:=False, _
SaveNativePictureFormat:=False, SaveFormsData:=False, SaveAsAOCELetter:= _
False
ActiveWindow.View.Type = wdWebView

Dim PathAndFileName As String, n As Long
PathAndFileName = "E:\macro tests\test v"
If Dir(PathAndFileName & ".doc") = "" Then
ActiveDocument.SaveAs (PathAndFileName & ".doc")
Else
n = 1
Do While Dir(PathAndFileName & n & ".doc") <> ""
n = n + 1
Loop
ActiveDocument.SaveAs PathAndFileName & n & ".doc"
End If


End Sub

Thanks
Jiggler

fumei
08-09-2007, 04:32 PM
I will start off with your first issue.

"The macro should always save the open document as its name with the new version number. "

You code does not do that. You are hard-coding the name.

BTW: please use the VBA code tags.
ActiveDocument.SaveAs FileName:="test.htm"This does not save the open document with its name. This is hard coded. Get the open document name, and use it. The name is just a string, so you can change it to whatever you need.

Second;Dim PathAndFileName As String, n As Long
PathAndFileName = "E:\macro tests\test v"
If Dir(PathAndFileName & ".doc") = "" Then
ActiveDocument.SaveAs (PathAndFileName & ".doc")This is rather odd. The Dir is reading as:Dir("E:\macro tests\test v.doc")If you are using just ONE file, why use Dir???? The string comes out as "E:\macro tests\test v.doc" - so why even make the the path/name string?

The open (ACTIVE) document's name is....Name. Its full path is FullName.