Consulting

Results 1 to 12 of 12

Thread: Making the name file to be automatic with VBA

  1. #1
    VBAX Newbie
    Joined
    Sep 2014
    Posts
    1
    Location

    Making the name file to be automatic with VBA

    Hi Everybody:
    My name is Michael i'm from Peru, well, i'm trying to make that the name of the file when i save it in any way, make it automatic. I mean, inside the doc there is 4 text that combine to form the name of the doc.
    I've been searching for some advice or code but i can't find it.
    I hope someone can help me.
    Regards

  2. #2
    Microsoft Word MVP 2003-2009 VBAX Guru gmaxey's Avatar
    Joined
    Sep 2005
    Posts
    3,335
    Location
    Sub ScratchMacro()
    'A basic Word macro coded by Greg Maxey
    Dim strName As String
      'You will have to determine what fields in the document define the four parts"
      strName = "Part1" & "Part2" & "Part3" & "Part4"
      With Application.Dialogs(wdDialogFileSaveAs)
        .Name = strName
        .Show
      End With
    End Sub
    Greg

    Visit my website: http://gregmaxey.com

  3. #3
    Following up on your advice:
    How do I assign let's say "Part1" to a certain piece of text in my document?

    Or does this specific text needs to be in a Textbox or ...?

  4. #4
    I found a post on windowssecrets.com, that I've implented:
    Yes, a macro that's going to create a file name can get the contents of bookmarks, with an expression like ActiveDocument.Bookmarks("Company").Range.Text where Company is the name of the bookmark
    But when using this method, I get too much information :/
    My Filename Save as, shows up as following:
    Taxatierapport SEK -  Autos2Go                                                    Van Leeuwenhoeklaan 2A             2713RB Zoetermeer - Volkswagen Passat - VF7SH8FP0AT559636.dotm
    1) I want to get rid of these extra spaces
    2) I only need the name of the company (not the address in total). The bookmark is tagged to a dropdown-menu where yes, all this info is available in (incl. the spaces).
    3) Preferable I want the last 4 digits of the chassisnumber instead of the whole field, but not sure how to bookmark the last 4 digits, when they change per document/car.

    Here's my code:

    Sub ScratchMacro()'
    ' ScratchMacro Macro
    '
    '
         'A basic Word macro coded by Greg Maxey
        Dim strName As String
         'You will have to determine what fields in the document define the four parts"
        strName = "Taxatierapport SEK - " & ActiveDocument.Bookmarks("Opdrachtgever").Range.Text & " - " & ActiveDocument.Bookmarks("Merk").Range.Text & " " & ActiveDocument.Bookmarks("Type").Range.Text & " - " & ActiveDocument.Bookmarks("Chassis").Range.Text
        
        
        With Application.Dialogs(wdDialogFileSaveAs)
            .Name = strName
            .Show
        End With
    End Sub

  5. #5
    Moderator VBAX Sage SamT's Avatar
    Joined
    Oct 2006
    Location
    Near Columbia
    Posts
    7,814
    Location
    & Trim(ActiveDocument.Bookmarks("Opdrachtgever").Range.Text) & Blah &Trim(etc,
    I expect the student to do their homework and find all the errrors I leeve in.


    Please take the time to read the Forum FAQ

  6. #6
    Quote Originally Posted by SamT View Post
    & Trim(ActiveDocument.Bookmarks("Opdrachtgever").Range.Text) & Blah &Trim(etc,
    When I replace ActiveDocument.Bookmarks("Opdrachtgever").Range.Text with your Trim(ActiveDocument.Bookmarks("Opdrachtgever").Range.Text) it doesnt work. Still same amount of spaces.

    When I add your Trim(ActiveDocument.Bookmarks("Opdrachtgever").Range.Text) it says "Error 9105: String is larger than 255 characters."

  7. #7
    You can reduce the length of the string by breaking it up into separate parts. Replace the line:
    strName = "Taxatierapport SEK - " & ActiveDocument.Bookmarks("Opdrachtgever").Range.Text & " - " & ActiveDocument.Bookmarks("Merk").Range.Text & " " & ActiveDocument.Bookmarks("Type").Range.Text & " - " & ActiveDocument.Bookmarks("Chassis").Range.Text
    with
    strName = "Taxatierapport SEK - "
    strName = strName & Trim(ActiveDocument.Bookmarks("Opdrachtgever").Range.Text)
    strName = strName & " - " & Trim(ActiveDocument.Bookmarks("Merk").Range.Text)
    strName = strName & " " & Trim(ActiveDocument.Bookmarks("Type").Range.Text)
    strName = strName & " - " & Trim(ActiveDocument.Bookmarks("Chassis").Range.Text)
    If you still have unwanted spaces we will need to see a sample of the document
    Graham Mayor - MS MVP (Word) 2002-2019
    Visit my web site for more programming tips and ready made processes
    http://www.gmayor.com

  8. #8
    Quote Originally Posted by gmayor View Post
    You can reduce the length of the string by breaking it up into separate parts. Replace the line:
    strName = "Taxatierapport SEK - " & ActiveDocument.Bookmarks("Opdrachtgever").Range.Text & " - " & ActiveDocument.Bookmarks("Merk").Range.Text & " " & ActiveDocument.Bookmarks("Type").Range.Text & " - " & ActiveDocument.Bookmarks("Chassis").Range.Text
    with
    strName = "Taxatierapport SEK - "
    strName = strName & Trim(ActiveDocument.Bookmarks("Opdrachtgever").Range.Text)
    strName = strName & " - " & Trim(ActiveDocument.Bookmarks("Merk").Range.Text)
    strName = strName & " " & Trim(ActiveDocument.Bookmarks("Type").Range.Text)
    strName = strName & " - " & Trim(ActiveDocument.Bookmarks("Chassis").Range.Text)
    If you still have unwanted spaces we will need to see a sample of the document
    I replaced the code with yours, and it still not getting rid of the spaces.
    I do think that's because the content of ''Opdrachtgever'' has these spaces in it.
    This is on purpose so the Name & Address will show up right under eachother.

    See attachment for the requested sample document.
    Attached Files Attached Files

  9. #9
    OK, I see the problem.
    Add the following function to the module. This is a standard function from Microsoft for removing unwanted spaces.

    Function TrimSpace(strInput As String) As String
       ' This procedure trims extra space from any part of
       ' a string.
    
       Dim astrInput()     As String
       Dim astrText()      As String
       Dim strElement      As String
       Dim lngCount        As Long
       Dim lngIncr         As Long
       
       ' Split passed-in string.
       astrInput = Split(strInput)
       
       ' Resize second array to be same size.
       ReDim astrText(UBound(astrInput))
       
       ' Initialize counter variable for second array.
       lngIncr = LBound(astrInput)
       ' Loop through split array, looking for
       ' non-zero-length strings.
       For lngCount = LBound(astrInput) To UBound(astrInput)
          strElement = astrInput(lngCount)
          If Len(strElement) > 0 Then
             ' Store in second array.
             astrText(lngIncr) = strElement
             lngIncr = lngIncr + 1
          End If
       Next
       ' Resize new array.
       ReDim Preserve astrText(LBound(astrText) To lngIncr - 1)
    
       ' Join new array to return string.
       TrimSpace = Join(astrText)
    End Function
    The change the code I posted earlier to
    strName = "Taxatierapport SEK - "
    strName = strName & TrimSpace(ActiveDocument.Bookmarks("Opdrachtgever").Range.Text)
    strName = strName & " - " & Trim(ActiveDocument.Bookmarks("Merk").Range.Text)
    strName = strName & " " & Trim(ActiveDocument.Bookmarks("Type").Range.Text)
    strName = strName & " - " & Trim(ActiveDocument.Bookmarks("Chassis").Range.Text)
    Incidentally this code should go in the template and not in the normal template, which is where I assume it currently resides.
    Graham Mayor - MS MVP (Word) 2002-2019
    Visit my web site for more programming tips and ready made processes
    http://www.gmayor.com

  10. #10
    Works perfect!
    Thanks again

    The only thing that happens in the document ''Factuur'', is that it outputs some weird lil' dot in the end.

    So a document would be saved like this, see pic:
    vba-weird-dot_v2.png

    Is there a way to get rid of this weird lil' dot?
    It doesnt show in the document itself.

    The code of the customized SaveAs function for document Factuur:

    Sub SaveAs_Factuur_incl_klantgegevens_factuur()'
    ' 
    ' SaveAs_Factuur_incl_klantgegevens_factuur
    '
            
        'A basic Word macro coded by Greg Maxey
        Dim strName As String
         'You will have to determine what fields in the document define the four parts"
    strName = "Factuur SEK - "
    strName = strName & TrimSpace(ActiveDocument.Bookmarks("Klantgegevens").Range.Text)
    strName = strName & " - " & TrimSpace(ActiveDocument.Bookmarks("Factuur").Range.Text)
        
        
        With Application.Dialogs(wdDialogFileSaveAs)
            .Name = strName
            .Show
            End With
            
    End Sub
    Function TrimSpace(strInput As String) As String
         ' This procedure trims extra space from any part of
         ' a string.
         
        Dim astrInput()     As String
        Dim astrText()      As String
        Dim strElement      As String
        Dim lngCount        As Long
        Dim lngIncr         As Long
         
         ' Split passed-in string.
        astrInput = Split(strInput)
         
         ' Resize second array to be same size.
        ReDim astrText(UBound(astrInput))
         
         ' Initialize counter variable for second array.
        lngIncr = LBound(astrInput)
         ' Loop through split array, looking for
         ' non-zero-length strings.
        For lngCount = LBound(astrInput) To UBound(astrInput)
            strElement = astrInput(lngCount)
            If Len(strElement) > 0 Then
                 ' Store in second array.
                astrText(lngIncr) = strElement
                lngIncr = lngIncr + 1
            End If
        Next
         ' Resize new array.
        ReDim Preserve astrText(LBound(astrText) To lngIncr - 1)
         
         ' Join new array to return string.
        TrimSpace = Join(astrText)
    End Function
    Attached Images Attached Images
    Last edited by snowseals; 05-25-2015 at 03:18 PM.

  11. #11
    The character is probably a bullet character Chr(149). That being the case add the line

    strName = Replace(strName, Chr(149), "")
    immediately before the line
    With Application.Dialogs(wdDialogFileSaveAs)
    If that doesn't fix it, send me the actual document (you should have my e-mail address by now) so I can determine what the character is. It will then be a simple matter to remove it from the string using the method above.
    Last edited by gmayor; 05-26-2015 at 01:31 AM.
    Graham Mayor - MS MVP (Word) 2002-2019
    Visit my web site for more programming tips and ready made processes
    http://www.gmayor.com

  12. #12
    I've emailed you the requested actual document, since the 2 lines from above not doing the trick yet.

Posting Permissions

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