Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 10-18-2012, 03:19 AM
Oliver Beirne Oliver Beirne is offline Auto update Filename field Windows XP Auto update Filename field Office 2007
Novice
Auto update Filename field
 
Join Date: Jan 2012
Posts: 7
Oliver Beirne is on a distinguished road
Default Auto update Filename field

Hi



I have some VBA that opens all Word documents in a given folder, pastes their content into a blank template then saves with the original filename in a new folder.

As part of this I want the filename field in the header of the new documents to automatically update with the document filename. I nknwo you can use F9 but I don't want this to be a task for the document users.

I've seen some vba examples such as:
Code:
 
Dim aStory As Range 
Dim aField As Field 
 
' Automatically update all fields in document when it is opened
For Each aStory In ActiveDocument.StoryRanges 
  For Each aField In aStory.Fields 
    aField.Update 
  Next aField 
Next aStory
but these all appear in their own sub routines and when I try to add it to my exisiting code it doesn't work.

I'd really appreciate any pointers on how to solve this, thanks.

Last edited by macropod; 10-18-2012 at 02:33 PM. Reason: Added code tags & formatting
Reply With Quote
  #2  
Old 10-18-2012, 05:10 AM
macropod's Avatar
macropod macropod is offline Auto update Filename field Windows 7 64bit Auto update Filename field Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,963
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

Without knowing what code you're trying to integrate this into, it's rather hard to advise ...
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #3  
Old 10-18-2012, 05:23 AM
Oliver Beirne Oliver Beirne is offline Auto update Filename field Windows XP Auto update Filename field Office 2007
Novice
Auto update Filename field
 
Join Date: Jan 2012
Posts: 7
Oliver Beirne is on a distinguished road
Default

Apologies, here is the code in question:
Code:
 ' Sub for recursing through multiple files in directories, to perform a common task
Sub MassEdit_Loop() 
    Dim MyPath As String, FilesInPath As String, RegEx As String, Template As String, NewFile As String 
    Dim MyFiles() As String 
    Dim FNum As Long 
    Dim MainDoc As Word.Document, MainDoc2 As Word.Document 
    Dim wdApp As Word.Application 
    Dim wdApp2 As Word.Application 
     
    MyPath = "C:\Documents and Settings\beirol\Desktop\Existing" 
    Template = "C:\Documents and Settings\beirol\Desktop\Template\Procedure Template.doc" 
     ' This is the Regex for file searching (e.g. all .doc files in the folder)
    RegEx = "*.doc" 
     
     ' Call upon the Recursive File Name builder
    Dim colFiles As New Collection 
    RecursiveDir colFiles, MyPath, RegEx, True 
     ' Loop through the Filenames to collect them into an Array
    FNum = 0 
    Dim vFile As Variant 
    For Each vFile In colFiles 
        FNum = FNum + 1 
        ReDim Preserve MyFiles(1 To FNum) 
        MyFiles(FNum) = vFile 
    Next vFile 
     
    If FNum > 0 Then 
        For FNum = LBound(MyFiles) To UBound(MyFiles) 
            On Error Resume Next 
             
             ' Open template
            Set wdApp = CreateObject("word.Application") 
            wdApp.Visible = False 
            wdApp.ScreenUpdating = False 
            Set MainDoc = wdApp.Documents.Open(Template) 
             
             ' Open source document
            Set wdApp2 = CreateObject("word.Application") 
            wdApp2.Visible = False 
            wdApp2.ScreenUpdating = False 
            Set MainDoc2 = wdApp2.Documents.Open(MyFiles(FNum)) 
             
             ' Select all data from source doc
             ' Copy data
            wdApp2.Selection.WholeStory 
            wdApp2.Selection.Copy 
            wdApp.Selection.Paste 
             
             'Format the content to Guidelines
            wdApp.Selection.WholeStory 
            wdApp.Selection.Font.Name = "Verdana" 
            wdApp.Selection.Font.Size = 10 
            wdApp.Selection.ParagraphFormat.LineSpacingRule = wdLineSpaceExactly 
            wdApp.Selection.ParagraphFormat.LineSpacing = 12 
             
             'set word back to normal
            wdApp2.ActiveDocument.Close SaveChanges:=wdDoNotSaveChanges 
            wdApp2.Application.Quit wdDoNotSaveChanges 
             
            wdApp2.Visible = True 
            wdApp2.ScreenUpdating = True 
            Set wdApp2 = Nothing 
             
             
            justFileName = Dir(MyFiles(FNum)) 
            NewFile = "C:\Documents and Settings\beirol\Desktop\Converted\" + justFileName 
            wdApp.ActiveDocument.SaveAs Filename:=NewFile 
            wdApp.ActiveDocument.Close 
            wdApp.Application.Quit wdDoNotSaveChanges 
             
            wdApp.Visible = True 
            wdApp.ScreenUpdating = True 
            Set wdApp = Nothing 
             
        Next FNum 
    End If 
    End Sub
Thanks

Last edited by macropod; 10-18-2012 at 02:35 PM. Reason: Added code tags & formatting
Reply With Quote
  #4  
Old 10-18-2012, 02:57 PM
macropod's Avatar
macropod macropod is offline Auto update Filename field Windows 7 64bit Auto update Filename field Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,963
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

Try the following, which I've streamlined quite a bit:
Code:
 ' Sub for recursing through multiple files in directories, to perform a common task
Sub MassEdit_Loop() 
    Dim MyPath As String, FilesInPath As String, RegEx As String, Template As String, NewFile As String 
    Dim MyFiles() As String 
    Dim FNum As Long 
    Dim MainDoc As Word.Document, MainDoc2 As Word.Document 
    Dim wdApp As Word.Application 
    Dim RngStry As Word.Range, Fld As Word.Field 
    MyPath = "C:\Documents and Settings\beirol\Desktop\Existing" 
    Template = "C:\Documents and Settings\beirol\Desktop\Template\Procedure Template.doc" 
     ' This is the Regex for file searching (e.g. all .doc files in the folder)
    RegEx = "*.doc" 
 
     ' Call upon the Recursive File Name builder
    Dim colFiles As New Collection 
    RecursiveDir colFiles, MyPath, RegEx, True 
     ' Loop through the Filenames to collect them into an Array
    FNum = 0 
    Dim vFile As Variant 
    For Each vFile In colFiles 
        FNum = FNum + 1 
        ReDim Preserve MyFiles(1 To FNum) 
        MyFiles(FNum) = vFile 
    Next vFile 
 
    If FNum > 0 Then 
         ' Start Word
        Set wdApp = CreateObject("word.Application") 
        With wdApp 
            .Visible = False 
            .ScreenUpdating = False 
            For FNum = LBound(MyFiles) To UBound(MyFiles) 
                On Error Resume Next 
                justFileName = Dir(MyFiles(FNum)) 
                NewFile = "C:\Documents and Settings\beirol\Desktop\Converted\" + justFileName 
                Set MainDoc = .Documents.Open(FileName:=Template, AddToRecentFiles:=False) 
 
                 ' Open source document
                Set MainDoc2 = .Documents.Open(FileName:=MyFiles(FNum), AddToRecentFiles:=False) 
 
                 ' Select all data from source doc
                 ' Copy data
                MainDoc2.WholeStory.Copy 
                MainDoc2.Close SaveChanges:=wdDoNotSaveChanges 
                With MainDoc 
                    With .Range 
                        .Paste 
                         'Format the content to Guidelines
                        .Font.Name = "Verdana" 
                        .Font.Size = 10 
                        .ParagraphFormat.LineSpacingRule = wdLineSpaceExactly 
                        .ParagraphFormat.LineSpacing = 12 
                    End With 
                     ' update all fields in document
                    For Each RngStry In .StoryRanges 
                        For Each Fld In RngStry.Fields 
                            Fld.Update 
                        Next 
                    Next 
                    .SaveAs FileName:=NewFile, AddToRecentFiles:=False 
                    .Close 
                End With 
            Next FNum 
            .Quit 
        End With 
        Set wdApp = Nothing 
    End If 
End Sub
PS: When posting code, please use the code tags. They're on the 'Go Advanced' tab.
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #5  
Old 10-19-2012, 03:33 AM
Oliver Beirne Oliver Beirne is offline Auto update Filename field Windows XP Auto update Filename field Office 2007
Novice
Auto update Filename field
 
Join Date: Jan 2012
Posts: 7
Oliver Beirne is on a distinguished road
Default

I'll remember that going forward and thanks very much for your help with this, worked a treat
Reply With Quote
Reply



Similar Threads
Thread Thread Starter Forum Replies Last Post
auto update slide on viewing videoman PowerPoint 0 07-11-2012 06:29 AM
Table of Contents - Direct Field Implementation - Auto-Update Numbering ztag Word 3 01-19-2012 03:04 PM
Auto update Filename field Use a mail merge Field as the final doc filename DaveBF Mail Merge 1 10-22-2011 10:16 PM
Auto Update the date. Nirik Excel 16 12-16-2010 04:23 AM
Project Auto-Update hB-sys Project 0 04-15-2010 06:46 AM

Other Forums: Access Forums

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