In Excel I have a userform that has a ListBox being populated from an Excel table using RowSource=myTable.
Now I want to select one line, press a button, and export eve
Word has multiple possibilities for the developer to write data that will be displayed in the document. Which to use depends on the individual requirement of the developer.
Bookmark
object provides a Range
property so that the inserted content can be formatted or otherwise manipulated. A bookmark can be referenced elsewhere in the document as a cross-reference or to repeat the information more than one time.VBA code to write to a bookmark
ActiveDocument.Bookmarks("BookmarkName").Range.Text = "Data as string"
Writing to a bookmark with content deletes the bookmark. If the bookmark should be retained
Dim sBookmarkName as String
Dim rngBookmark as Word.Range
Dim doc as Word.Document
Dim bkm as Word.Bookmark
sBookmarkName = "BookmarkName"
Set doc = ActiveDocument
If doc.Bookmarks.Exists(sBookmarkName) Then
Set bkm = doc.Bookmarks(sBookmarkName)
Set rng = bkm.Range
rng.Text = "Data as string"
doc.Bookmarks.Add(sBookmarkName, rngBookmark)
End If
Document.Variable
or CustomDocumentProperty
objects and the content reflected on the document surface by use of DocVariable
or DocProperty
fields. This has the advantage of the content travelling with the document, whether the user edits what's displayed on the surface, or not. Formatting the content is problematic.To write to document Variables and custom document properties:
ActiveDocument.Variables("VariableName") = "Data as string"
'See the language reference for more about values for Type
ActiveDocument.CustomDocumentProperties.Add Name:="PropertyName", _
LinkToContent:=False, _
Type:=msoPropertyTypeString, _
Value:="Data as string"
Since content controls can have the same title or tag, the object model returns an array for these properties. Word also assigns each content control a unique ID (GUID) - if the GUID is known, the content control can be addressed directly.
Dim doc as Word.Document
Dim sCC_ID as String
Dim sCC_Title as String
Dim sCC_Tag as String
Set doc = ActiveDocument
'ID value is passed as a string
doc.ContentControls("1691881769").Range.Text = "Data as String"
'Get the first content control with the given title
sCC_Title = "Content Control Title"
doc.ContentControls.SelectContentControlByTitle(sCC_Title).Item(1).Range.Text = "Data as String"
'Loop all content controls having the same tag
Dim aCC as Word.ContentControls
Dim cc as Word.ContentControl
sCC_Tag = "Content Control Tag"
Set aCC = doc.SelectContentControlsByTag("Content Control Tag")
For Each cc In aCC
cc.Range.Text = "Data as string"
Next
The following snippet demonstrates how to loop all fields in document not linked to a mail merge data source, search for the merge field and replace it with data
Dim fld As Word.Field
Dim fldRange As Word.Range
Dim sFldCode As String, sDataField As String, sDataContent
sDataField = "MergefieldName"
sDataContent = "Data as string"
For Each fld In ActiveDocument.Fields
sFldCode = fld.code
Select Case InStr(sFldCode, sDataField)
Case Is > 0
Set fldRange = fld.result
fld.Delete
fldRange = sDataContent
End Select
Next
Simple search code:
Dim wdDoc As Word.Document
Dim sPlaceholder as String, sData as String
sData = "Data as string"
sPlaceholder = "DataFieldName"
Set wdDoc = ActiveDocument
With wdDoc.content.Find
.ClearFormatting
.Text = "<<" & sPlaceholder & ">>"
With .Replacement
.ClearFormatting
.Text = sData
End With
.Execute Replace:=wdReplaceAll
End With