PDA

View Full Version : Using Bookmark name as part of a save file name- VBA Word



Victor
08-25-2015, 07:32 AM
Hi all

I have tis Word 2010 macro which add a sequential name to an invoice number, write it to the file and then store the file with the name of the invoice number.

It works fine but now I want to add a predefine name which I add manually to the file and want it to be part of the file name. I modified the VBA macro enclosed without success.

This is what it does

When run the AutoNEW macro it writes de invoice number 001 and and saves the file as Factura001. When it runs again it adds a number to the previous invoice. In tis case Factura002 and save the file with the name Factura002.

The help I need is how to modify the VBA that if I assign he bookmark name to let say "OB" the file name when saved assign the name "FacturaOB00". Continue Add a number to the invoice number. My problem is with the PreOrder bookmark name and the invoice number is not increasing.

The catch is that is not always OB, so this can not be fixed as part od the macro. That's why I assigned bookmark name.

Enclosed is the VBA Word 2010 AutoNew macro.



Sub AutoNew()
'
' AutoNew Macro
'
'
Order = System.PrivateProfileString("c:\settings.txt", "MacroSettings", "Order")
PreOrder = System.PrivateProfileString("c:\settings.txt", "MacroSettings", "PreOrder")
If Order = "" Then
Order = 1
Else
Order = Order + 1
End If
On Error Resume Next
System.PrivateProfileString("c:\settings.txt", "MacroSettings", "Order") = Order

ActiveDocument.Bookmarks("Order").Range.InsertBefore Format(Order, "00#")
ActiveDocument.SaveAs2 FileName:="FACTURA" & Format(PreOrder, "@@") & Format(Order, "00#")
End Sub


Thanks for the help,

gmayor
08-26-2015, 01:33 AM
It is good practice to always declare your variables.
I assume that PreOrder is supposed to be the value of the bookmark 'OB'?
The following modifications should work for you if I have understood the requirement correctly.


Option Explicit
Sub AutoNew()
Dim Order As String
Dim PreOrder As String
Dim oBM As Bookmark
Dim oRng As Range
Const strPath As String = "C:\Path\" ' where you want to save the documents

Order = System.PrivateProfileString("c:\settings.txt", "MacroSettings", "Order")
PreOrder = ""
For Each oBM In ActiveDocument.Bookmarks
If oBM.name = "OB" Then
PreOrder = oBM.Range.Text
Exit For
End If
Next oBM
'PreOrder = System.PrivateProfileString("c:\settings.txt", "MacroSettings", "PreOrder")
If Order = "" Then
Order = 1
Else
Order = Order + 1
End If
On Error Resume Next
System.PrivateProfileString("c:\settings.txt", "MacroSettings", "Order") = Order
Set oRng = ActiveDocument.Bookmarks("Order").Range
oRng.Text = Format(Order, "00#")
oRng.Bookmarks.Add "Order"
'ActiveDocument.Bookmarks("Order").Range.InsertBefore Format(Order, "00#")
ActiveDocument.SaveAs2 Filename:=strPath & "FACTURA" & PreOrder & Format(Order, "00#") & ".docx"
lbl_Exit:
Set oRng = Nothing
Set oBM = Nothing
Exit Sub
End Sub

Victor
08-26-2015, 09:52 AM
Hi Graham

When run the macro the invoice number does not increase by one.

The file save name has no problems since it increase by one.

I installed the macro in a template macro enable inside a Word normal module.

I run the new file as new using the template as "Run From Existing Option"

Please let me know if I am missing something and how to fixed it.

Thanks for the help.

PS Bookmarks names used in file:
oBM
oRng
Order
PreOrder

Victor
08-26-2015, 06:21 PM
Hi Graham:

Enclosed FACTURAOB001.docx with the code so you can test it and help me to find where is the problem.

As I see it this part of the code is not running. I think it has relation to the Order declared string and this are numbers:

If Order = "" Then
Order = 1
Else
Order = Order + 1
End If

Thanks for the help.

gmayor
08-26-2015, 09:15 PM
The problem with your code is that it is unable to create the settings text file in the root of the C:\ drive. This is often a protected area. You can get around it by changing the two calls to C:\ to use the pre-defined strPath e.g.
Order = System.PrivateProfileString(strPath & "settings.txt", "MacroSettings", "Order")
and
System.PrivateProfileString(strPath & "settings.txt", "MacroSettings", "Order") = OrderThe path defined at strPath must exist, or you must add code to create it - see CreateFolders at http://www.gmayor.com/useful_vba_functions.htm

Victor
08-27-2015, 04:57 AM
I am really trying to fixed with my limit abilities.

In the code there is whre a defined the folder path created by me:

Const strPath As String = "c:\TestFolder" 'where you want to save the documents

Which I believe is where you define the strPath. But there is no way I can make it passes to:

If Order = "" Then
Order = 1
Else
Order = Order + 1
End If

You have been really patience with me. Sorry to keep asking for your help, but you are my only hope to make it works.

Thanks for the help.

PS read the CreateFolders but iI think is previously define as part of the code. See below:

Const strPath As String = "c:\TestFolder" 'where you want to save the documents

gmayor
08-27-2015, 05:02 AM
You need the folder separator or the code will try and create "C:\TestFolderSettings.txt", which it cannot for the same reason already discussed.

Const strPath As String = "c:\TestFolder\" The folder must exist. Const strPath As String does not create the folder, it only assigns it to a string variable.

Victor
08-27-2015, 01:49 PM
You need the folder separator or the code will try and create "C:\TestFolderSettings.txt", which it cannot for the same reason already discussed.

Const strPath As String = "c:\TestFolder\" The folder must exist. Const strPath As String does not create the folder, it only assigns it to a string variable.

Excellent explanation for the Const srPath As String. I was erroneous as how it woks.

Now the code runs without any problems.

Without your help make it runs was impossible for me.

Tnanks for all your help.

Until next time.