Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 07-09-2024, 04:46 PM
WordUser789 WordUser789 is offline The command "ActiveDocument.AttachedTemplate" causes macro to skip to the end Windows 10 The command "ActiveDocument.AttachedTemplate" causes macro to skip to the end Office 2016
Novice
The command "ActiveDocument.AttachedTemplate" causes macro to skip to the end
 
Join Date: Jun 2017
Posts: 25
WordUser789 is on a distinguished road
Default The command "ActiveDocument.AttachedTemplate" causes macro to skip to the end

I have a short macro that updates the styles in a document with those in the attached template:



Code:
With ActiveDocument
        .UpdateStylesOnOpen = True
        .AttachedTemplate = aTmp
    End With
    With ActiveDocument
        .UpdateStylesOnOpen = False
        .AttachedTemplate = aTmp
    End With
This works fine, however if I am using F8 to step through the code, once I press F8 on the .AttachedTemplate line, it will run the rest of the code to the end. Not such an issue with such a short macro however sometimes this is part of a larger macro. The only way I can check anything after it is to set another break point in the code after this step so it's forced to stop.

The other issue I'm finding is that when I run it as part of the larger macro and it is actually switching the URL of a template, it will run the first half (setting update styles to true) and then stop. Setting a breakpoint after the step in this instance doesn't have any effect: anything after it isn't run.

If I run the same macro again, it will complete all the steps: the only thing that's different is that it hasn't changed the template.

Any clues?
Reply With Quote
  #2  
Old 07-09-2024, 11:42 PM
Guessed's Avatar
Guessed Guessed is offline The command "ActiveDocument.AttachedTemplate" causes macro to skip to the end Windows 10 The command "ActiveDocument.AttachedTemplate" causes macro to skip to the end Office 2016
Expert
 
Join Date: Mar 2010
Location: Canberra/Melbourne Australia
Posts: 4,158
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

Firstly, you can import styles from attached template more simply
Code:
ActiveDocument.UpdateStyles              'Imports styles from the attached template
Secondly, where is the code you are running residing? If it is in the attached template and you then change the attached template, how does it stay available? Perhaps place the code in Normal, an addin, or document if it includes changing the attached template.
__________________
Andrew Lockton
Chrysalis Design, Melbourne Australia
Reply With Quote
  #3  
Old 07-12-2024, 12:20 AM
WordUser789 WordUser789 is offline The command "ActiveDocument.AttachedTemplate" causes macro to skip to the end Windows 10 The command "ActiveDocument.AttachedTemplate" causes macro to skip to the end Office 2016
Novice
The command "ActiveDocument.AttachedTemplate" causes macro to skip to the end
 
Join Date: Jun 2017
Posts: 25
WordUser789 is on a distinguished road
Default

Thanks for the tip on updating the styles with the single command! My journey into macros started with recording them (like a lot of others I'm sure) and that's what it records when you do the manual procedure and I never looked into anything more efficient.

I have a fairly complicated setup which tries to deal with all sorts of eventualities and they're all to do with SharePoint and how it tries its best to mess with templates and styles. The master "refresh" macro lives in a separate template that people have on their own computers (in the Word startup folder) and is tasked with first linking the correct template.

The document templates all have their own macro which does the actual refreshing, as there's no one size fits all process for the refreshing (sometimes the document has custom numbering which we don't want to reset with a refresh, or sometimes some substitution needs to be done with SharePoint paths as the synced folder name and the URL don't match if someone renames the site). That's called by the master macro.

Your simple solution will work most of the time, however there's a few instances where the document is linked to a template with a valid update macro, but I want to change it to a different one. I think this would have to involve using my method, which is where the issue becomes relevant again.
Reply With Quote
  #4  
Old 07-12-2024, 08:36 PM
Guessed's Avatar
Guessed Guessed is offline The command "ActiveDocument.AttachedTemplate" causes macro to skip to the end Windows 10 The command "ActiveDocument.AttachedTemplate" causes macro to skip to the end Office 2016
Expert
 
Join Date: Mar 2010
Location: Canberra/Melbourne Australia
Posts: 4,158
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

I'm not fully understanding what you are saying but one other tip that might be useful is the ability to import styles from a template other than the 'attached template'
Code:
ActiveDocument.CopyStylesFromTemplate (strTemplateFullPath)
This would allow you to import styles without changing the attached template first.
__________________
Andrew Lockton
Chrysalis Design, Melbourne Australia
Reply With Quote
  #5  
Old 07-14-2024, 11:13 PM
WordUser789 WordUser789 is offline The command "ActiveDocument.AttachedTemplate" causes macro to skip to the end Windows 10 The command "ActiveDocument.AttachedTemplate" causes macro to skip to the end Office 2016
Novice
The command "ActiveDocument.AttachedTemplate" causes macro to skip to the end
 
Join Date: Jun 2017
Posts: 25
WordUser789 is on a distinguished road
Default

Yep I guess that would work: if I make attaching the new template the very last thing the macro does then it doesn't matter if that step will stop it running (it executes the procedure so that's the main thing).

I am mainly curious as to why this step causes a macro to behave like you've pressed F5 instead of F8 at that line when stepping through the code.
Reply With Quote
  #6  
Old 07-15-2024, 05:09 PM
Guessed's Avatar
Guessed Guessed is offline The command "ActiveDocument.AttachedTemplate" causes macro to skip to the end Windows 10 The command "ActiveDocument.AttachedTemplate" causes macro to skip to the end Office 2016
Expert
 
Join Date: Mar 2010
Location: Canberra/Melbourne Australia
Posts: 4,158
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

I've encountered your 'stepping through code' issue when debugging lots of different macros and haven't gone looking for a reason why. As you pointed out you can add breakpoints so I've never been particularly bothered by the runaway train.

In the cases I've seen this happen, the runaway hasn't been related to changing attached templates so there is more than one trigger for this behaviour.
__________________
Andrew Lockton
Chrysalis Design, Melbourne Australia
Reply With Quote
Reply

Tags
attachedtemplate, debugging



Similar Threads
Thread Thread Starter Forum Replies Last Post
In a macro to clean up language, what functions do "ClearFormatting" and "Replacement" serve? ctviggen Word VBA 2 12-04-2023 10:06 AM
Modifying a Macro that Splits Word Docs by Heading to Skip Some Headings ("Bibliographies") learnerlamp Word VBA 0 10-18-2023 01:20 PM
Excel 2003: VBA "Function" causes "#VALUE!" errors after running "insert/delete row" custom macro Matt C Excel Programming 2 01-08-2022 06:03 AM
The command "ActiveDocument.AttachedTemplate" causes macro to skip to the end How do you use the "seq" command to start a list at "2" (not "1")? techwriter3k Word 4 05-22-2020 07:59 AM
remove repeated words with " macro " or " wild cards " in texts with parentheses and commas jocke321 Word VBA 2 12-10-2014 11:27 AM

Other Forums: Access Forums

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