PDA

View Full Version : Create a VBA simulating Save As



tom7w
10-06-2005, 04:21 AM
I noticed when we save a Word Document the first time, a pop up Save window appears with the first line of the document as the name of the file as a suggestion for the name of the file.
This behavior also happens if we clikc Save As menu item.
How do I create this behavior as a VBA module?
I breaks a long ten-page file into a ten individual sections/files.
There is a VBA KnowledgeBase to do this.(see separate mail merge file)
Then I would like to continue with naming the ten files using the first line of each file. How do I create a VBA for saveAS with naming file using the first line?

Thank for reading /answering my question.

TonyJollans
10-06-2005, 07:10 AM
Hi Tom,

Off the top of my head I'm not exactly sure what Save As does but you can extract the first paragraph usingActiveDocument.Paragraphs(1).Range.TextYou may have to strip off the paragraph mark at the end - and truncate the text if it's excessively long.

If you really want the first line (Word doesn't really have a concept of line because it's printer dependent) then you need to use the Selection ..Selection.Homekey wdstory
selection.bookmarks("\line").select
FirstLineText = Selection.TextThe same holds true regarding truncation for this as does for the paragraph.

If you just want to use the name then doActiveDocument.saveAs YourPath & ExtractedText & ".doc"
If you want to just supply a defaut for the user then doWith Application.Dialogs(wdDialogfilesaveas)
.Name = YourPath & ExtractedText & ".doc"
.Show
End WithDepending on Word version, this may be able to be made slightly easier/better

fumei
10-06-2005, 07:29 AM
Depending on how much you want to automate.....

The following extracts every section and saves each one as a separate file using the first TWO words as the filename. You could of course make it the whole first line, and you could make it come up as a dialog as Tony points out. This is fully automated and just does it - using the first two words.
Sub SectionsToDocs()
Dim oRange As Word.Range
Dim oSection As Word.Section
Dim strFileName As String
Dim ThisDoc As Document
Dim ThatDoc As Document
Set ThisDoc = ActiveDocument
For Each oSection In ThisDoc.Sections()
Set oRange = oSection.Range
strFileName = Trim(oRange.Words(1) & oRange.Words(2))
oRange.Select
Selection.Copy
Documents.Add
Set ThatDoc = ActiveDocument
Selection.Paste
ThatDoc.SaveAs FileName:=strFileName & ".doc"
ThatDoc.Close
Set ThatDoc = Nothing
Next
set ThisDoc = Nothing
End Sub