Need to insert style name at starting and ending of paragraph

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Venkatesan82
    New Member
    • Feb 2016
    • 4

    Need to insert style name at starting and ending of paragraph

    I would like to insert relevant style tag at starting and ending of each paragraph. E.g.,If you find style name of paragraph as "Head1", the output should like below.

    <Head1>Text</Head1>

    I would like to convert for all complete documents, not only single style. Please provide me macro for this process.
  • zmbd
    Recognized Expert Moderator Expert
    • Mar 2012
    • 5501

    #2
    Venkatesan82
    + Office version?

    + Which application, as this isn't an Access property and how to approach this is entirely different between Word, PowerPoint, Outlook, etc...

    + Asking for completed code is also not encouraged. You should show what work you've attempted. This way we have a better idea as to what it is that you are really attempting to accomplish.
    Last edited by zmbd; Feb 1 '16, 11:49 AM.

    Comment

    • Venkatesan82
      New Member
      • Feb 2016
      • 4

      #3
      I'm using Microsoft word 2010 version. I have tried for below macro. Please advice.
      ----------
      Code:
      Sub StyleToTag()
              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
      Last edited by Rabbit; Feb 1 '16, 08:48 PM. Reason: Please use [code] and [/code] tags when posting code or formatted data.

      Comment

      • zmbd
        Recognized Expert Moderator Expert
        • Mar 2012
        • 5501

        #4
        There's nothing wrong with this code.

        It in fact, this code closely mirrors code that I have used in the past when troubleshooting our SOP-Templates for the lab.
        >> actually it appears to be:
        How to insert tag in word document as per content style - Post#10

        Can you tell us what errors or issues you are experiencing?

        How have you attempted to use this code within the document?


        BTW: I know that many refer to VBA code as Macro code - IMHO, it is better to refer to it as VBA. Using the term "Macro", causes a great deal of confusion when one moves from Excel/Word/PowerPoint to Access where the two, "Macro" and "VBA," are not the same programming language.
        Last edited by zmbd; Feb 2 '16, 01:06 AM.

        Comment

        • Venkatesan82
          New Member
          • Feb 2016
          • 4

          #5
          I could not see any action in word document after running this macro. Is it technical issue? Please confirm.

          Comment

          • zmbd
            Recognized Expert Moderator Expert
            • Mar 2012
            • 5501

            #6
            This code works in Word from versions 2003 thru 2013
            Windows XP thru Windows 8.1 personal and enterprise equivalents.

            This is not a technical issue with the code. More likely it is how you are attempting to use the code.... which you did not explain per my last request.

            >> The code should be placed in the document in question.
            >> Make sure the "Active Document" is the document you want the code to act against. If you have more than one document open, this code will attempt to run against the currently active document.
            >> Your macro security should be set to allow the VBA to execute. In newer versions of Office this is controlled in the "Trust Center" under "Macro Settings." I usually set this as "Disable All Macros With Notification" I find that the other settings are either too dangerous or too restrictive for my applications.

            > Open your document
            > Open the VBA Editor (VBE), [Alt][F11]
            + Once VBE is open, {Menu Bar\Tools\Optio ns}
            - IN the Options dialog, "Editor Tab"
            - Unselect "Auto Syntax Check" -- don't worry, your errors will still flag "red" just no pop-up message for every error
            - Select "Require Variable Declaration" From now on, you will have to declaire the variables using a Dim(Read More)
            -- Optional> increase tab width to 3 or 4. I like a little more pronounced code stepping.
            - Select [OK]

            ++ Selecting the options as given will help tremendously with trouble shooting and proper coding. There is more information about this in the trouble shooting section of [*]> Before Posting (VBA or SQL) Code

            > Insert a module in to the correct document
            Usually along the right hand side of the VBE is the project pane that shows the tree that typically shows the Normal template object, the current document object Project(documen tname) wherein "documentna me" refers to the File Name of the document you are working with, and any other objects/addins associated with your installation.
            + Expand the (Project(docume ntname)) branch. You should see branches named "Microsoft Word Objects" and "References "
            - Right click on the (Project(docume ntname)) branch and within the quick menu select {Insert\Module}
            - Expand the (Project(docume ntname)) branch if you have not already done so, there should now be a "Modules" object. Expand the "Modules" object branch... there should be at least one "Module#" object where # is the sequence number of the inserted module - I want you to be using a new, empty module; thus, I will refer to this new, empty module as Module# - please interprete this as the new, empty module you just created replacing the # sign with the corresponding sequence number.
            - Right click on Module#, select {View Code}
            - You should see "[Module# (Code)]" in the VBE Title bar. If not then you are in the wrong code pane. Close all active code panes and then Right click on Module#, select {View Code}
            - This pane should contain Option Explicit and no other code.
            - Insert the code as provided in How to insert tag in word document as per content style - Post#10
            - Press <Ctrl><G> to open the Immediate Pane.

            > Minimize the VBE
            > Bring the document you wish to run this code against to the front
            >{Ribbon\View\M acros\Macros}
            - Macros dialog box
            -- Select [StyleToTag] in the list
            -- Select Run

            >> At this point you should see the style tags at the paragraph level. If not then maximize the VBE, see if there are any errors reported in the immediate pane:
            For example, paragraphs within a table you might see:
            Code:
            - - error      5251         This is not a valid action for the end of a row.        Microsoft Word
            as the code reaches the end of the table row. This occurs because tables are not paragraphs; thus, they do not fall under this code's scope.
            Last edited by zmbd; Feb 2 '16, 04:36 PM.

            Comment

            • Venkatesan82
              New Member
              • Feb 2016
              • 4

              #7
              Thanks for your update. After opening my document in word 2010, i goes to View--->Macro from the tab

              1) Click on view macro
              2) And then create new macro
              3) And then paste my macro within it.
              4) Run macro

              There is no action and i could not get any error also. I feel it is valuable macro, but i unable to find out the solution. Could you please help me?

              Comment

              • zmbd
                Recognized Expert Moderator Expert
                • Mar 2012
                • 5501

                #8
                Please follow the instructions in my last post.
                I've re-ran the vb-script:
                From my archive copy
                From the https://bytes.com/topic/access/answe...le#post3760578 thread
                and from post#3
                Both from my home installations and from my work installations of Office. In all six cases, the code has executed as expected.

                Either there is something wrong with your Office-Word installation or you have the VBA scripting disabled. There are no other obvious explanations.
                Last edited by zmbd; Feb 4 '16, 03:40 AM.

                Comment

                • zmbd
                  Recognized Expert Moderator Expert
                  • Mar 2012
                  • 5501

                  #9
                  > One thought here.
                  + Open your document that contains this script
                  + Open the VBE (<alt><F11>)
                  + Typically along the left side is the Project Pane
                  + In the project pane, select the branch
                  Project(YourDoc umentNameHere)
                  + MenuBar>Debug>C ompile Project

                  +> See if there are any errors that occur
                  (These are the same trouble shooting steps you'll find in the Before Posting (VBA or SQL) Code thread)

                  You already should be receiving compiler errors if there's something amiss with the code; however, let's just force the issue here :)

                  Comment

                  Working...