Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 03-24-2015, 07:37 AM
shawnee24 shawnee24 is offline FileSaveAs and UpdateAllFields macros not working in new document Windows 7 64bit FileSaveAs and UpdateAllFields macros not working in new document Office 2010 64bit
Novice
FileSaveAs and UpdateAllFields macros not working in new document
 
Join Date: Mar 2015
Posts: 6
shawnee24 is on a distinguished road
Default FileSaveAs and UpdateAllFields macros not working in new document


Hi, I have a .docx document that I am doing a directory merge into. This document has both the FileSaveAs and UpdateAllFields macros in the footer to automatically update the file name each time it is saved as. However, after I finish the merge into a new document, while the macros are still there, they don't seem to run. I Save As the new file name and the file name field in the footer does not update to the new saved as name. I have tested the base document over and over again and these macros operate well. It's just after the merge is finished in the new document they don't run. I am very much a beginner in both the merge and macro tasks... Thanks in advance!
Reply With Quote
  #2  
Old 03-25-2015, 12:22 AM
gmayor's Avatar
gmayor gmayor is offline FileSaveAs and UpdateAllFields macros not working in new document Windows 7 64bit FileSaveAs and UpdateAllFields macros not working in new document Office 2010 32bit
Expert
 
Join Date: Aug 2014
Posts: 4,106
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 of
Default

There are questions
What do you mean by 'macros in the footer?' You can't have macros in a footer. You can have them act on a footer but then you say your document is DOCX format and that cannot store macros, so where have you stored the macros?
For macros to work on any document they must be stored in a global template. By default that would be the normal template.

It would help if you posted your FileSaveAs code so we can see what the macro does.

For an update all fields macro - take a look at the example code at http://www.gmayor.com/installing_macro.htm This will cover most situations - certainly fields in header/footers.

The FileSaveAs macro would have to either determine the name with which the document is to be saved, then write it to the footer, or apply it to a docvariable field in the footer, or it would have to save the document, update the filename field then save it again to store the new value.
__________________
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-25-2015, 06:05 AM
shawnee24 shawnee24 is offline FileSaveAs and UpdateAllFields macros not working in new document Windows 7 64bit FileSaveAs and UpdateAllFields macros not working in new document Office 2010 64bit
Novice
FileSaveAs and UpdateAllFields macros not working in new document
 
Join Date: Mar 2015
Posts: 6
shawnee24 is on a distinguished road
Default

You are right - macro is not in the footer - said it wrong. I want the macro to execute a function in the footer. As well, I changed my document to .dotm yesterday after reading copious amounts of info online. Still not working. Here is exactly what I want to do - I have a "base" document that I am doing a directory merge into to produce 1 new document with all my data in a table format. Works perfectly. However, the base document has my macros, which are to update my file name in the footer when the new document is "saved as" (via print preview). This is not working. If I "save as" before executing the merge the correct doc name appears in the footer; however, if I were to "save as" again after the merge, it won't update. Here are the macros:

Sub UpdateAllFields()
Dim currentView As WdViewType
currentView = ActiveWindow.View
Options.UpdateFieldsAtPrint = True
ActiveWindow.View = wdPrintPreview
ActiveWindow.View = currentView
End Sub

Sub FileSaveAs()
Dim dlg As Dialog
Set dlg = Dialogs(wdDialogFileSaveAs)
dlg.Show
UpdateAllFields
End Sub


I am setting up a range of template documents that are schedules to legal documents and essentially are the same template; however, the data being merged into them changes, depending on the template. I have a team of users who will be doing the "mass production" of these documents and am trying to have them set up so there is continuity and uniformity to all the documents despite the number of people who will be using them. The only way to differentiate between all of these documents is to be sure the file name is accurate in each footer.
Thanks for your time
Reply With Quote
  #4  
Old 03-25-2015, 08:04 AM
gmayor's Avatar
gmayor gmayor is offline FileSaveAs and UpdateAllFields macros not working in new document Windows 7 64bit FileSaveAs and UpdateAllFields macros not working in new document Office 2010 32bit
Expert
 
Join Date: Aug 2014
Posts: 4,106
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 of
Default

The reason that the field in the footer does not update is that the merge process changes the filename field that you are trying to update into plain text. It would therefore be better if you removed the filename field from the merge document, and then add it after the merge e.g. call the following macro to add the field to the merged document after you have saved it (and note that it will need to be saved again).

Code:
Sub AddFilename()
Dim oSection As Section
Dim oFooter As HeaderFooter
Dim oFld As field
Dim bFound As Boolean
Dim oRng As Range
    For Each oSection In ActiveDocument.Sections
        Set oFooter = oSection.Footers(wdHeaderFooterPrimary)
        For Each oFld In oFooter.Range.Fields
            If oFld.Type = wdFieldFileName Then
                bFound = True
                Exit For
            End If
        Next oFld
        If Not bFound Then
            oFooter.Range.InsertParagraphAfter
            Set oRng = oFooter.Range.Paragraphs.Last.Range
            oRng.ParagraphFormat.Alignment = wdAlignParagraphRight
            oRng.Fields.Add oRng, wdFieldFileName, " \p", False
            oRng.Fields.Update
        End If
    Next oSection
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
  #5  
Old 03-30-2015, 01:38 PM
shawnee24 shawnee24 is offline FileSaveAs and UpdateAllFields macros not working in new document Windows 7 64bit FileSaveAs and UpdateAllFields macros not working in new document Office 2010 64bit
Novice
FileSaveAs and UpdateAllFields macros not working in new document
 
Join Date: Mar 2015
Posts: 6
shawnee24 is on a distinguished road
Default Add Filename works great

I've had a chance to test this finally and it does work well - thank you very much!! However (sorry), how would I be able to have just the file name and not the path?

Thanks for your help
Reply With Quote
  #6  
Old 03-30-2015, 09:54 PM
gmayor's Avatar
gmayor gmayor is offline FileSaveAs and UpdateAllFields macros not working in new document Windows 7 64bit FileSaveAs and UpdateAllFields macros not working in new document Office 2010 32bit
Expert
 
Join Date: Aug 2014
Posts: 4,106
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 of
Default

Change
Code:
oRng.Fields.Add oRng, wdFieldFileName, " \p", False
to
Code:
oRng.Fields.Add oRng, wdFieldFileName, , False
__________________
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
Reply



Similar Threads
Thread Thread Starter Forum Replies Last Post
FileSaveAs and UpdateAllFields macros not working in new document Can DocumentBeforeSave replace FileSaveAs Macer Word VBA 2 06-05-2015 10:03 PM
FileSaveAs and UpdateAllFields macros not working in new document This macros are not working in office english version 2007 hans Excel Programming 4 12-10-2011 07:03 AM
FileSaveAs and UpdateAllFields macros not working in new document Macros not working when template is emailed EMH Excel Programming 1 07-06-2011 09:24 AM
FileSaveAs and UpdateAllFields macros not working in new document Macros not working in word 2010 louise_chapman Word VBA 3 10-25-2010 08:34 AM
FileSaveAs with Code bsmith Office 0 01-05-2006 08:43 AM

Other Forums: Access Forums

All times are GMT -7. The time now is 08:47 AM.


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