Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 06-21-2023, 12:34 PM
ksor ksor is offline Inserting a Footer if it doesn't exist ??? Windows 10 Inserting a Footer if it doesn't exist ??? Office 2016
Advanced Beginner
Inserting a Footer if it doesn't exist ???
 
Join Date: Feb 2018
Location: Århus V, Denmark
Posts: 74
ksor is on a distinguished road
Default 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 ?
Reply With Quote
  #2  
Old 06-21-2023, 01:44 PM
gmaxey gmaxey is offline Inserting a Footer if it doesn't exist ??? Windows 10 Inserting a Footer if it doesn't exist ??? Office 2019
Expert
 
Join Date: May 2010
Location: Brasstown, NC
Posts: 1,429
gmaxey is a jewel in the roughgmaxey is a jewel in the roughgmaxey is a jewel in the roughgmaxey is a jewel in the rough
Default

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
__________________
Greg Maxey
Please visit my web site at http://www.gregmaxey.com/
Reply With Quote
  #3  
Old 06-21-2023, 09:56 PM
ksor ksor is offline Inserting a Footer if it doesn't exist ??? Windows 10 Inserting a Footer if it doesn't exist ??? Office 2016
Advanced Beginner
Inserting a Footer if it doesn't exist ???
 
Join Date: Feb 2018
Location: Århus V, Denmark
Posts: 74
ksor is on a distinguished road
Default

THX Greg - I'll try !
Reply With Quote
  #4  
Old 06-21-2023, 10:55 PM
ksor ksor is offline Inserting a Footer if it doesn't exist ??? Windows 10 Inserting a Footer if it doesn't exist ??? Office 2016
Advanced Beginner
Inserting a Footer if it doesn't exist ???
 
Join Date: Feb 2018
Location: Århus V, Denmark
Posts: 74
ksor is on a distinguished road
Default

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 ?
Reply With Quote
  #5  
Old 06-22-2023, 01:29 AM
Guessed's Avatar
Guessed Guessed is offline Inserting a Footer if it doesn't exist ??? Windows 10 Inserting a Footer if it doesn't exist ??? Office 2016
Expert
 
Join Date: Mar 2010
Location: Canberra/Melbourne Australia
Posts: 3,977
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

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
Reply With Quote
  #6  
Old 06-22-2023, 04:35 AM
ksor ksor is offline Inserting a Footer if it doesn't exist ??? Windows 10 Inserting a Footer if it doesn't exist ??? Office 2016
Advanced Beginner
Inserting a Footer if it doesn't exist ???
 
Join Date: Feb 2018
Location: Århus V, Denmark
Posts: 74
ksor is on a distinguished road
Default

Quote:
Originally Posted by Guessed View Post
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.

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 ?
Reply With Quote
  #7  
Old 06-26-2023, 05:45 AM
Italophile Italophile is offline Inserting a Footer if it doesn't exist ??? Windows 11 Inserting a Footer if it doesn't exist ??? Office 2021
Expert
 
Join Date: Mar 2022
Posts: 338
Italophile is just really niceItalophile is just really niceItalophile is just really niceItalophile is just really nice
Default

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
Reply With Quote
  #8  
Old 07-08-2023, 12:50 AM
ksor ksor is offline Inserting a Footer if it doesn't exist ??? Windows 10 Inserting a Footer if it doesn't exist ??? Office 2016
Advanced Beginner
Inserting a Footer if it doesn't exist ???
 
Join Date: Feb 2018
Location: Århus V, Denmark
Posts: 74
ksor is on a distinguished road
Default

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 !
Reply With Quote
Reply



Similar Threads
Thread Thread Starter Forum Replies Last Post
=CALLOUTTARGETREF()!Prop doesn't exist BenMcLean Visio 9 07-13-2020 04:00 PM
Inserting a Footer if it doesn't exist ??? 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

Other Forums: Access Forums

All times are GMT -7. The time now is 02:56 PM.


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