Comments for DOCX not working

I cannot get either the API or Ruby SDK working when attempting to add comments to a word doc. I have tried using simple coordinates 0.0.0.0 and using the coordinates from a text search. For the Ruby SDK, I have used the nodes and document positions returned from the text search directly and built them with the SDK tooling. Regardless of what I do, I am getting this error message back: "Message\": \"Object reference not set to an instance of an object.. Finding text and getting all comments work as expected.

This is the Ruby code I am using:

 range_start = AsposeWordsCloud::DocumentPosition.new({ NodeId: node.range_start.node.node_id, Offset: node.range_start.offset })
    range_end = AsposeWordsCloud::DocumentPosition.new({ NodeId: node.range_end.node.node_id, Offset: node.range_end.offset })
    request_comment = AsposeWordsCloud::CommentInsert.new({
                                                            RangeStart: range_start,
                                                            RangeEnd: range_end,
                                                            Initial: 'IA',
                                                            Author: 'John Doe',
                                                            Text: 'A new Comment'
                                                          })
    begin
      Tempfile.create(['document', '.docx']) do |temp_file_2|
        temp_file_2.binmode
        temp_file_2.write(@document)
        temp_file_2.rewind
        insert_request = AsposeWordsCloud::InsertCommentOnlineRequest.new(document: @document, comment: request_comment)
        @words_api.insert_comment_online(insert_request)
      end
    rescue StandardError => e
      error_info = {
        error: e.message,
        class: e.class.name,
        backtrace: e.backtrace&.take(1)
      }
      error_info[:response_body] = e.response_body if e.respond_to?(:response_body)
      return { success: false, error: "Upload failed: #{error_info.inspect}", debug_info: @debug_info }

This is the version I attempted with the API call:


  def get_aspose_token(client_id, client_secret)
    response = HTTParty.post(
      'https://api.aspose.cloud/connect/token',
      body: {
        grant_type: 'client_credentials',
        client_id: client_id,
        client_secret: client_secret
      }
    )
    raise "Token error: #{response.body}" unless response.code == 200
    JSON.parse(response.body)['access_token']
  end

  def add_comment_via_httparty(temp_file_path, node_id, offset, author, initials, text, token)
    url = 'https://api.aspose.cloud/v4.0/words/online/post/comments'
    comment = {
      'RangeStart' => { 'Node' => { 'NodeId' => node_id }, 'Offset' => offset },
      'RangeEnd'   => { 'Node' => { 'NodeId' => node_id }, 'Offset' => offset },
      'Author'     => author,
      'Initial'    => initials,
      'Text'       => text
    }
    HTTParty.put(
      url,
      headers: { 'Authorization' => "Bearer #{token}" },
      body: {
        'document' => File.open(temp_file_path, 'rb'),
        'comment'  => comment.to_json
      },
      multipart: true
    )
  end

NOTE: I have tried this with EVERY variation of sample values I have found in the documentation AND with the exact API request that is in the documentation. None of that has changed the fact that this this is not working. If you give an example in the response, please use the API not the Java version.

@rachel.cp

It seems you are encountering an issue when trying to add comments to a Word document using the Aspose API or Ruby SDK. The error message you are receiving, "Object reference not set to an instance of an object," typically indicates that there is a null reference in your code, which could be due to incorrect handling of the document nodes or comment ranges.

Here are a few suggestions to help you troubleshoot and resolve the issue:

  1. Ensure Node Validity: Before creating the DocumentPosition objects for RangeStart and RangeEnd, make sure that the node.range_start.node and node.range_end.node are not null. You can add checks to confirm that these nodes exist.

  2. Correctly Set Offsets: When setting the offsets for RangeStart and RangeEnd, ensure that the offsets are valid and correspond to the actual text positions in the document. If the offsets are out of bounds or incorrect, it may lead to null references.

  3. Use CommentRangeStart and CommentRangeEnd: Instead of directly using RangeStart and RangeEnd, consider using the CommentRangeStart and CommentRangeEnd classes to define the range for your comment. This is a common practice when working with comments in Aspose.

  4. Check API Documentation: Review the API documentation for examples on how to properly add comments. The documentation provides detailed examples that can guide you in structuring your code correctly.

Here’s a simplified example of how to add a comment using the CommentRangeStart and CommentRangeEnd classes:

# Assuming 'doc' is your Document object and 'builder' is a DocumentBuilder
comment = AsposeWordsCloud::Comment.new({
  Author: 'John Doe',
  Initial: 'JD',
  Text: 'This is a comment.'
})

# Create a range for the comment
comment_range_start = AsposeWordsCloud::CommentRangeStart.new(doc, comment.Id)
comment_range_end = AsposeWordsCloud::CommentRangeEnd.new(doc, comment.Id)

# Insert the comment range start and end in the document
builder.CurrentParagraph.AppendChild(comment_range_start)
builder.CurrentParagraph.AppendChild(new Run(doc, "Text to comment on"))
builder.CurrentParagraph.AppendChild(comment_range_end)
builder.CurrentParagraph.AppendChild(comment)

# Save the document
doc.Save("output.docx")

Make sure to adapt the example to fit your specific use case and ensure that all nodes and offsets are valid. If you continue to face issues, consider checking the Aspose forums or documentation for additional insights or similar cases.

If you need further assistance, feel free to ask!