How to insert tag in word document as per content style
Does anyone know the code to insert tags as per style in word.
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.
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. 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).
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: - Option Explicit
-
Sub poc201311051339()
-
Dim f As Integer
-
Dim x As Integer
-
Dim y As Integer
-
Dim z As Integer
-
Dim zdoc As Document
-
Dim zpgphs As Paragraphs
-
Dim zpgph As Paragraph
-
Dim zsens As Sentences
-
Dim zwrds As Words
-
Dim zrng1 As Range
-
Dim zrng2 As Range
-
'
-
On Error GoTo zerr
-
'
-
f = FreeFile
-
Open "AppliedStylesAtEachLevel.txt" For Output As #f
-
'
-
Set zdoc = ThisDocument
-
Set zpgphs = zdoc.Paragraphs
-
Print #f, "Paragraphs:", zdoc.Paragraphs.Count
-
Print #f, "Sentences:", zdoc.Sentences.Count
-
Print #f, "Words:", zdoc.Words.Count
-
Print #f, String(25, "-")
-
'
-
For Each zpgph In zdoc.Paragraphs
-
x = x + 1
-
Print #f, "zpgph", x, , , zpgph.Style
-
Set zsens = zpgph.Range.Sentences
-
For Each zrng1 In zsens
-
y = y + 1
-
Print #f, "-", "Sntc", y, , zrng1.Style
-
Set zwrds = zrng1.Words
-
For Each zrng2 In zwrds
-
z = z + 1
-
Print #f, "- -", "- -", z, zrng2.Text, zrng2.Style
-
Next zrng2
-
Set zwrds = Nothing
-
z = 0
-
Next zrng1
-
y = 0
-
Set zsens = Nothing
-
Print #f, " "
-
Next zpgph
-
Close #f
-
Set zpgphs = Nothing
-
Set zdoc = Nothing
-
'
-
Exit Sub
-
zerr:
-
Debug.Print "- - error", Err.Number, Err.Description, Err.Source
-
Print #f, "- - error", Err.Number, Err.Description, Err.Source
-
Resume Next
-
End Sub
-
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. -
Sub Macro6()
-
'
-
' Macro6 Macro
-
'
-
'
-
Selection.Find.ClearFormatting
-
Selection.Find.Style = ActiveDocument.Styles("Heading 1")
-
With Selection.Find.ParagraphFormat
-
With .Shading
-
.Texture = wdTextureNone
-
.ForegroundPatternColor = wdColorBlack
-
.BackgroundPatternColor = wdColorBlack
-
End With
-
.Borders.Shadow = False
-
End With
-
Selection.Find.Replacement.ClearFormatting
-
With Selection.Find
-
.Text = ""
-
.Replacement.Text = "<heading1>^&</headin1>"
-
.Forward = True
-
.Wrap = wdFindContinue
-
.Format = True
-
.MatchCase = False
-
.MatchWholeWord = False
-
.MatchWildcards = False
-
.MatchSoundsLike = False
-
.MatchAllWordForms = False
-
End With
-
Selection.Find.Execute
-
With Selection
-
If .Find.Forward = True Then
-
.Collapse Direction:=wdCollapseStart
-
Else
-
.Collapse Direction:=wdCollapseEnd
-
End If
-
.Find.Execute Replace:=wdReplaceOne
-
If .Find.Forward = True Then
-
.Collapse Direction:=wdCollapseEnd
-
Else
-
.Collapse Direction:=wdCollapseStart
-
End If
-
.Find.Execute
-
End With
-
End Sub
-
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.
Thanks a lot @ZMBD, your given link is very helpful. I have got what i wanted to get. -
Sub runallmac()
-
Call macro6
-
Call macro7
-
Call macro8
-
Call Macro3
-
End Sub
-
-
-
Sub macro6()
-
-
Dim i As Paragraph
-
For Each i In ActiveDocument.Paragraphs
-
If i.Style.NameLocal = "CM448" Then
-
i.Range.Select
-
Selection.InsertBefore "<CM448>"
-
Selection.InsertAfter "</CM448>"
-
End If
-
Next
-
End Sub
-
Sub macro7()
-
-
Dim i As Paragraph
-
For Each i In ActiveDocument.Paragraphs
-
If i.Style.NameLocal = "CM449" Then
-
i.Range.Select
-
Selection.InsertBefore "<CM449>"
-
Selection.InsertAfter "</CM449>"
-
End If
-
Next
-
End Sub
-
Sub macro8()
-
-
Dim i As Paragraph
-
For Each i In ActiveDocument.Paragraphs
-
If i.Style.NameLocal = "CM450" Then
-
i.Range.Select
-
Selection.InsertBefore "<CM450>"
-
Selection.InsertAfter "</CM450>"
-
End If
-
Next
-
End Sub
-
-
Sub Macro3()
-
'
-
' Macro3 Macro
-
'
-
'
-
ChangeFileOpenDirectory "C:\Documents and Settings\User\Desktop\Reverend\"
-
ActiveDocument.SaveAs FileName:="Chapter 1.txt", FileFormat:=wdFormatText, _
-
LockComments:=False, Password:="", AddToRecentFiles:=True, WritePassword _
-
:="", ReadOnlyRecommended:=False, EmbedTrueTypeFonts:=False, _
-
SaveNativePictureFormat:=False, SaveFormsData:=False, SaveAsAOCELetter:= _
-
False, Encoding:=65001, InsertLineBreaks:=False, AllowSubstitutions:= _
-
False, LineEnding:=wdCRLF
-
End Sub
-
-
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 - <CM448>HELLO
-
<CM449></CM448>HOW ARE YOU
-
<CM450></CM449>I AM FINE
-
</CM50>
-
But i want like it below. -
<CM448>HELLO</CM448>
-
<CM449>HOW ARE YOU</CM449>
-
<CM450>I AM FINE</CM50>
-
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.
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\"
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: - Option Explicit
-
Sub poc201311111035()
-
Dim zstr As String
-
Dim zdoc As Document
-
Dim zpgphs As Paragraphs
-
Dim zpgph As Paragraph
-
Dim zrng As Range
-
'
-
'This code has been tested in Office 2010(32bit)
-
'Windows7-Enterprise(64Bit)
-
'
-
On Error GoTo zerr
-
'
-
Set zdoc = ActiveDocument
-
Set zpgphs = zdoc.Paragraphs
-
'
-
Set zdoc = ThisDocument
-
For Each zpgph In zdoc.Paragraphs
-
Set zrng = zdoc.Range(zpgph.Range.Start, zpgph.Range.End - 1)
-
zstr = "<" & zpgph.Style & ">"
-
zrng.InsertBefore zstr
-
'
-
zstr = "</" & zpgph.Style & ">"
-
zrng.InsertAfter zstr
-
Set zrng = Nothing
-
Next zpgph
-
'
-
Set zpgphs = Nothing
-
Set zdoc = Nothing
-
Exit Sub
-
zerr:
-
Debug.Print "- - error", Err.Number, Err.Description, Err.Source
-
Resume Next
-
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
Sign in to post your reply or Sign up for a free account.
Similar topics
by: B Deepak |
last post by:
|
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...
|
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...
|
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...
|
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...
|
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...
|
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?
|
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...
|
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...
|
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...
|
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...
|
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...
|
by: ryjfgjl |
last post by:
ExcelToDatabase: batch import excel into database automatically...
|
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...
|
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)...
|
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...
|
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....
|
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
|
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...
| |