I can create a word document using VB code but how do I actually input text, tables etc.
Any pointers or sample code would be gratefully received.
Thanks.
Printable View
I can create a word document using VB code but how do I actually input text, tables etc.
Any pointers or sample code would be gratefully received.
Thanks.
Add the Microsoft Word Object library to your VB project references. You can do just about anything you want in Word using VB. This should at least get you started. The easiest way to get sample code is to record a macro in Word and look at the code produced. Then, just take what you need.
Hope this helps!Code:Sub WriteToNewWordDoc
Set wdApp = New Word.Application
wdApp.Visible = True
wdApp.WindowState = wdWindowStateMaximize
wdApp.Documents.Add
With wdApp.ActiveDocument.PageSetup
.Orientation = wdOrientPortrait
.TopMargin = InchesToPoints(1)
.BottomMargin = InchesToPoints(1)
.LeftMargin = InchesToPoints(1)
.RightMargin = InchesToPoints(1)
.PageWidth = InchesToPoints(8.5)
.PageHeight = InchesToPoints(11)
End With
'--------------------
'Document Title
'--------------------
With wdApp.Selection
.ParagraphFormat.Alignment = wdAlignParagraphCenter
.Font.Size = 24
.TypeText Text:="Good spot for a document title"
.TypeParagraph
.TypeText Text:="Maybe a SubTitle here."
.TypeParagraph
.TypeParagraph
End With