473,320 Members | 2,083 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

How to insert tag in word document as per content style

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
10 3100
zmbd
5,501 Expert Mod 4TB
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
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, 866 views)
Nov 5 '13 #3
zmbd
5,501 Expert Mod 4TB
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
5,501 Expert Mod 4TB
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
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
5,501 Expert Mod 4TB
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
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
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
5,501 Expert Mod 4TB
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
Many thanks @zmbd. :)
Nov 12 '13 #11

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

Similar topics

1
by: aparna | last post by:
Hi To All I am doing one task, in that i am having Word document, and want to insert that word document in the database using asp.net.Pleas tell me which data type can i take in database to...
10
by: Noor | last post by:
Hi all, I need to well formatted ms word document through vb.net. All the data that will make the word document will come from the database. and formatted dynamically. Currently i have...
0
by: CognitiveFlux | last post by:
Hi All, I'm pretty new to .Net, not to mention XML, so I truly appreciate any advice! I have user data that I want to selectively insert into a MS Word document in XML. Given the various...
0
by: =?Utf-8?B?V2FubmFiZQ==?= | last post by:
I am creating a webpage and opening it as a Word document. Problem is that as I create one of the questions, some of the text is not appearing in the document as it should. As I step through it, I...
0
by: srk | last post by:
hi to all, i need some help , my problem is that i have to generate a word document withe the data from data base and it should be displayed as tables in word and i have to insert some header and...
17
by: jvieira | last post by:
Hi All I got a MS Word Document and I´d like to do Search and Replace on it with Javascript. Is there any function javascript that do this, to send me?
2
by: allan.s.palmer | last post by:
Hello, I have an asp.net pop up that is generating a Word document real time and sending it to the user by sending the appropriate response headers. When it is run on our development environment...
2
by: uamusa | last post by:
I am Dynamically generating a proposal(report) in MS Word. By default the Paragraph Alignment is "Left". For the First 6 Paragraphs I set the Alignment to "Center", and then when attempting to...
1
by: SowAruna | last post by:
can anyone one help me how to write the below code in C#.NET i am able to create the wordapp and openthe document but further things like compare word document content and not able to.... plz help...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.