Skip to main content
Participant
July 15, 2020
Answered

html tags to paragraph styles

  • July 15, 2020
  • 2 replies
  • 704 views

I have a Word doc with some simple html tags, ie. <CT> for the chapter title. Is there a way for me to globally change the paragraph following the tag to a existing paragraph style?

This topic has been closed for replies.
Correct answer lfcorullon13651490

Brian is right. Find/Change will do the job. You can also use a script to do this.

Does the paragraph styles names the same of the tag content?

If yes, you can use this:

//DESCRIPTION: Apply pStyle based on tag
//=============================================================
//  Script by Luis Felipe Corullón
//  Contato: lf@corullon.com.br
//  Site: http://scripts.corullon.com.br
//  +55 (51) 9-9685.7565
//  Version: 20200715_2128_GMT-3
//=============================================================

var myScriptName = "Apply pStyle based on tag";
var myIDversion = app.version.split(".")[0];
if (!app.documents.length || (app.documents.length && !app.documents[0].visible)) {
    alert("There is no opened document." , "Script by LFCorullón" , true);
    }
else {
    if (myIDversion < 6) main();
    else app.doScript(main, ScriptLanguage.javascript, undefined, UndoModes.entireScript, myScriptName);
    }

function main() {
	var doc = app.activeDocument;
	
	app.findGrepPreferences = app.changeGrepPreferences = NothingEnum.NOTHING;
	app.findGrepPreferences.findWhat = "<.+?>";
	var f = doc.findGrep();
	app.findGrepPreferences = app.changeGrepPreferences = NothingEnum.NOTHING;
	
	for (var i=0; i<f.length; i++) {
		var c = f[i].contents.replace(/<|>/g , "");
		for (var j=0; j<doc.allParagraphStyles.length; j++) {
			if (doc.allParagraphStyles[j].name == c) {
				f[i].appliedParagraphStyle = doc.allParagraphStyles[j];
				}
			}
		}
		
    }

 

2 replies

lmronan!Author
Participant
July 15, 2020

Thank you. That would certainly work, but I am looking for a quicker, more global way of approaching this. A year or so ago I had found a script, lost it, and can't seem to google it up, again.

lfcorullon13651490
lfcorullon13651490Correct answer
Legend
July 16, 2020

Brian is right. Find/Change will do the job. You can also use a script to do this.

Does the paragraph styles names the same of the tag content?

If yes, you can use this:

//DESCRIPTION: Apply pStyle based on tag
//=============================================================
//  Script by Luis Felipe Corullón
//  Contato: lf@corullon.com.br
//  Site: http://scripts.corullon.com.br
//  +55 (51) 9-9685.7565
//  Version: 20200715_2128_GMT-3
//=============================================================

var myScriptName = "Apply pStyle based on tag";
var myIDversion = app.version.split(".")[0];
if (!app.documents.length || (app.documents.length && !app.documents[0].visible)) {
    alert("There is no opened document." , "Script by LFCorullón" , true);
    }
else {
    if (myIDversion < 6) main();
    else app.doScript(main, ScriptLanguage.javascript, undefined, UndoModes.entireScript, myScriptName);
    }

function main() {
	var doc = app.activeDocument;
	
	app.findGrepPreferences = app.changeGrepPreferences = NothingEnum.NOTHING;
	app.findGrepPreferences.findWhat = "<.+?>";
	var f = doc.findGrep();
	app.findGrepPreferences = app.changeGrepPreferences = NothingEnum.NOTHING;
	
	for (var i=0; i<f.length; i++) {
		var c = f[i].contents.replace(/<|>/g , "");
		for (var j=0; j<doc.allParagraphStyles.length; j++) {
			if (doc.allParagraphStyles[j].name == c) {
				f[i].appliedParagraphStyle = doc.allParagraphStyles[j];
				}
			}
		}
		
    }

 

brian_p_dts
Community Expert
Community Expert
July 15, 2020

Find/Replace should do. Find the tag, replace with the same tag and change the paragraph style to the desired one?