locked
How to write a text at specific location inside the word document RRS feed

  • Question

  • Hi,

    I am writing a VBA, can anyone tell me on how to write a text to specific location in word document without using table or anything.



    Thanks, Parth

    Wednesday, July 4, 2012 7:57 AM

Answers

  • Hi,

    I think the most convenient way is to use a bookmark.

    • In the file, select the position where you want to enter the text.
    • Then click Insert -> Bookmark, name the bookmark, such as “bk_1”, then click “Add”.
    • Then write code :

    Sub enter_bm()

    ActiveDocument.Bookmarks("bm_1").Range.InsertBefore _

      "Inserted Text"

    End Sub

    • Put the text what you want to enter in the position of the Bookmark.
    • Run the macro.

    For adding a Bookmark, please refer to the link:

    http://office.microsoft.com/en-us/word-help/add-or-delete-bookmarks-HA102269544.aspx?CTT=1


    Jaynet Zhang

    TechNet Community Support


    • Edited by Jaynet Zhang Thursday, July 5, 2012 5:12 AM
    • Marked as answer by Parth Rawal Thursday, July 5, 2012 5:48 AM
    Thursday, July 5, 2012 5:10 AM
  • Hi jaynet,

    You need much more sophisticated code if the inserted text might need to be updated.

    The following macro replaces a specified bookmark’s content with a new text string.

    Sub UpdateBookMark()
    Dim BmkNm As String, NewTxt As String, BmkRng As Range
    BmkNm = InputBox("Bookmark Name")
    NewTxt = InputBox("New Bookmark Text")
    With ActiveDocument
      If .Bookmarks.Exists(BmkNm) Then
        Set BmkRng = .Bookmarks(BmkNm).Range
        BmkRng.Text = NewTxt
        .Bookmarks.Add BmkNm, BmkRng
      Else
        MsgBox "Bookmark: " & BmkNm & " not found."
      End If
    End With
    Set BmkRng = Nothing
    End Sub

    Alternatively, if calling the macro with parameters for the bookmark’s name and new string:

    Sub UpdateBookmark (BmkNm as string, NewTxt as string)
    Dim BmkRng as Range
    With ActiveDocument
      If.Bookmarks.Exists(BmkNm) Then
        Set BmkRng =.Bookmarks(BmkNm).Range
        BmkRng.Text = NewTxt
        .Bookmarks.Add BmkNm, BmkRng
      End if
    End With
    Set BmkRng = Nothing
    End Sub


    Cheers
    Paul Edstein
    [MS MVP - Word]

    • Marked as answer by Jaynet Zhang Thursday, July 5, 2012 8:56 AM
    Thursday, July 5, 2012 5:47 AM

All replies

  • You have to be able to specify the location somehow. If it's the start of end of the document or a Section, or even a page, you can set a range object to that location then update the range, otherwise you could use a table (which you say you don't want to use) or a textbox or bookmark at the desired location. But you can't do it "without using anything".

    Cheers
    Paul Edstein
    [MS MVP - Word]

    Wednesday, July 4, 2012 9:07 AM
  • Can you give me sample code for this..!!?

    Thanks, Parth

    Wednesday, July 4, 2012 9:12 AM
  • Not unless you tell me which variant of the six options I suggested you're interested in pursuing.

    Cheers
    Paul Edstein
    [MS MVP - Word]

    Wednesday, July 4, 2012 12:52 PM
  • Hi,

    I think the most convenient way is to use a bookmark.

    • In the file, select the position where you want to enter the text.
    • Then click Insert -> Bookmark, name the bookmark, such as “bk_1”, then click “Add”.
    • Then write code :

    Sub enter_bm()

    ActiveDocument.Bookmarks("bm_1").Range.InsertBefore _

      "Inserted Text"

    End Sub

    • Put the text what you want to enter in the position of the Bookmark.
    • Run the macro.

    For adding a Bookmark, please refer to the link:

    http://office.microsoft.com/en-us/word-help/add-or-delete-bookmarks-HA102269544.aspx?CTT=1


    Jaynet Zhang

    TechNet Community Support


    • Edited by Jaynet Zhang Thursday, July 5, 2012 5:12 AM
    • Marked as answer by Parth Rawal Thursday, July 5, 2012 5:48 AM
    Thursday, July 5, 2012 5:10 AM
  • Hi jaynet,

    You need much more sophisticated code if the inserted text might need to be updated.

    The following macro replaces a specified bookmark’s content with a new text string.

    Sub UpdateBookMark()
    Dim BmkNm As String, NewTxt As String, BmkRng As Range
    BmkNm = InputBox("Bookmark Name")
    NewTxt = InputBox("New Bookmark Text")
    With ActiveDocument
      If .Bookmarks.Exists(BmkNm) Then
        Set BmkRng = .Bookmarks(BmkNm).Range
        BmkRng.Text = NewTxt
        .Bookmarks.Add BmkNm, BmkRng
      Else
        MsgBox "Bookmark: " & BmkNm & " not found."
      End If
    End With
    Set BmkRng = Nothing
    End Sub

    Alternatively, if calling the macro with parameters for the bookmark’s name and new string:

    Sub UpdateBookmark (BmkNm as string, NewTxt as string)
    Dim BmkRng as Range
    With ActiveDocument
      If.Bookmarks.Exists(BmkNm) Then
        Set BmkRng =.Bookmarks(BmkNm).Range
        BmkRng.Text = NewTxt
        .Bookmarks.Add BmkNm, BmkRng
      End if
    End With
    Set BmkRng = Nothing
    End Sub


    Cheers
    Paul Edstein
    [MS MVP - Word]

    • Marked as answer by Jaynet Zhang Thursday, July 5, 2012 8:56 AM
    Thursday, July 5, 2012 5:47 AM
  • Hi Parth Rawal,

    I think you could try the code of Paul. His code is more helpful than mine. Also thanks Paul to give the suggestion!


    Jaynet Zhang

    TechNet Community Support

    Thursday, July 5, 2012 8:55 AM