Microsoft Office Forums

Go Back   Microsoft Office Forums > >

 
 
Thread Tools Display Modes
Prev Previous Post   Next Post Next
  #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
 



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 03:13 AM.


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