Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 03-29-2015, 11:32 PM
sannhei sannhei is offline Disableling macros when i save Windows 8 Disableling macros when i save Office 2013
Novice
Disableling macros when i save
 
Join Date: Mar 2015
Posts: 7
sannhei is on a distinguished road
Default Disableling macros when i save


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?
Reply With Quote
  #2  
Old 03-30-2015, 02:41 AM
gmayor's Avatar
gmayor gmayor is offline Disableling macros when i save Windows 7 64bit Disableling macros when i save Office 2010 32bit
Expert
 
Join Date: Aug 2014
Posts: 4,143
gmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud of
Default

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
Reply With Quote
  #3  
Old 03-30-2015, 03:36 AM
Guessed's Avatar
Guessed Guessed is offline Disableling macros when i save Windows 7 32bit Disableling macros when i save Office 2010 32bit
Expert
 
Join Date: Mar 2010
Location: Canberra/Melbourne Australia
Posts: 4,176
Guessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant future
Default

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
Reply With Quote
  #4  
Old 03-30-2015, 06:04 AM
sannhei sannhei is offline Disableling macros when i save Windows 8 Disableling macros when i save Office 2013
Novice
Disableling macros when i save
 
Join Date: Mar 2015
Posts: 7
sannhei is on a distinguished road
Default File atteched

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).
Attached Files
File Type: docm Bestillingsskjema test v2.docm (47.9 KB, 10 views)
Reply With Quote
  #5  
Old 03-30-2015, 07:22 AM
gmayor's Avatar
gmayor gmayor is offline Disableling macros when i save Windows 7 64bit Disableling macros when i save Office 2010 32bit
Expert
 
Join Date: Aug 2014
Posts: 4,143
gmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud of
Default

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
For this type of application you should be using a template and not a document which you appear to be renaming. Create new documents from the template and then you shouldn't have the problem. To do that replace all of the macros with the following macros and save as a macro enabled template DOTM format:

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
Reply With Quote
  #6  
Old 03-30-2015, 10:28 PM
sannhei sannhei is offline Disableling macros when i save Windows 8 Disableling macros when i save Office 2013
Novice
Disableling macros when i save
 
Join Date: Mar 2015
Posts: 7
sannhei is on a distinguished road
Default Problem solved

Thanks a lot. This code worked fine. Now i just have to sit Down understanding this for the future
Reply With Quote
Reply



Similar Threads
Thread Thread Starter Forum Replies Last Post
Disableling macros when i save Macro to save as pdf with ability to choose save as folder 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
Disableling macros when i save When I try to save an existing word doc, save as pops up and will not save... 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

Other Forums: Access Forums

All times are GMT -7. The time now is 03:22 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