Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 05-13-2014, 02:52 PM
gerrymac gerrymac is offline code to run once only Windows 7 64bit code to run once only Office 2007
Novice
code to run once only
 
Join Date: May 2014
Posts: 5
gerrymac is on a distinguished road
Exclamation code to run once only

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
Reply With Quote
  #2  
Old 05-13-2014, 03:29 PM
macropod's Avatar
macropod macropod is offline code to run once only Windows 7 32bit code to run once only Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,963
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

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]
Reply With Quote
  #3  
Old 05-19-2014, 10:39 PM
gerrymac gerrymac is offline code to run once only Windows 7 64bit code to run once only Office 2007
Novice
code to run once only
 
Join Date: May 2014
Posts: 5
gerrymac is on a distinguished road
Wink thanks, but need the vba to be removed

Quote:
Originally Posted by macropod View Post
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
But what I need is for the code to be removed from the new doc once it was created, I found this code but this is for a macro so not quite what I need, but its along the correct direction

http://support.microsoft.com/kb/172109
Reply With Quote
  #4  
Old 05-20-2014, 01:19 AM
macropod's Avatar
macropod macropod is offline code to run once only Windows 7 32bit code to run once only Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,963
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

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]
Reply With Quote
  #5  
Old 07-25-2014, 09:59 PM
gerrymac gerrymac is offline code to run once only Windows 7 64bit code to run once only Office 2007
Novice
code to run once only
 
Join Date: May 2014
Posts: 5
gerrymac is on a distinguished road
Default

Quote:
Originally Posted by macropod View Post
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.
Hi, sorry for the late reply, but I had gave up on this because i wasn't getting anywhere with this code, but i'll have one more last attempt, firstly I am not really sure sure what you mean "resides in the template". The question is, is there any point in creating numbers anywhere in any document for any one if when you close it and reopen it, it then places a new number in by over-writing the newly created number? This is the only reason I was asking how to delete the code once it had ran and inserted number into the document so as it doesn't overwrite. So in essence if there is a way of placing a sequential number in a word doc and once it's in it stays in regardles of how many times it's opened afterwards. Then where do I put this code exactly eg.. on open and what extention do I name it.
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
Reply With Quote
  #6  
Old 07-25-2014, 10:16 PM
macropod's Avatar
macropod macropod is offline code to run once only Windows 7 32bit code to run once only Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,963
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

Quote:
Originally Posted by gerrymac View Post
I am not really sure sure what you mean "resides in the template".
Do you not understand the difference between a document and a template? That difference is a fundamental one in Word! See: http://wordprocessing.about.com/od/u...blwordtemp.htm

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]
Reply With Quote
  #7  
Old 07-26-2014, 12:19 AM
gerrymac gerrymac is offline code to run once only Windows 7 64bit code to run once only Office 2007
Novice
code to run once only
 
Join Date: May 2014
Posts: 5
gerrymac is on a distinguished road
Default

Quote:
Originally Posted by macropod View Post
Do you not understand the difference between a document and a template? That difference is a fundamental one in Word! See: http://wordprocessing.about.com/od/u...blwordtemp.htm

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).
Thanks I thought I did know the difference, but I wasn't sure about where the code should reside because there is few places I could place it in on the left hand side of the vba window when opened and not sure which. like ..+normal, (project document1) and +project(template).
thanks for your quick response
Reply With Quote
Reply



Similar Threads
Thread Thread Starter Forum Replies Last Post
code to run once only Need Help with Below Code rsrasc Word VBA 6 04-01-2014 03:42 PM
code to run once only Where does my code go? rbaldwin Word VBA 3 03-14-2012 02:31 PM
code to run once only 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

Other Forums: Access Forums

All times are GMT -7. The time now is 09:42 PM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2024 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft