Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie
Hi there,
There is an issue with role permissions that is being worked on at the moment.
If you are having trouble with access or permissions on regional forums please post here to get access: https://www.boards.ie/discussion/2058365403/you-do-not-have-permission-for-that#latest

Adding/Editing Comments to Word with VB

  • 01-04-2005 2:50pm
    #1
    Moderators, Arts Moderators Posts: 35,738 Mod ✭✭✭✭


    I'm writing a macro to automatically select certain chunks of text in a Word (2000) doc and apply a comment to the selected chunk. However, I don't want the author's name to be the author of the Document, I want to set it for each comment. Similarly with the Reference/Initials of the comment, I want to specify my own instead of using the default document Initials.
    The Comment object has Author, Reference and Initials fields, but it's not possible to assign values to these when creating a comment. The only permissible parameters are the Range and the Text. I have a way to loop through the whole document, changing this value for each comment, but in the case where I want to add two comments from different authors this is no use:
    Selection.Comments.Add Range:=Selection.Range, Text:=mContent
        
        Set oComments = ActiveDocument.Comments
        For Each oComment In oComments
          oComment.Author = mAuthor
          oComment.Initial = mID
        Next oComment
    

    Ideally I want to reference the comment added in the first line, but the following doesn't work :
    Selection.Comments(1).Author = mAuthor
    
    The syntax is OK, but the debugger tells me that (1) is not a valid member of the comments list (same using 0 )

    Can anyone help?


Comments

  • Registered Users, Registered Users 2 Posts: 15,443 ✭✭✭✭bonkey


    Perhaps:

    Selection.Comments.Item(1).Author = mAuthor

    I don't have a reference to hand, but your syntax actually requires that the .Item property be set as the default property for the collection in question, which - if memory serves - it doesn't necessarily have to be.

    Oh - it also doesn't have to be called .Item, but I'm assuming it will be. Check the object model if there's any doubt.

    jc


  • Registered Users, Registered Users 2 Posts: 15,443 ✭✭✭✭bonkey


    Hmm...I could be wrong...

    Just looked at msdn and they do something similar. However, note that teh comments collection in the samples on that url are all based off ActiveDocument, and not off Selection. Does the Selection object have a Comments collection as a property in teh first place?


  • Moderators, Arts Moderators Posts: 35,738 Mod ✭✭✭✭pickarooney


    I added a watch to selection.comments and one to selection.range.
    There are properties visible for selection.comments.count and selection.range.comments.count but both are 0 immediately after adding the comment.
    Then I noticed that the comment had added a hidden tag [AB1] between the end of my text and my tag at the end, which meant that the range of the comment now fell a few characters outside the range of the selection, thus returning a zero value for comments.count.

    So that's that pretty much solved. Thanks for the help :)


  • Moderators, Arts Moderators Posts: 35,738 Mod ✭✭✭✭pickarooney


    Going to reopen this for a related issue.

    The next part of the macro searches for blocks of text surrounded by specific tags (<r> and </r>) in my case. However, I'm having problems getting the search to work when the closing tag is located within a table. The problematic segment looks like:
    <r>This is the chapter header.
    Here is some more text.
    And then there's a table.
    ____________________
    |text in the table  |
    |and so it ends </r>|
    --------------------
    

    Running a search from within word, with wild cards activated, for
    \<r\>*\</r\> finds all the tagged blocks of text which have no tables, but doesn't find the above segment.

    Can anyone think of a workaround?


  • Registered Users, Registered Users 2 Posts: 7,468 ✭✭✭Evil Phil


    Perhaps, post your code though so we'll have a much better idea of what you are doing.


  • Advertisement
  • Registered Users, Registered Users 2 Posts: 15,443 ✭✭✭✭bonkey


    <r>This is the chapter header.
    Here is some more text.
    And then there's a table.
    ____________________
    |text in the table |
    |and so it ends </r>|
    --------------------
    

    Seems a bit strange that the lower-right vertical bar and the entire bottom "line" of the table fall outside your tags, but anyways...thats your business.
    Running a search from within word, with wild cards activated, for
    \<r\>*\</r\> finds all the tagged blocks of text which have no tables, but doesn't find the above segment.

    Umm....yes, it does find it.

    I copy/pasted that text chunk into a word doc.
    Then I copy/pasted that search chunk into the word Find dialog, and selected to use wildcards.
    Worked fine.
    Can anyone think of a workaround?
    Either its a version-specific bug, a code-vs-doing-it-manually bug, or your code isn't doing what you're describing, because a manual search works fine for me.

    For info, I'm running Word 2003, SP1

    jc


  • Moderators, Arts Moderators Posts: 35,738 Mod ✭✭✭✭pickarooney


    Sorry, that above quoted piece of code was my feeble attempt to represent a Word table in ASCII. Here's the actual section of the DOC attached. I'm working with Word 2000, VB 6.3. The VB code, as it stands, follows. Functions such as getAuthor are just used to parse the tags and extract the author, id, etc. This bit runs OK once the required piece of text is selected, but as I'm stuck on the search routine there's no code for that included - if there was it would just be a standard lump of Find code from a Word macro.
    Sub ReplaceComments()
        
        Dim mID As String
        Dim mContent As String
        Dim mAuthor As String
        Dim bEginner As Integer
        Dim eNder As Integer
        Dim mIddle As Integer
        Dim sillY As Integer
        Dim cComment As Comment
        
        bEginner = Selection.Start ''note start and end points of selection
        eNder = Selection.End
        mIddle = getMiddle()
        Selection.Start = bEginner ''re-expand selection
        Selection.End = eNder
        
        mID = getID() ''read the ID for the selected block
        
        Selection.Start = bEginner
        Selection.End = eNder
        
        mContent = getContent() ''read the content of the comment
        
        Selection.Start = bEginner
        Selection.End = eNder
        
        mAuthor = getAuthor() ''read the author of the comment
        
        Selection.Start = mIddle ''start reading after the tags
        Selection.End = eNder - 4 ''exclude the end tag (could be tidier for tags in tables)
        
        Set cComment = Selection.Comments.Add(Selection.Range, mContent)
        
        cComment.Author = mAuthor
        cComment.Initial = mID
                                 
    End Sub
    


  • Registered Users, Registered Users 2 Posts: 15,443 ✭✭✭✭bonkey


    Sorry, that above quoted piece of code was my feeble attempt to represent a Word table in ASCII.

    No problem...

    Now that I see the attached doc, I can tell you what your problem is...

    A table is considerd by Word as a single in-line object. There is no functionality in Word to search inside the object and outside the object simultaneously.

    You can see what I mean by this if you manually select text outside the table, and extend your selection to include the table. You will find that its impossible to select part of the table as well as the surrounding text.

    Once you go inside the table, you'll find that individual cells are also individual objects. You cannot select all of one cell, and then half of the text in another.

    So....what does this mean in terms of your app....

    Well, you either need the <r> to be after and outside the table (which is the most logical solution to me), or you need to rethink your entire search-strategy and change it to a far, far more complex one.

    I can't see the point of doing the latter, because Word will never allow you to manipulate the region delineated by these tags as a single selection, manually or through code. So logically I can't see why you'd want them where they are.

    jc


  • Moderators, Arts Moderators Posts: 35,738 Mod ✭✭✭✭pickarooney


    Well, if it were up to me, they'd be outside the table. Unfortunately I've no control over the creation of the files, so that probably leaves me with option 3 - select the text manually and run the rest of the macro, or manually move the tags outside before running it. It's not such a problem, as only about 1 in 6 of the tags ends up inside a table in the files I've had so far, and the workload is already cut down a lot with what I have.
    Thanks very much for the explanation, it makes much more sense now when I consider the tables as separate objects. :)


Advertisement