PDA

View Full Version : Making the name file to be automatic with VBA



quarkcom
10-01-2014, 10:31 AM
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

gmaxey
10-01-2014, 01:51 PM
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

snowseals
05-24-2015, 10:06 AM
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 ...?

snowseals
05-24-2015, 12:16 PM
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

SamT
05-24-2015, 12:46 PM
& Trim(ActiveDocument.Bookmarks("Opdrachtgever").Range.Text) & Blah &Trim(etc,

snowseals
05-24-2015, 02:59 PM
& 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."

gmayor
05-24-2015, 09:20 PM
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

snowseals
05-25-2015, 04:52 AM
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.

gmayor
05-25-2015, 06:44 AM
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.

snowseals
05-25-2015, 02:53 PM
Works perfect!
Thanks again :friends:

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:
13511

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

gmayor
05-25-2015, 09:48 PM
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.

snowseals
05-26-2015, 04:55 AM
I've emailed you the requested actual document, since the 2 lines from above not doing the trick yet.