Solved

vbscript word automation

Posted on 2004-08-10
3
1,515 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 125 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

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

Introduction I needed to skip over some file processing within a For...Next loop in some old production code and wished that VB (classic) had a statement that would drop down to the end of the current iteration, bypassing the statements that were c…
Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
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…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

803 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