Solved

vbscript word automation

Posted on 2004-08-10
3
1,532 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Enroll in June's Course of the Month

June’s Course of the Month is now available! Experts Exchange’s Premium Members, Team Accounts, and Qualified Experts have access to a complimentary course each month as part of their membership—an extra way to sharpen your skills and increase training.

Question has a verified solution.

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

I’ve seen a number of people looking for examples of how to access web services from VB6.  I’ve been using a test harness I built in VB6 (using many resources I found online) that I use for small projects to work out how to communicate with web serv…
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
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…
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…

728 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