How do I get rid of the weird "box" characters that have appeared prefixed to the Word VBA-generated "Heading 2" style text (likewise the weird boxes added both before and after the "Normal" style text)?
Screenshot of the Word DOCX is attached. It shows 2 views of same DOCX. One showing special characters and one without.
Public theStyle As String
Public OldContent As String
Public Normal As Style
Public head1 As Style
Public head2 As Style
Sub Build_DOCX()
OldContent = "Welcome to XYZ!" & Chr(13)
OldContent = OldContent
theStyle = "head1"
KM_Insert_Styled_Text_Use_Vars
OldContent = "Overview" & Chr(13)
theStyle = "head2"
KM_Insert_Styled_Text_Use_Vars
OldContent = "When you compile your application" & Chr(13)
theStyle = "normal"
KM_Insert_Styled_Text_Use_Vars
End Sub
Sub KM_Insert_Styled_Text_Use_Vars()
Debug.Print "OldContent = "; OldContent
Debug.Print "theStyle = "; theStyle
If theStyle = "normal" Then
Clipboard_Paste_With_Style_Normal
ElseIf theStyle = "head1" Then
Clipboard_Paste_With_Style_Heading1
ElseIf theStyle = "head2" Then
Clipboard_Paste_With_Style_Heading2
End If
End Sub
Sub Clipboard_Paste_With_Style_Normal()
Dim MyRange As Object
Dim clipboard As MSForms.DataObject
Set clipboard = New MSForms.DataObject
clipboard.SetText OldContent
clipboard.PutInClipboard
Set MyRange = Selection.Range
Selection.Paste
MyRange.Collapse Direction:=wdCollapseStart
MyRange.Style = wdStyleNormal
End Sub
Sub Clipboard_Paste_With_Style_Heading1()
Dim MyRange As Object
Dim clipboard As MSForms.DataObject
Set clipboard = New MSForms.DataObject
clipboard.SetText OldContent
clipboard.PutInClipboard
Set MyRange = Selection.Range
Selection.Paste
MyRange.Collapse Direction:=wdCollapseStart
MyRange.Style = wdStyleHeading1
End Sub
Sub Clipboard_Paste_With_Style_Heading2()
Dim MyRange As Object
Dim clipboard As MSForms.DataObject
Set clipboard = New MSForms.DataObject
clipboard.SetText OldContent
clipboard.PutInClipboard
Set MyRange = Selection.Range
Selection.Paste
MyRange.Collapse Direction:=wdCollapseStart
MyRange.Style = wdStyleHeading2
End Sub
Example of 1 Normal Text ("More Text" in Cindy's code) is in the image:


