Thread: [Solved] code to run once only
View Single Post
 
Old 07-25-2014, 09:59 PM
gerrymac gerrymac is offline Windows 7 64bit Office 2007
Novice
 
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