简体   繁体   中英

How to add text to place holder from word vba

Hi I am trying to copy Cells from excel sheet and populating word document. I want to copy text from excel cells to a specific place in word document. I am able to store the text from excel cells in a string, but not sure how to make it store in a word placeholder or bookmark that I have mentioned in my word doc. This is what I have so far:

Set ws = xlBook.Worksheets("DIP Main")
    Tmp = ws.Cells(25, "C").Value
    .Text = Tmp
    .Execute Replace:=("Placeholder1")
   ' [Placeholder1] = Tmp.Text
   ' MyDOc.Fields("Placeholder1") = Tmp.Valu

e

Tmp is storing value from excel, but I am not able to replace and print it in the placeholder on my word document, or if there is any other way of printing Tmp string in a specific location of word doc that would work too. Also im not declaring any placeholder in my code, I am not sure if i am supposed to. I have created place holder in the word document itself called "Placeholder1". I am using VBA word to code it.

For Full code please refer to this: How to copy excel range from a sheet and place it into a specific place it into a specific place in word using word vba

这是Word中在书签中插入文本的代码

    ActiveDocument.Bookmarks("Placeholder1").Range = "abc123"

This ought to help you on your way.

Sub ExcelToWord()

    ' define all Excel variables you are going to use:
    Dim Wb As Workbook
    Dim Ws As Worksheet
    Dim Cell As Range

    ' define all Word variables you are going to use:
      ' since you are running the code from Excel
      ' you must specify "Word" for each data type
    Dim WdApp As Word.Application
    Dim Doc As Word.Document

    ' define variables which you can use in either application:
      ' (if you aren't sure, define separate ones)
    Dim Tmp As String
    Dim R As Long
    Dim i As Integer

    ' replace this name with the name & path of your own Word document
    Tmp = "E:\PVT Archive\Class 1\1-2017 (Jan 2019)\STO 170317.docm"
    If Dir(Tmp) = "" Then                           ' Check if the file exists
        MsgBox "The file doesn't exist.", _
               vbInformation, "Invalid file or path name"
        Exit Sub
    End If
    Set WdApp = CreateObject("Word.Application")    ' open Word
    WdApp.Visible = True
    Set Doc = WdApp.Documents.Open(Tmp)             ' open the document

    If Not Doc.Bookmarks.Exists("Amark") Then
        MsgBox "The bookmark 'Amark' doesn't exist.", _
               vbInformation, "Can't find bookmark"
        Exit Sub
    End If

    Set Wb = ActiveWorkbook
    Set Ws = Wb.Worksheets("DIP Main")
    Tmp = Ws.Cells(25, "C").Value

    Doc.Bookmarks("Amark").Range.Text = Tmp
End Sub

Note that the bookmark will be replaced with the next. It doesn't exist thereafter. If you wish to re-use it you must use code to set it again.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM