Results 1 to 5 of 5
  1. #1
    2 Star Lounger
    Join Date
    Jul 2003
    Location
    Park City, Utah
    Posts
    189
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Filenaming Macro (Word XP)

    Are there any examples anywhere of a macro that copies selected text and names the Word document using that text? I have a filename created and inserted in a new document from user input. Now I need to get that text into the filename and I can't find my VBA reference! Again, I should know this...

  2. #2
    Super Moderator jscher2000's Avatar
    Join Date
    Feb 2001
    Location
    Silicon Valley, USA
    Posts
    23,112
    Thanks
    5
    Thanked 93 Times in 89 Posts

    Re: Filenaming Macro (Word XP)

    Which piece of the puzzle are you missing? These might help you find what you need in the online help:

    Selection.Text returns the currently selected word(s)
    ActiveDocument.SaveAs will allow you to specify a new document name when saving

  3. #3
    2 Star Lounger
    Join Date
    Jul 2003
    Location
    Park City, Utah
    Posts
    189
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Re: Filenaming Macro (Word XP)

    I feel so dumb...I have the macro go to a bookmark, then I need to turn the select on while it goes to the next bookmark so the text between the 2 bookmarks is selected. I can't remember how to do that.

  4. #4
    Plutonium Lounger
    Join Date
    Nov 2001
    Posts
    10,550
    Thanks
    0
    Thanked 7 Times in 7 Posts

    Re: Filenaming Macro (Word XP)

    If your two bookmarks are named b1 and b2 then...
    <font face="Georgia">
    Dim strFilename As String
    strFilename = ActiveDocument.Range(Start:=ActiveDocument.Bookmar ks("b1").Range.End, End:=ActiveDocument.Bookmarks("b2").Range.Start).T ext
    </font face=georgia>
    You may want to add some error handling in case the bookmarks don't exist or are in the wrong order.

    StuartR

  5. #5
    Super Moderator jscher2000's Avatar
    Join Date
    Feb 2001
    Location
    Silicon Valley, USA
    Posts
    23,112
    Thanks
    5
    Thanked 93 Times in 89 Posts

    Re: Filenaming Macro (Word XP)

    Is there a particular best way to do this? Using the Selection object, I can think of two approaches:
    <pre>Sub SelectBetweenBmksExtend()
    ' Method 1 uses Select.Extend
    ActiveDocument.Bookmarks("one").Select
    Selection.Extend
    Selection.GoTo what:=wdGoToBookmark, Name:="two"
    MsgBox Selection.Text
    End Sub

    Sub SelectBetweenBmksExpand()
    ' Method 2 calculates the distance and moves the Selection.End
    With ActiveDocument
    .Bookmarks("one").Select
    Selection.MoveEnd unit:=wdCharacter, _
    Count:=.Bookmarks("two").Start - .Bookmarks("one").End
    End With
    MsgBox Selection.Text
    End Sub</pre>

    If you prefer not to move the selection, you can simply build a range between the two:
    <pre>Function TextBtwnBmks(strFirstBookmark As String, strSecondBookmark As String) As String
    ' This is not fault tolerant, so don't pass me no bad parameters
    With ActiveDocument
    TextBtwnBmks = .Range(Start:=.Bookmarks(strFirstBookmark).End, _
    End:=.Bookmarks(strSecondBookmark).Start).Text
    End With
    End Function

    Sub GetMyFileName()
    MsgBox TextBtwnBmks("one", "two")
    End Sub</pre>

    Hope this helps.

Posting Permissions

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