jessebh2003
Board Regular
- Joined
- Feb 28, 2020
- Messages
- 71
- Office Version
- 365
- Platform
- Windows
My Excel macro opens a Word template and replaces a specified merge field with the text from an identified cell. This all works great except that when the merge field is replaced, either the preceding or following space is being removed too.
For instance...
"B44" = 4.69
"C44" = 0.09
In the Word document, the text is <<OverallMeanScore>> ± <<OverallStDev>>. But when running the macro, the values end up being 4.69 ±0.09. The preceding space for "0.09" is removed.
Elsewhere in the Word document, the merge codes are within tables and are replaced the same as above.
"B5" = 4.65
"C5" = 0.66
In the Word document, the text is <<RelevantMean>> ± <<RelevantStDev>>. The values end up being 4.65± 0.66. The following space for "4.65" is removed.
Both of these behaviors are occurring throughout the document both in paragraphs and in tables. How can I correct this from happening? Thanks.
For instance...
VBA Code:
Dim wrdApp As Word.Application
Dim wrdDoc As Word.Document
Set wrdApp = CreateObject("Word.Application")
wrdApp.Visible = True
Set wrdDoc = wrdApp.Documents.Open("SomeTemplateName.dotx")
Dim Fld As Word.Field
With wrdDoc
'Populates Level 1 Text
Set Fld = GetField(wrdDoc, "OverallMeanScore")
If Not Fld Is Nothing Then
Fld.Select
.Application.Selection.Delete
.Application.Selection.Text = Range("B44").Text
.Application.Selection.Collapse wdCollapseEnd
End If
Set Fld = GetField(wrdDoc, "OverallStDev")
If Not Fld Is Nothing Then
Fld.Select
.Application.Selection.Delete
.Application.Selection.Text = Range("C44").Text
.Application.Selection.Collapse wdCollapseEnd
End If
"B44" = 4.69
"C44" = 0.09
In the Word document, the text is <<OverallMeanScore>> ± <<OverallStDev>>. But when running the macro, the values end up being 4.69 ±0.09. The preceding space for "0.09" is removed.
Elsewhere in the Word document, the merge codes are within tables and are replaced the same as above.
VBA Code:
Set Fld = GetField(wrdDoc, "RelevantMean")
If Not Fld Is Nothing Then
Fld.Select
.Application.Selection.Delete
.Application.Selection.Text = Range("B5").Text
.Application.Selection.Collapse wdCollapseEnd
End If
Set Fld = GetField(wrdDoc, "RelevantStDev")
If Not Fld Is Nothing Then
Fld.Select
.Application.Selection.Delete
.Application.Selection.Text = Range("C5").Text
.Application.Selection.Collapse wdCollapseEnd
End If
"B5" = 4.65
"C5" = 0.66
In the Word document, the text is <<RelevantMean>> ± <<RelevantStDev>>. The values end up being 4.65± 0.66. The following space for "4.65" is removed.
Both of these behaviors are occurring throughout the document both in paragraphs and in tables. How can I correct this from happening? Thanks.