Consulting

Results 1 to 3 of 3

Thread: Create a VBA simulating Save As

  1. #1
    VBAX Newbie
    Joined
    Sep 2005
    Posts
    4
    Location

    Create a VBA simulating Save As

    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.

  2. #2
    VBAX Master TonyJollans's Avatar
    Joined
    May 2004
    Location
    Norfolk, England
    Posts
    2,291
    Location
    Hi Tom,

    Off the top of my head I'm not exactly sure what Save As does but you can extract the first paragraph using[vba]ActiveDocument.Paragraphs(1).Range.Text[/vba]You 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 ..[vba]Selection.Homekey wdstory
    selection.bookmarks("\line").select
    FirstLineText = Selection.Text[/vba]The same holds true regarding truncation for this as does for the paragraph.

    If you just want to use the name then do[vba]ActiveDocument.saveAs YourPath & ExtractedText & ".doc"[/vba]
    If you want to just supply a defaut for the user then do[vba]With Application.Dialogs(wdDialogfilesaveas)
    .Name = YourPath & ExtractedText & ".doc"
    .Show
    End With[/vba]Depending on Word version, this may be able to be made slightly easier/better
    Enjoy,
    Tony

    ---------------------------------------------------------------
    Give a man a fish and he'll eat for a day.
    Teach him how to fish and he'll sit in a boat and drink beer all day.

    I'm (slowly) building my own site: www.WordArticles.com

  3. #3
    VBAX Wizard
    Joined
    May 2004
    Posts
    6,713
    Location
    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.
    [vba]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[/vba]

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •