![]() |
|
#1
|
|||
|
|||
![]()
Hi,
I have a document in the format docm. I have a macro running on a field that updates the field every time the document is opened. Now I am looking for a way to disable the macro when i save the document with a new name. I don't want the field to update after i have saved the document. Any suggestions? |
#2
|
||||
|
||||
![]()
You could test the macro against the name of the document (or save the document as DOCX format and it will not have a macro).
In the case of the former, let's assume a docvariable field that displays the docvariable "varDate", which to demonstrate has a value that is the current time. The following macro saved in the ThisDocument module will only run if the document containing the macro is called 'docname.docm' Code:
Option Explicit Private Sub Document_Open() Dim oFld As Field If ThisDocument.Name = "docname.docm" Then For Each oFld In ThisDocument.Fields If oFld.Type = wdFieldDocVariable Then If InStr(1, oFld.Code, "varDate") > 0 Then oFld.Locked = False ThisDocument.Variables("varDate").Value = Format(Time, "hh:mm:ss") oFld.Update oFld.Locked = True Exit For End If End If Next oFld End If lbl_Exit: Exit Sub End Sub
__________________
Graham Mayor - MS MVP (Word) (2002-2019) Visit my web site for more programming tips and ready made processes www.gmayor.com |
#3
|
||||
|
||||
![]()
You didn't specify how to identify the field that needs to be locked so Graham made an assumption on what that field may have contained. You would most likely need to alter the code to fit your actual field content.
Another way may be to bookmark the field and then change the code to lock any fields contained in that bookmarked range.
__________________
Andrew Lockton Chrysalis Design, Melbourne Australia |
#4
|
|||
|
|||
![]()
Hi, Tried the suggested code, but did something wrong. Have attached the file here.
The point is that the file should give a new reference number each time it is opened "clean", but if the user fill something in the document, the reference number should be fixed (Not changed next time it is opened). |
#5
|
||||
|
||||
![]()
The code was intended to be used instead of your existing code, but in the ThisDocument module, and not Module1. However all you need do is wrap the existing macro in a conditional statement e.g.
Code:
Sub AutoOpen() Dim oVars As Variables Dim oVar As Variable Dim bVar As Boolean Dim lngCount As Long If ThisDocument.Name = "Bestillingsskjema test v2-1.docm" Then Set oVars = ActiveDocument.Variables For Each oVar In oVars If oVar.Name = "varNum" Then bVar = True lngCount = oVar.Value + 1 Exit For End If Next oVar If Not bVar Then lngCount = 1 oVars("varNum").Value = lngCount UpdateAllFields ActiveDocument.Save End If lbl_Exit: Exit Sub End Sub Code:
Option Explicit Sub AutoNew() Dim oVars As Variables Dim oVar As Variable Dim bVar As Boolean Dim lngCount As Long Set oVars = ThisDocument.Variables For Each oVar In oVars If oVar.Name = "varNum" Then bVar = True lngCount = oVar.Value + 1 Exit For End If Next oVar If Not bVar Then lngCount = 1 oVars("varNum").Value = lngCount ActiveDocument.Variables("varNum").Value = lngCount UpdateAllFields ActiveDocument UpdateAllFields ThisDocument UpdateTemplate lbl_Exit: Exit Sub End Sub Sub UpdateTemplate() Dim bBackup As Boolean bBackup = Options.CreateBackup Options.CreateBackup = False ThisDocument.Save Options.CreateBackup = bBackup lbl_Exit: Exit Sub End Sub Sub UpdateAllFields(oDoc as Document) Dim oStory As Range For Each oStory In oDoc.StoryRanges oStory.Fields.Update If oStory.StoryType <> wdMainTextStory Then While Not (oStory.NextStoryRange Is Nothing) Set oStory = oStory.NextStoryRange oStory.Fields.Update Wend End If Next oStory Set oStory = Nothing lbl_Exit: Exit Sub End Sub
__________________
Graham Mayor - MS MVP (Word) (2002-2019) Visit my web site for more programming tips and ready made processes www.gmayor.com |
#6
|
|||
|
|||
![]()
Thanks a lot. This code worked fine. Now i just have to sit Down understanding this for the future
![]() |
![]() |
|
![]() |
||||
Thread | Thread Starter | Forum | Replies | Last Post |
![]() |
rvessio | Word VBA | 4 | 07-25-2016 12:37 PM |
VBA to save a workbook without the macros | OTPM | Excel Programming | 1 | 01-15-2014 12:42 PM |
![]() |
samanthab | Word | 3 | 01-19-2013 06:27 AM |
Word ask to save template whenever i save a derived document | jorbjo | Word | 3 | 10-04-2012 10:52 AM |
PowerPoint Crashes every time I try to save/save as | Crimson_Ninja | PowerPoint | 0 | 05-04-2011 01:54 PM |