Hi, Paul;
I believe I got your help in this April or May in a topic of "how to save contents in a textbox into Word file via VBA". This time it is the same topic but in VB 2013 express environment. I am pretty new for VB 2013. I realized that I need to change something in the code you once sent to me. I searched online and worked out the following code. However it opened a Word file but just stopped there. It said .range is not defined ....So Could you help me to make the code run?
In the texts sent to Word file from the textbox, there are some lines starting with "#@#" or "###" which means the line should be in bold font.
Thank you so much.
Jason
Code:
Sub SaveOutputAsWordDocument()
Dim oWord As Word.Application
Dim oDoc As Word.Document
oWord = CreateObject("Word.Application")
oWord.Visible = True
oDoc = oWord.Documents.Add
Dim wdNN As Integer = 0
Dim tempText As String
Dim TTN As Integer
Dim tempPath As String
With oWord
.Range.Text = txtDocumentOutput.Text 'send texts to the Word file for editing
With .Styles("Normal")
With .ParagraphFormat
.SpaceBefore = 0
.SpaceAfter = 0
'.LineSpacingRule = wdLineSpaceSingle (may not work, with a error underline)
End With
With .Font
.Name = "Cambria"
.Size = 11
End With
End With
End With
wdNN = oDoc.Paragraphs.Count
For TTN = 1 To wdNN
tempText = oDoc.Paragraphs(TTN).Range.Text
If Mid(tempText, 1, 3) = "#@#" Or Mid(tempText, 1, 3) = "###" Then
tempText = Mid(tempText, 4, Len(tempText) - 3)
oDoc.Paragraphs(TTN).Range.Text = tempText
oDoc.Paragraphs(TTN).Range.FormattedText.Font.Bold = True
End If
Next
tempPath = SaveFileDialog1.FileName
oDoc.SaveAs2 (tempPath)
MsgBox ("The Scope file is saved in the location you assigned.")
oDoc.Close()
oWord.Quit()
End Sub