![]() |
|
#1
|
|||
|
|||
|
Forgive me in advance, but I have a word.dot and have managed to get the code to insert a unique receipt number sequentially into the header of the document on open, trouble is I need to also find a way of stopping the code from running again and again once it has created an invoice number as when ever someone else opens the doc it runs again and the code overwrites the originally created number with a new one, has anyone came across or solved this in the past and knows how to get around it, i thought of using the system date/time but not sure how to implement this Code:
Code:
Dim path As String
path = "\Documents\Contracts\"
Order = System.PrivateProfileString("\Documents\Contracts\settings.txt", "macrosettings", "Order")
With ActiveDocument.Bookmarks
.Add Name:="Order"
End With
If Order = "" Then
Order = 1
Else
Order = Order + 1
End If
System.PrivateProfileString("\\Documents\Contracts\settings.txt", "MacroSettings", _
"Order") = Order
ActiveDocument.Bookmarks("Order").Range.InsertBefore Format(Order, "7000#")
'ActiveDocument.SaveAs FileName:=path & Format(Order, "00#")
End Sub
|
|
#2
|
||||
|
||||
|
What is missing from your post is the Sub's name, which is all you might need to change to prevent it from updating the new document. See, for example:
https://www.msofficeforums.com/word/...html#post19265 https://www.msofficeforums.com/word-...-together.html
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
|
#3
|
|||
|
|||
|
Quote:
![]() http://support.microsoft.com/kb/172109 |
|
#4
|
||||
|
||||
|
Assuming your macro resides in a template, there isn't any code to be removed from the document. But, since your code is incomplete, missing the vital sub name, I can't tell how it's triggered. The links I posted both show code that will only run of its own accord when a new document is created from the template.
If, however, you're using a document and simply copying it (bad idea), you will have great difficulty in running a macro from that document to delete all macros in the document. After all, as soon as it deletes its own code module, it'll stop running. Apart from that, Word would require trusted access to the VBA project (unwise as a normal setting) and that can't be given programmatically.
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
|
#5
|
|||
|
|||
|
Quote:
I really don't know how else to say it or maybe people who reading this are looking into this post too deeply and I'm not the brightess when it comes to VBA please help ..Heres what I have so far Code:
Private Sub Document_Open()
Dim path, pwd As String
Dim mybookmark As String
Dim BMRange As Range
pwd = gint3232
mybookmark = System.PrivateProfileString("P:\Industries\Workshops\Maintenance\settings.txt", _
"macrosettings", "mybookmark")
If mybookmark = "" Then
mybookmark = 1
Else
mybookmark = mybookmark + 1
End If
'Identify current Bookmark range and insert text
System.PrivateProfileString("P:\Industries\Workshops\Maintenance\settings.txt", "MacroSettings", _
"mybookmark") = mybookmark 'this line increments settings.txt by one
Set BMRange = ActiveDocument.Bookmarks("MyBookmark").Range
BMRange.Text = Format(mybookmark, "700000#") 'this line adds the numbers to the bookmark in word
path = "P:\Industries\Workshops\Maintenance\" 'this line is for saving when i eventually switch it on
ActiveDocument.Bookmarks.Add "MyBookmark", BMRange
If ActiveDocument.ProtectionType = wdNoProtection Then
ActiveDocument.Protect _
Type:=wdAllowOnlyFormFields, NoReset:=True, Password:=password1
End If
ActiveDocument.SaveAs2
End Sub
Last edited by macropod; 07-25-2014 at 10:03 PM. Reason: Added code tags & formatting |
|
#6
|
||||
|
||||
|
Quote:
It seems you're trying to create something like an invoicing system, where each new document is allocated a number that stays with that document for life. For that, see, for example: https://www.msofficeforums.com/word/...html#post19265 and: https://www.msofficeforums.com/word/...html#post34444 and: https://www.msofficeforums.com/word/...html#post34511 Note that the attachment in the second & third links show how to implement the code using a template (all three links are to parts of the same thread).
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
|
#7
|
|||
|
|||
|
Quote:
thanks for your quick response |
|
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
Need Help with Below Code
|
rsrasc | Word VBA | 6 | 04-01-2014 03:42 PM |
Where does my code go?
|
rbaldwin | Word VBA | 3 | 03-14-2012 02:31 PM |
vbc code
|
rajpeter | Excel Programming | 2 | 09-13-2011 02:29 PM |
| vba code in excel | rajpeter | Excel Programming | 5 | 09-11-2011 06:13 PM |