By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,504 Members | 1,448 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,504 IT Pros & Developers. It's quick & easy.

How to insert tag in word document as per content style

P: 16
How to insert tag in word document as per content style

Does anyone know the code to insert tags as per style in word.
Nov 1 '13 #1
Share this Question
Share on Google+
10 Replies


zmbd
Expert Mod 5K+
P: 5,287
I'm sorry.
You're not really being very clear.
There are a lot of tags and some are available in one version of word/office and not in others; thus, you need to provide more detail including which version of office and clearly explain your objective.

Attachements will not normally be opened by our experts as quite often we are at our office PCs and such activity is usually restricted/prohibited by company policy.
Nov 4 '13 #2

P: 16
Hi zmbd,

I am using office 2007.

My qusestion is, i just want to insert tag as per content style in word document.

See the attached image for your reference.

Attached Images
File Type: jpg Tag.jpg (31.2 KB, 506 views)
Nov 5 '13 #3

zmbd
Expert Mod 5K+
P: 5,287
Fortunately, you were able to upload the image to the thread; hoewever, unfortunately the image is too small for enough detail to tell what you are doing here.

Please describe in a very detail manner the steps you took to get to the point you were trying to show us in the image. It would also help to see any code/VBA that you have already tried along with any error messages you've received (exactly as given please).
Nov 5 '13 #4

zmbd
Expert Mod 5K+
P: 5,287
Well,
Got to playing with this, from what little I can tell from the OP image.
This works in Word2010.
Open a document and insert a standard module.
When ran, this will open a text file: AppliedStylesAtEachLevel.txt
in the same folder as the word document...
I understand that OP want's this inserted within the document; however, this should provide a basis.

There is an Occational Error 91 when dealing with the "Header 1" thru "Header *" sytles at the Sentence level. The same text formatted as plain or Title appears to be just fine and the individual words don't seem to have an issue with the header format.

So, Here, We, Go:
Expand|Select|Wrap|Line Numbers
  1. Option Explicit
  2. Sub poc201311051339()
  3.     Dim f As Integer
  4.     Dim x As Integer
  5.     Dim y As Integer
  6.     Dim z As Integer
  7.     Dim zdoc As Document
  8.     Dim zpgphs As Paragraphs
  9.     Dim zpgph As Paragraph
  10.     Dim zsens As Sentences
  11.     Dim zwrds As Words
  12.     Dim zrng1 As Range
  13.     Dim zrng2 As Range
  14.     '
  15.     On Error GoTo zerr
  16.     '
  17.     f = FreeFile
  18.     Open "AppliedStylesAtEachLevel.txt" For Output As #f
  19.     '
  20.     Set zdoc = ThisDocument
  21.     Set zpgphs = zdoc.Paragraphs
  22.     Print #f, "Paragraphs:", zdoc.Paragraphs.Count
  23.     Print #f, "Sentences:", zdoc.Sentences.Count
  24.     Print #f, "Words:", zdoc.Words.Count
  25.     Print #f, String(25, "-")
  26.     '
  27.     For Each zpgph In zdoc.Paragraphs
  28.         x = x + 1
  29.         Print #f, "zpgph", x, , , zpgph.Style
  30.         Set zsens = zpgph.Range.Sentences
  31.         For Each zrng1 In zsens
  32.             y = y + 1
  33.             Print #f, "-", "Sntc", y, , zrng1.Style
  34.             Set zwrds = zrng1.Words
  35.             For Each zrng2 In zwrds
  36.                 z = z + 1
  37.                 Print #f, "- -", "- -", z, zrng2.Text, zrng2.Style
  38.             Next zrng2
  39.             Set zwrds = Nothing
  40.             z = 0
  41.         Next zrng1
  42.         y = 0
  43.         Set zsens = Nothing
  44.         Print #f, " "
  45.     Next zpgph
  46.     Close #f
  47.     Set zpgphs = Nothing
  48.     Set zdoc = Nothing
  49. '
  50. Exit Sub
  51. zerr:
  52.     Debug.Print "- - error", Err.Number, Err.Description, Err.Source
  53.     Print #f, "- - error", Err.Number, Err.Description, Err.Source
  54.     Resume Next
  55. End Sub
  56.  
Nov 5 '13 #5

P: 16
Thanks for doing that but i want to insert tag as per style of content, i just created a macro in word 2007 but it is not helpfull so far because when i run it again it taks active document style format. I wanted to run it in whole file with diffrent style.
Expand|Select|Wrap|Line Numbers
  1. Sub Macro6()
  2. '
  3. ' Macro6 Macro
  4. '
  5. '
  6.     Selection.Find.ClearFormatting
  7.     Selection.Find.Style = ActiveDocument.Styles("Heading 1")
  8.     With Selection.Find.ParagraphFormat
  9.         With .Shading
  10.             .Texture = wdTextureNone
  11.             .ForegroundPatternColor = wdColorBlack
  12.             .BackgroundPatternColor = wdColorBlack
  13.         End With
  14.         .Borders.Shadow = False
  15.     End With
  16.     Selection.Find.Replacement.ClearFormatting
  17.     With Selection.Find
  18.         .Text = ""
  19.         .Replacement.Text = "<heading1>^&</headin1>"
  20.         .Forward = True
  21.         .Wrap = wdFindContinue
  22.         .Format = True
  23.         .MatchCase = False
  24.         .MatchWholeWord = False
  25.         .MatchWildcards = False
  26.         .MatchSoundsLike = False
  27.         .MatchAllWordForms = False
  28.     End With
  29.     Selection.Find.Execute
  30.     With Selection
  31.         If .Find.Forward = True Then
  32.             .Collapse Direction:=wdCollapseStart
  33.         Else
  34.             .Collapse Direction:=wdCollapseEnd
  35.         End If
  36.         .Find.Execute Replace:=wdReplaceOne
  37.         If .Find.Forward = True Then
  38.             .Collapse Direction:=wdCollapseEnd
  39.         Else
  40.             .Collapse Direction:=wdCollapseStart
  41.         End If
  42.         .Find.Execute
  43.     End With
  44. End Sub
  45.  
Nov 6 '13 #6

zmbd
Expert Mod 5K+
P: 5,287
kamlesh21630:
1) Yes, I know that you would like the tags inserted inline with the document. I clearly stated that the code provided was only a protype

2) The code I provided should give you the ideas needed to cycle thru the entire document, pull the applied style from the text, and at least store it to another file.
You should be able to modify the code to insert it before a range object within the text file.

3) You are again being very unclear as to what you want to accomplish with your project.
3a) The code you provided looks very much like the code found here Macro for Word problem: replacing or changing styles and obviously deals with applying formating to text within the document - this is not what your original nor subsequent questions are asking.

3b) Is it possible that you are using non-standard syntax to describe you goal? It is VERY importaint that you find, and use, the proper syntax to describe what it is that want to accomplish.
4) I clearly asked you to provide a step by step description as to how you created the document that you posted an image of in post#3. By doing this, you can compensate, to some extent, for non-standard syntax.
Nov 6 '13 #7

P: 16
Thanks a lot @ZMBD, your given link is very helpful. I have got what i wanted to get.

Expand|Select|Wrap|Line Numbers
  1. Sub runallmac()
  2. Call macro6
  3. Call macro7
  4. Call macro8
  5. Call Macro3
  6. End Sub
  7.  
  8.  
  9. Sub macro6()
  10.  
  11. Dim i As Paragraph
  12.   For Each i In ActiveDocument.Paragraphs
  13.         If i.Style.NameLocal = "CM448" Then
  14.             i.Range.Select
  15.             Selection.InsertBefore "<CM448>"
  16.             Selection.InsertAfter "</CM448>"
  17.         End If
  18.     Next
  19. End Sub
  20. Sub macro7()
  21.  
  22. Dim i As Paragraph
  23.   For Each i In ActiveDocument.Paragraphs
  24.         If i.Style.NameLocal = "CM449" Then
  25.             i.Range.Select
  26.             Selection.InsertBefore "<CM449>"
  27.             Selection.InsertAfter "</CM449>"
  28.         End If
  29.     Next
  30. End Sub
  31. Sub macro8()
  32.  
  33. Dim i As Paragraph
  34.   For Each i In ActiveDocument.Paragraphs
  35.         If i.Style.NameLocal = "CM450" Then
  36.             i.Range.Select
  37.             Selection.InsertBefore "<CM450>"
  38.             Selection.InsertAfter "</CM450>"
  39.         End If
  40.     Next
  41. End Sub
  42.  
  43. Sub Macro3()
  44. '
  45. ' Macro3 Macro
  46. '
  47. '
  48.     ChangeFileOpenDirectory "C:\Documents and Settings\User\Desktop\Reverend\"
  49.     ActiveDocument.SaveAs FileName:="Chapter 1.txt", FileFormat:=wdFormatText, _
  50.          LockComments:=False, Password:="", AddToRecentFiles:=True, WritePassword _
  51.         :="", ReadOnlyRecommended:=False, EmbedTrueTypeFonts:=False, _
  52.         SaveNativePictureFormat:=False, SaveFormsData:=False, SaveAsAOCELetter:= _
  53.         False, Encoding:=65001, InsertLineBreaks:=False, AllowSubstitutions:= _
  54.         False, LineEnding:=wdCRLF
  55. End Sub
  56.  
  57.  
1:- In above code i am giving style tag before and after of content and saving that document in text file.

2:- Problem:- but the main problem is:
a:- if i have another style just below of first style it is inserting 2nd style tag before 1st style closing tag, see the below for better understanding.style.

A:-
Style Name Content
CM448------HELLO
CM449------HOW ARE YOU
CM450------I AM FINE

B:-
After running macro

Expand|Select|Wrap|Line Numbers
  1. <CM448>HELLO
  2. <CM449></CM448>HOW ARE YOU
  3. <CM450></CM449>I AM FINE
  4. </CM50>
  5.  
But i want like it below.

Expand|Select|Wrap|Line Numbers
  1. <CM448>HELLO</CM448>
  2. <CM449>HOW ARE YOU</CM449>
  3. <CM450>I AM FINE</CM50>
  4.  
Does it possible, Please help i am working on a project and i'll have to make it anyhow. if you could do so, it would be great help.

Please help.
Nov 10 '13 #8

P: 16
One thing i also want to know that in Macro3 i am saving file but if my file path is new I'll have to give that new path again in code but i want to save file in .txt randomly how to do that:

ChangeFileOpenDirectory "C:\Documents and Settings\User\Desktop\kamlesh\"
Nov 10 '13 #9

zmbd
Expert Mod 5K+
P: 5,287
Good effort.
Word documents can be difficult to work with in the best of cases.

Try the following in a new standard module:
Expand|Select|Wrap|Line Numbers
  1. Option Explicit
  2. Sub poc201311111035()
  3.     Dim zstr As String
  4.     Dim zdoc As Document
  5.     Dim zpgphs As Paragraphs
  6.     Dim zpgph As Paragraph
  7.     Dim zrng As Range
  8.     '
  9. 'This code has been tested in Office 2010(32bit)
  10. 'Windows7-Enterprise(64Bit)
  11.     '
  12.     On Error GoTo zerr
  13.     '
  14.     Set zdoc = ActiveDocument
  15.     Set zpgphs = zdoc.Paragraphs
  16.     '
  17.     Set zdoc = ThisDocument
  18.     For Each zpgph In zdoc.Paragraphs
  19.         Set zrng = zdoc.Range(zpgph.Range.Start, zpgph.Range.End - 1)
  20.         zstr = "<" & zpgph.Style & ">"
  21.         zrng.InsertBefore zstr
  22.         '
  23.         zstr = "</" & zpgph.Style & ">"
  24.         zrng.InsertAfter zstr
  25.         Set zrng = Nothing
  26.     Next zpgph
  27.     '
  28.     Set zpgphs = Nothing
  29.     Set zdoc = Nothing
  30. Exit Sub
  31. zerr:
  32.     Debug.Print "- - error", Err.Number, Err.Description, Err.Source
  33.     Resume Next
  34. End Sub
Now this will not tag sentences within the paragraph... JUST the paragraph formatting.
That is to say:
If the paragraph format is "Normal" and the first sentence of the paragraph is "Title" then you will ONLY get "<Normal>" at the start of the paragraph and </Normal> at the end of the paragraph.
If you want each sentence then you will need to take a look at my code in #5 and combine it with the code here.

Also, notice that I didn't test for style names? I just used them to build a string for the insert. Compair this code to yours.
-z
Nov 11 '13 #10

P: 16
Many thanks @zmbd. :)
Nov 12 '13 #11

Post your reply

Sign in to post your reply or Sign up for a free account.