Share via

select current includetext field?

Anonymous
2013-07-09T20:45:34+00:00

I hope I am following proper etiquette in posing this question about Word 2010 VBA.

When in Print Layout view, I'd like to be able to place my cursor in an INCLUDETEXT field and switch \* MERGFORMAT off using VBA. The field might be part of a paragraph, not a separate paragraph.

If only I could select the field in which the cursor sits, I think I could change the Field.Code to include or exclude the \* MERGEFORMAT string.

But I can't figure out how to select the field in which the cursor sits. I've tried a lot of ways, but I'm stumped. I bet it's really easy, and I'll be very grateful if someone can tell me how.

Thanks in advance for any help, and apologies if I've transgressed netiquette.

Microsoft 365 and Office | Word | For home | Windows

Locked Question. This question was migrated from the Microsoft Support Community. You can vote on whether it's helpful, but you can't add comments or replies or follow the question.

0 comments No comments

13 answers

Sort by: Most helpful
  1. Anonymous
    2013-07-10T00:07:55+00:00

    Ah, thanks Greg and Doug. I got the same error from Doug's code as Greg did.

    Now I see that Greg's code should work through the document until it finds the currently selected field. For some reason, this didn't seem to work on my file. Incidentally, my file is rather large and has hundreds of INCLUDETEXT fields. Word seemed to run through them all quite quickly, but it didn't change the field where the selection pointer (cursor) stood.

    Was this answer helpful?

    0 comments No comments
  2. Anonymous
    2013-07-09T22:54:56+00:00

    Doug,

    Here that returns RTE 5941.

    Joe,  You are not processing each field.  You are only process the field that contains the selection in its results range.  However, this would be better:

    Sub ScratchMacro()

    'A basic Word macro coded by Greg Maxey

    Dim oFld As Word.Field

    For Each oFld In ActiveDocument.Fields

       If Selection.Range.InRange(oFld.Result) Then

          oFld.Code.Text = Replace(oFld.Code.Text, "\* MergeFormat", "")

          oFld.Updat

          Exit For

      End If

    Next oFld

    End Sub

    Was this answer helpful?

    0 comments No comments
  3. Doug Robbins - MVP - Office Apps and Services 323.1K Reputation points MVP Volunteer Moderator
    2013-07-09T22:21:50+00:00

    Use

    With Selection.Fields(1)

       .Code.Text = Replace(.Code.Text, "\* MergeFormat", "")

    End WIth

    Was this answer helpful?

    0 comments No comments
  4. Anonymous
    2013-07-09T21:57:21+00:00

    Thanks for this code, but I don't want to change all fields in the document. I need to change only the field where the cursor (or selection pointer) sits. Any further thoughts on how I might do that? Thanks!

    Was this answer helpful?

    0 comments No comments
  5. Anonymous
    2013-07-09T21:03:57+00:00

    Sub ScratchMacro()

    'A basic Word macro coded by Greg Maxey

    Dim oFld As Word.Field

    For Each oFld In ActiveDocument.Fields

      If Selection.Range.InRange(oFld.Result) Then

        oFld.Code.Text = Replace(oFld.Code.Text, "\* MergeFormat", "")

        Exit For

      End If

      oFld.Update

    Next

    End Sub

    Was this answer helpful?

    0 comments No comments