#1
|
|||
|
|||
Inserting a Footer if it doesn't exist ???
I'm running MSOffice 365 Home edition.
I have 1000's of Word document where I want to insert a Footer showing FILENAME and PAGE of NUMPAGES. I plan to do this 0) Add the footer with the needed FIELDS to the template 1) Run ONCE a treverse the hieraki of folders with the files 2) check if Footer exists already in each file If NOT then insert the FIELDS, UPDATE the fields SAVE the document again else leave the file be and treverse to next I have the new Footer inserted in the template and the treversing to function ! But the ONE-time-use subrutine to open a document and check for existing Footer is BAD ! I use this code for handlin a file from the hieraki: Code:
Sub insertFooterIfMissing(file As String) Dim appWrd As New Word.Application Dim doc As Word.Document Dim footer As HeaderFooter ' Set appWrd = CreateObject(Word.Application) ' Open the Word document Set doc = appWrd.Documents.Open(file) <<<<<<<< (A) ' Check if theapppWrd footer exists If doc.Sections(1).Footers(wdHeaderFooterPrimary).Exists Then <<<<<< (B) ' just leave it be Else ' insert the Footer here AND update the Footer Debug.Print "Nu indsættes Footer i : " & file End If ' Close the document doc.Close Set doc = Nothing Set appWrd = Nothing End Sub (A) this line is failing showing "macros is not allowed" in a Msgbox (B) is ALWAYS true eventhough the document has NO Footer ... not that I can see at least ! What do I do wrong ? |
#2
|
|||
|
|||
As for (B), by default all new documents have a primary page footer and header even if they contain no visible text.
you might try: If Len(ActiveDocument.Sections(1).Footers(wdHeaderFoo terPrimary).Range.Text) = 1 Then 'Write your footer text End If |
#3
|
|||
|
|||
THX Greg - I'll try !
|
#4
|
|||
|
|||
I think it should be:
If Len(doc.Sections(1).Footers(wdHeaderFooterPrimary) .Range.Text) = 1 Then "doc" instead of "ActiveDocument" - then it seems to get the right Footer = the one in the file from the hieraky. I wonder why the LEN() of an empty Footer is 1 - what is in that Footer ? Still I have the problem in the (A)-line - no ideas how to cure that ? |
#5
|
||||
|
||||
An empty footer will still contain a paragraph mark (which is where the length of 1 comes from).
With regards to your (A) question, we don't know what document you are opening but it is possible that it contains macros or has a template attached that contains macros and the error could be coming from that source.
__________________
Andrew Lockton Chrysalis Design, Melbourne Australia |
#6
|
|||
|
|||
Quote:
The document I try to open is from the folder hieraki and instantiated i the "doc" variable - it opens with the Msgbox telling me macros not allowed, but I can just MANUALLY click the OK-button in that msgbox and the code continues and works. There IS in fact macros in all af the files in the folder hieraki - but how can I allow them to be opened WITHOUT that warning i the msgbox ? |
#7
|
|||
|
|||
Cross-posted at: security - Try open Word doc at my own network by VBA but it opens with macros disabled? - Stack Overflow
For cross-posting etiquette, please read: A message to forum cross posters - Excelguru |
#8
|
|||
|
|||
SOLVED - the code CAN NOT be run from the Normal.dotm without these problems !
I moved the exact same code to a module in Access - and it run without any problems at all ! I see NO reason why ! |
|
Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
=CALLOUTTARGETREF()!Prop doesn't exist | BenMcLean | Visio | 9 | 07-13-2020 04:00 PM |
Saving information into Footer doesn't close footer | Ken Leidner | Word VBA | 2 | 07-13-2018 12:34 PM |
Inserting unicode characters that don't exist in MS Word | jdavid10 | Word | 5 | 01-11-2017 12:14 PM |
Detecting that previous character doesn't exist (i.e., present character is first in document) | Robert K S | Word VBA | 15 | 08-01-2016 09:33 AM |
INCLUDETEXT is file doesn't exist | neoported | Mail Merge | 2 | 07-21-2016 11:47 PM |