Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 12-09-2014, 02:55 PM
saltlakebuffalo saltlakebuffalo is offline Windows 7 64bit Office 2010 32bit
Novice
how to save contents in textbox in VB2013 form into a Word document?
 
Join Date: Feb 2014
Posts: 11
saltlakebuffalo is on a distinguished road
Default 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
Reply With Quote
  #2  
Old 12-09-2014, 03:22 PM
macropod's Avatar
macropod macropod is offline how to save contents in textbox in VB2013 form into a Word document? Windows 7 64bit how to save contents in textbox in VB2013 form into a Word document? Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,962
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

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]
Reply With Quote
  #3  
Old 12-09-2014, 07:24 PM
saltlakebuffalo saltlakebuffalo is offline Windows 7 64bit Office 2010 32bit
Novice
how to save contents in textbox in VB2013 form into a Word document?
 
Join Date: Feb 2014
Posts: 11
saltlakebuffalo is on a distinguished road
Default

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
Reply With Quote
  #4  
Old 12-09-2014, 09:19 PM
macropod's Avatar
macropod macropod is offline how to save contents in textbox in VB2013 form into a Word document? Windows 7 64bit how to save contents in textbox in VB2013 form into a Word document? Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,962
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

AFAIK under VB express, you need to replace 'With' constructs, such as:
Code:
With oWord
  .Range.Text = txtDocumentOutput.Text
with code like:
Code:
oWord.Range.Text = txtDocumentOutput.Text
See, for example: http://support.microsoft.com/kb/313193

PS: When posting code, please use the code tags, indicated by the # button on the posting menu.
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #5  
Old 12-10-2014, 08:30 AM
saltlakebuffalo saltlakebuffalo is offline Windows 7 64bit Office 2010 32bit
Novice
how to save contents in textbox in VB2013 form into a Word document?
 
Join Date: Feb 2014
Posts: 11
saltlakebuffalo is on a distinguished road
Default

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
Reply With Quote
  #6  
Old 12-10-2014, 03:07 PM
macropod's Avatar
macropod macropod is offline how to save contents in textbox in VB2013 form into a Word document? Windows 7 64bit how to save contents in textbox in VB2013 form into a Word document? Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,962
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

Looking at you code again, your 'With oWord' should have been 'With oDoc'.
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #7  
Old 12-10-2014, 06:12 PM
saltlakebuffalo saltlakebuffalo is offline Windows 7 64bit Office 2010 32bit
Novice
how to save contents in textbox in VB2013 form into a Word document?
 
Join Date: Feb 2014
Posts: 11
saltlakebuffalo is on a distinguished road
Default

Hi, Paul;
Yes, I did and it runs. Thank you so much.
Jason
Reply With Quote
Reply



Similar Threads
Thread Thread Starter Forum Replies Last Post
how to save contents in textbox in VB2013 form into a Word document? 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

Other Forums: Access Forums

All times are GMT -7. The time now is 10:24 PM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2024 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft