Solved

vbscript word automation

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
z = x + y – 1 6 67
Validating VB6 Function 19 56
Prevent checkbox click event occur while editing it in vb6 8 32
Recommendation vb6 to vb.net or others 14 109
Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
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…

911 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now