0
votes

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.

DOCX showing errors same DOCX with special chars showing

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:

Normal Text with some manually bolded words

1
I don't understand why you're putting the text in the Clipboard, then pasting it into the document - it makes no sense. Why not write the text directly to the document? Most likely, the odd characters are coming from this process (from the DataObject), possibly from round-tripping the Chr(13). - Cindy Meister
No doubt you are correct when you say: "write the text directly to the document?". I'm not a VBA expert. Can you suggest the relevant code? Thanks. - s2016
Normally, StackOverflow isn't for "tutorials", but learning to insert text correctly into Word is very important. You'd have gotten a good start, though, if you'd used the macro recorder while inserting and formatting content... - Cindy Meister

1 Answers

0
votes

It's usually best to use the Range object, something like as follows. The macro recorder uses the Selection object, which is less reliable and less efficient.

I've used Word's Builtin styles constants, since you only use built-in styles in your question. You can find a list in the VB Editor object browser (F2) if you type wdStyle in the search box.

Sub WriteAndFormatTextToWord()
  Dim doc As word.Document
  Dim rng As word.Range
  Dim newText As String

  newText = "Text to insert."

  Set doc = ActiveDocument
  Set rng = doc.content
  InsertTextAndFormat newText & vbCr, rng, wdStyleHeading1
  newText = "More text"
  InsertTextAndFormat newText & vbCr, rng, wdStyleNormal
End Sub

Sub InsertTextAndFormat(t As String, rng As word.Range, _
                        styl As word.WdBuiltinStyle)
  rng.Text = t
  rng.Style = styl
  rng.Collapse wdCollapseEnd
End Sub