VBA to copy text from excel to on specific location in wordfile

后端 未结 2 1708
死守一世寂寞
死守一世寂寞 2021-01-28 14:50

Problem: Pasting copied data from excel to specific location in a word file.

Currently I have code which can paste the value, but it does so to \"paragr

相关标签:
2条回答
  • 2021-01-28 15:25

    Word gives a number to each character in the document's body, from 1 up. It then defines a range with Range.Start to Range.End So, Paragraphs(1).Range might be equal to Range(Start:=1, End:=120).

    The text contained in that range is Range.Text, Read/Write. Therefore, Paragraphs(1).Range.Text = "My new paragraph text" will replace the existing text in the document's first paragraph. ActiveDocument.Range(0, 0).Text specifies the range before the first character in the document.

    In order to insert text at a specific location you have to find the location, meaning the Range. As you have seen above, if the range has a length of 0 you can insert before or between existing text, and if it has any length the new text will replace whatever was there before. New and old text need not have the same length.

    Counting paragraphs is helpful to find a range. You can also count words or sentences. You can search for a specific word combination. Or you can use a bookmark. In all of these cases you define a range the text of which you can replace outright, or which you can use to find a location relative to it where to insert the text, such as the beginning or end or after the 3rd word or whatever.

    0 讨论(0)
  • 2021-01-28 15:30

    You could also use some bookmarks:

    You can choose where you put your bookmark and then write on it like this ThisDocument.Bookmarks("NAME_OF_THE_BOOKMARK").Range.Text = THE_EXCEL_DATA

    To place a bookmark you have to click on the selected area and then go on Insert->Bookmarks and then name it.

    0 讨论(0)
提交回复
热议问题