VB6: How to WRITE output to Word Document
VB6: How to WRITE output to Word Document
(OP)
Hi All,
What I would like to know is how I can create a Word Document from within VB6 such that the output of an Application can be written to Word. (e.g. How to open a Word Document , write the output from VB6 in a specified format including Tables etc). As you can guess I'm reasonably new to VB6 so any help will be welcome or even a website pointer would be nice.
Many Thanks in advance
What I would like to know is how I can create a Word Document from within VB6 such that the output of an Application can be written to Word. (e.g. How to open a Word Document , write the output from VB6 in a specified format including Tables etc). As you can guess I'm reasonably new to VB6 so any help will be welcome or even a website pointer would be nice.
Many Thanks in advance





RE: VB6: How to WRITE output to Word Document
Within your application, put a command button named "Word" ( or anything else good for you).
Look in References and select: Microsoft Word 8.0 or 9.0 Object library. Depending on what you have.
You must put a Common Dialog Button. Look at Components and select: Microsotf Commom Dialog Control 6.0 (SP3) in
c:\windows\system32\COMDLG32.OCX
Name the common dialog: cdlDSB123
Double click the command and type the following ( You don't have to do that because you can copy and paste ):
Private Sub cmdWord_Click()
Set objWordApp = GetObject("", "Word.Application.9")
If objWordApp = "Nothing" Then ' True if not running
Set objWordApp = CreateObject("Word.Application.9")
End If
' Add a document to the Collection
objWordApp.Documents.Add
' Title
' objWordApp.Documents(1).Content.Font.Arial
objWordApp.Documents(1).Content.Font.Size = 14
objWordApp.Documents(1).Content.Font.Bold = True
objWordApp.Documents(1).Content.InsertAfter Text:="DSB123"
' The Date and Time
' objWordApp.Documents(1).Content.Font.Arial = True
objWordApp.Documents(1).Content.Font.Size = 12
objWordApp.Documents(1).Content.Font.Bold = False
objWordApp.Documents(1).Range.InsertAfter Text:=Format(Now, "dddd, mmmm dd, yyyy hh:mm:ss") & vbCrLf
' The body of the Document is next
objWordApp.Documents(1).Range.InsertAfter Text:="Properties of the Channel" & vbCrLf
objWordApp.Documents(1).Range.InsertAfter Text:="b1 = " & Val(txtb1.Text) & " in" & " " & "b2 = " & Val(txtb2.Text) & " in" & " " & "b3 = " & Val(txtb3.Text) & " in" & vbCrLf
objWordApp.Documents(1).Range.InsertAfter Text:="d1 = " & Val(txtd1.Text) & " in" & " " & "d3 = " & Val(txtd3.Text) & " in" & " " + "h = " & Val(txth.Text) & " in" & " " & vbCrLf
objWordApp.Documents(1).Range.InsertAfter Text:="Weight = " & Val(txtGi.Text) & " lb/ft" & vbCrLf
objWordApp.Documents(1).Range.InsertAfter Text:="Weight = " & Val(txtGm.Text) & " kg/m" & vbCrLf
objWordApp.Documents(1).Range.InsertAfter Text:="Area = " & Val(txta.Text) & " [ in² ]" & vbCrLf
objWordApp.Documents(1).Range.InsertAfter Text:="Area Sup Flange = " & Val(txtAf.Text) & " [ in² ]" & vbCrLf
objWordApp.Documents(1).Range.InsertAfter Text:="Area Inf Flange = " & Val(txtAfi.Text) & " [ in² ]" & vbCrLf
objWordApp.Documents(1).Range.InsertAfter Text:="Center = " & Val(txtC.Text) & " in" & vbCrLf
objWordApp.Documents(1).Range.InsertAfter Text:="Ix = " & Val(txtIx.Text) & " [ in4 ]" & vbCrLf
objWordApp.Documents(1).Range.InsertAfter Text:="Sx Compression = " & Val(txtSxC.Text) & " [ in3 ]" & vbCrLf
objWordApp.Documents(1).Range.InsertAfter Text:="Sx Tension = " & Val(txtSxT.Text) & " [ in3 ]" & vbCrLf
objWordApp.Documents(1).Range.InsertAfter Text:="rx = " & Val(txtrx.Text) & " in"
objWordApp.Documents(1).Range.InsertAfter Text:="Iy = " & Val(txtIy.Text) & " in" & vbCrLf
objWordApp.Documents(1).Range.InsertAfter Text:="Sy Compression = " & Val(txtSyC.Text) & " [ in3 ]" & vbCrLf
objWordApp.Documents(1).Range.InsertAfter Text:="Sy Tension = " & Val(txtSyT.Text) & " [ in3 ]" & vbCrLf
objWordApp.Documents(1).Range.InsertAfter Text:="ry = " & Val(txtry.Text) & " in"
objWordApp.Documents(1).Range.InsertAfter Text:="eo = " & Val(txteo.Text) & " in" & vbCrLf
objWordApp.Documents(1).Range.InsertAfter Text:="J = " & Val(txtJ.Text) & " [ in4 ]" & vbCrLf
objWordApp.Documents(1).Range.InsertAfter Text:="Cw = " & Val(txtCw.Text) & vbCrLf
objWordApp.Documents(1).Range.InsertAfter Text:="rT = " & Val(txtrT.Text) & " in"
objWordApp.Documents(1).Range.InsertAfter Text:="Zx = " & Val(txtZx.Text) & " [ in3 ]" & vbCrLf
objWordApp.Documents(1).Range.InsertAfter Text:="Zy = " & Val(txtZy.Text) & " [ in3 ]"
objWordApp.Documents(1).Range.InsertAfter Text:="Ah = 5·tf·bf/3 = " & Val(txtAh.Text) & vbCrLf
objWordApp.Documents(1).Range.InsertAfter Text:="Av = tw·d = " & Val(txtAv.Text)
objWordApp.Documents(1).Range.InsertAfter Text:="AISC Table B5.1" & vbCrLf
objWordApp.Documents(1).Range.InsertAfter Text:="b/2tf = " & Val(txtbt.Text) & vbCrLf
objWordApp.Documents(1).Range.InsertAfter Text:="b/2tf <= 65/Fy^0.5 = " & Val(txtbtA36.Text) & " for ASTM A36" & vbCrLf
objWordApp.Documents(1).Range.InsertAfter Text:="b/2tf <= 65/Fy^0.5 = " & Val(txtbtA50.Text) & " for ASTM A50" & vbCrLf
objWordApp.Documents(1).Range.InsertAfter Text:="d/tw = " & Val(txtdtw.Text) & vbCrLf
objWordApp.Documents(1).Range.InsertAfter Text:="d/tw <= 640/Fy^0.5 = " & Val(txtdtwA36.Text) & " for ASTM A36" & vbCrLf
objWordApp.Documents(1).Range.InsertAfter Text:="d/tw <= 640/Fy^0.5 = " & Val(txtdtwA50.Text) & " for ASTM A50" & vbCrLf
objWordApp.Documents(1).Range.InsertAfter Text:="h/tw = " & Val(txthtw.Text) & vbCrLf
objWordApp.Documents(1).Range.InsertAfter Text:="h/tw <= 760/(0.6·Fy) = " & Val(txthtwA36.Text) & " for ASTM A36" & vbCrLf
objWordApp.Documents(1).Range.InsertAfter Text:="h/tw <= 760/(0.6·Fy) = " & Val(txthtwA50.Text) & " for ASTM A50" & vbCrLf
objWordApp.Documents(1).Range.InsertAfter Text:="d/Af = " & Val(txtdAf.Text) & vbCrLf
objWordApp.Documents(1).Range.InsertAfter Text:="(E·Cw/(G·J))^0.5 = " & Val(txtECw.Text) & vbCrLf
objWordApp.Documents(1).Range.InsertAfter Text:="F''y = " & Val(txtF3y.Text) & vbCrLf
' Save the Document
' Stablishing the Common Dialog Filter
StrFilter = "All Microsoft Word Files (*.doc)|*.doc|Text Files (*.txt)|*.txt|All Files (*.*)|*.*|"
cdlDSB123.Filter = StrFilter
' Opening the Common Dialog for Save
cdlDSB123.ShowSave
' Be sure about the filename is not a blank or emoty name
If cdlDSB123.FileName <> "" Then
' if the name is not empty, open the file
StrFileName = cdlDSB123.FileName
' Prompt to OverWrite the File
cdlDSB123.Flags = cdlOFNOwerwritePrompt
' Open a File for Output
objWordApp.SaveAs StrFileName
End If
' Close the Word Document
objWordApp.Documents(1).Close
objWordApp.Quit
End Sub
Last thing: in InsertAfter Text:=
the part Text:= can be eliminated and the program is working good too.
If you need more commands in Word, you must have some information about Word VBA.
if you need more help, please email me at:
atklinger@hotmail.com
Alberto
RE: VB6: How to WRITE output to Word Document
By a mistake I last part in the file must be:
( The prompt OverWrite MUST BE Before the ShowSave)
' Save the Document
' Stablishing the Common Dialog Filter
StrFilter = "All Microsoft Word Files (*.doc)|*.doc|Text Files (*.txt)|*.txt|All Files (*.*)|*.*|"
cdlDSB123.Filter = StrFilter
' Prompt to OverWrite the File
cdlDSB123.Flags = cdlOFNOwerwritePrompt
' Opening the Common Dialog for Save
cdlDSB123.ShowSave
' Be sure about the filename is not a blank or emoty name
If cdlDSB123.FileName <> "" Then
' if the name is not empty, open the file
StrFileName = cdlDSB123.FileName
' Open a File for Output
objWordApp.SaveAs StrFileName
End If
' Close the Word Document
objWordApp.Documents(1).Close
objWordApp.Quit
Alberto