#1
|
|||
|
|||
how to save contents in textbox in VB2013 form into a Word document?
I have a textbox on a VB 2013 Form, and I want to send the texts in this textbox into a new Word document and save this Word document from the VB interface. What is the code for this process? Thank you.
Jason |
#2
|
||||
|
||||
That's pretty basic stuff. What have you tried? Have you done a search and looked at the (probably) thousands of examples of this on the web?
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#3
|
|||
|
|||
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 Last edited by macropod; 12-09-2014 at 09:09 PM. Reason: Added code tags & formatting |
#4
|
||||
|
||||
AFAIK under VB express, you need to replace 'With' constructs, such as:
Code:
With oWord .Range.Text = txtDocumentOutput.Text Code:
oWord.Range.Text = txtDocumentOutput.Text PS: When posting code, please use the code tags, indicated by the # button on the posting menu.
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#5
|
|||
|
|||
Hi, Paul;
I tried by your suggestion but it still doesn't work. I got a pop-up saying Public member 'Range' on type 'ApplicaitonClass' not found. So what should I do to improve it? Thank you again. Jason |
#6
|
||||
|
||||
Looking at you code again, your 'With oWord' should have been 'With oDoc'.
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#7
|
|||
|
|||
Hi, Paul;
Yes, I did and it runs. Thank you so much. Jason |
|
Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
Outlook 2007 Code For Matching Textbox to a Combobox in a Different Form | lms | Outlook | 4 | 07-03-2013 08:34 AM |
Word ask to save template whenever i save a derived document | jorbjo | Word | 3 | 10-04-2012 10:52 AM |
Saving document using Form Contents | jillapass | Word | 1 | 03-28-2012 12:03 AM |
Is there a way to toggle the contents that are displayed in a Word document? | sclind | Word | 1 | 02-24-2012 04:56 PM |
PowerPoint 2007 Textbox Lock / Form Issues | LTechie12 | PowerPoint | 0 | 01-08-2012 02:08 PM |