Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

vbscript word automation

Posted on 2004-08-10
3
Medium Priority
?
1,574 Views
Last Modified: 2010-07-27
How would I code a word automation vbscript that produces a word doc with headers and tables?
0
Comment
Question by:arichexe
  • 2
3 Comments
 
LVL 14

Expert Comment

by:Farzad Akbarnejad
ID: 11770124
Hi,

You can start to saving MAcro in Word then do your tasks then view or edit the VBA generated code.

Sub Macro1()
'
' Macro1 Macro
' Macro recorded 8/11/2004 by Farzad Akbarnejad
'
    If ActiveWindow.View.SplitSpecial <> wdPaneNone Then
        ActiveWindow.Panes(2).Close
    End If
    If ActiveWindow.ActivePane.View.Type = wdNormalView Or ActiveWindow. _
        ActivePane.View.Type = wdOutlineView Then
        ActiveWindow.ActivePane.View.Type = wdPrintView
    End If
    ActiveWindow.ActivePane.View.SeekView = wdSeekCurrentPageHeader
    Selection.TypeText Text:="This is a text typed in Header"
    ActiveWindow.ActivePane.View.SeekView = wdSeekMainDocument
    Selection.TypeParagraph
    Selection.TypeParagraph
    ActiveDocument.Tables.Add Range:=Selection.Range, NumRows:=7, NumColumns:= _
        5, DefaultTableBehavior:=wdWord9TableBehavior, AutoFitBehavior:= _
        wdAutoFitFixed
    With Selection.Tables(1)
        If .Style <> "Table Grid" Then
            .Style = "Table Grid"
        End If
        .ApplyStyleHeadingRows = True
        .ApplyStyleLastRow = True
        .ApplyStyleFirstColumn = True
        .ApplyStyleLastColumn = True
    End With
    Selection.TypeText Text:="Row 1  Col  1"
    Selection.MoveRight Unit:=wdCell
    Selection.TypeText Text:="Row 1  Col 2"
    Selection.MoveRight Unit:=wdCell
    Selection.MoveRight Unit:=wdCell
    Selection.MoveRight Unit:=wdCell
    Selection.MoveRight Unit:=wdCell
    Selection.MoveRight Unit:=wdCell
    Selection.MoveRight Unit:=wdCell
    Selection.MoveRight Unit:=wdCell
    Selection.MoveRight Unit:=wdCell
    Selection.MoveRight Unit:=wdCell
    Selection.MoveRight Unit:=wdCell
    Selection.MoveRight Unit:=wdCell
    Selection.TypeText Text:="Row 3  Col 3"
    Selection.MoveRight Unit:=wdCell
    Selection.MoveRight Unit:=wdCell
    Selection.MoveRight Unit:=wdCell
    Selection.MoveRight Unit:=wdCell
    Selection.MoveRight Unit:=wdCell
    Selection.MoveRight Unit:=wdCell
    Selection.MoveRight Unit:=wdCell
    Selection.MoveRight Unit:=wdCell
    Selection.MoveRight Unit:=wdCell
    Selection.MoveRight Unit:=wdCell
    Selection.MoveRight Unit:=wdCell
    Selection.TypeText Text:="Row 5 Col 4"
End Sub


-FA
0
 

Author Comment

by:arichexe
ID: 11776438
How would I incorporate your header and table logic within the vbscript below, without using a macro?

Set Oword = CreateObject("word.application")
Oword.Visible = true
Set WRDDOC = Oword.Documents.Add
WRDDOC.Select

' insert heading here

with Oword
.Selection.MoveDown
.Selection.InsertAfter "Hello World!"
.Selection.ParagraphFormat.Alignment = wdAlignParagraphCenter
.Selection.Font.Name = "VERDANA"
.Selection.Font.Size = 9
.Selection.Font.Bold = True
.Selection.Font.Underline = False
.Selection.InsertParagraphAfter
.Selection.InsertParagraphAfter
.Selection.MoveDown

' insert table here

end with
0
 
LVL 14

Accepted Solution

by:
Farzad Akbarnejad earned 500 total points
ID: 11783964
For adding header write the following script instead of  

 'insert heading here:

with Oword

   If .ActiveWindow.View.SplitSpecial <> wdPaneNone Then
        .ActiveWindow.Panes(2).Close
    End If
    If .ActiveWindow.ActivePane.View.Type = wdNormalView Or .ActiveWindow. _
        .ActivePane.View.Type = wdOutlineView Then
        .ActiveWindow.ActivePane.View.Type = wdPrintView
    End If
    .ActiveWindow.ActivePane.View.SeekView = wdSeekCurrentPageHeader
    .Selection.TypeText Text:="This is a text typed in Header"
    .ActiveWindow.ActivePane.View.SeekView = wdSeekMainDocument
    .Selection.TypeParagraph


end with


for create table insert the folloing script instead of  
' insert table here:


    .ActiveDocument.Tables.Add Range:=Selection.Range, NumRows:=7, NumColumns:= _
        5, DefaultTableBehavior:=wdWord9TableBehavior, AutoFitBehavior:= _
        wdAutoFitFixed
    With .Selection.Tables(1)
        If .Style <> "Table Grid" Then
            .Style = "Table Grid"
        End If
        .ApplyStyleHeadingRows = True
        .ApplyStyleLastRow = True
        .ApplyStyleFirstColumn = True
        .ApplyStyleLastColumn = True
    End With
    .Selection.TypeText Text:="Row 1  Col  1"
    .Selection.MoveRight Unit:=wdCell
    .Selection.TypeText Text:="Row 1  Col 2"
    .Selection.MoveRight Unit:=wdCell
    .Selection.MoveRight Unit:=wdCell
    .Selection.MoveRight Unit:=wdCell
    .Selection.MoveRight Unit:=wdCell
    .Selection.MoveRight Unit:=wdCell
    .Selection.MoveRight Unit:=wdCell
    .Selection.MoveRight Unit:=wdCell
    .Selection.MoveRight Unit:=wdCell
    .Selection.MoveRight Unit:=wdCell
    .Selection.MoveRight Unit:=wdCell
    .Selection.MoveRight Unit:=wdCell
    .Selection.TypeText Text:="Row 3  Col 3"
    .Selection.MoveRight Unit:=wdCell
    .Selection.MoveRight Unit:=wdCell
    .Selection.MoveRight Unit:=wdCell
    .Selection.MoveRight Unit:=wdCell
    .Selection.MoveRight Unit:=wdCell
    .Selection.MoveRight Unit:=wdCell
    .Selection.MoveRight Unit:=wdCell
    .Selection.MoveRight Unit:=wdCell
    .Selection.MoveRight Unit:=wdCell
    .Selection.MoveRight Unit:=wdCell
    .Selection.MoveRight Unit:=wdCell
    .Selection.TypeText Text:="Row 5 Col 4"


Take care of using constant such as wdOutlineView. If you add reference of Microsoft Word in your IDE and/or use VB IDE you have these constant in your hand. If not you must replace them with numbers you can find in your documents.

Thanks
-FA
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
Suggested Courses

876 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question