Follow along with the video below to see how to install our site as a web app on your home screen.
Note: This feature may not be available in some browsers.
Sub ExpWord()
'
' ExpWord Macro
' Macro recorded 8/8/2005 by Steven
'
With ActiveDocument.Styles(wdStyleNormal).Font
If .NameFarEast = .NameAscii Then
.NameAscii = ""
End If
.NameFarEast = ""
End With
{ step 1}
With ActiveDocument.PageSetup
.LineNumbering.Active = False
.Orientation = wdOrientLandscape
.TopMargin = InchesToPoints(1.25)
.BottomMargin = InchesToPoints(1.25)
.LeftMargin = InchesToPoints(1)
.RightMargin = InchesToPoints(1)
.Gutter = InchesToPoints(0)
.HeaderDistance = InchesToPoints(0.5)
.FooterDistance = InchesToPoints(0.5)
.PageWidth = InchesToPoints(11)
.PageHeight = InchesToPoints(8.5)
.FirstPageTray = wdPrinterDefaultBin
.OtherPagesTray = wdPrinterDefaultBin
.SectionStart = wdSectionNewPage
.OddAndEvenPagesHeaderFooter = False
.DifferentFirstPageHeaderFooter = False
.VerticalAlignment = wdAlignVerticalTop
.SuppressEndnotes = False
.MirrorMargins = False
etc.....
End With
{Step 2}
Selection.TypeText Text:= _
"Order No;Customer No;Sale date;Emp No;Shipment;Terms;Total;Tax "
Selection.TypeText Text:="Rate;Freight;Paid"
{Step 3}
Selection.TypeParagraph
Selection.TypeText Text:= _
"100;200;4/9/2005;300;Agent;FOB;20000;0.25;500;18000"
Selection.TypeParagraph
Selection.TypeText Text:="104;205;5/8/2005;302;DHL;15005;0;100;4000"
Selection.MoveLeft Unit:=wdCharacter, Count:=17
Selection.MoveRight Unit:=wdCharacter, Count:=1
Selection.TypeText Text:="Net 30;"
Selection.MoveRight Unit:=wdCharacter, Count:=17
Selection.TypeParagraph
{ Step 4....}
Selection.WholeStory
Application.DefaultTableSeparator = ";"
Selection.ConvertToTable Separator:=wdSeparateByDefaultListSeparator, _
NumColumns:=10, NumRows:=3, AutoFitBehavior:=wdAutoFitFixed
With Selection.Tables(1)
.Style = "Table Contemporary"
.ApplyStyleHeadingRows = True
.ApplyStyleLastRow = True
.ApplyStyleFirstColumn = True
.ApplyStyleLastColumn = True
End With
Selection.Tables(1).Columns(2).SetWidth ColumnWidth:=77.55, RulerStyle:= _
wdAdjustNone
Selection.MoveRight Unit:=wdCell
Selection.MoveRight Unit:=wdCell
Selection.MoveLeft Unit:=wdCell
Selection.SelectColumn
Selection.ParagraphFormat.Alignment = wdAlignParagraphRight
Selection.MoveRight Unit:=wdCell
Selection.MoveRight Unit:=wdCell
Selection.SelectColumn
Selection.ParagraphFormat.Alignment = wdAlignParagraphRight
Selection.MoveRight Unit:=wdCell
Selection.MoveRight Unit:=wdCell
Selection.MoveRight Unit:=wdCell
Selection.SelectColumn
Selection.ParagraphFormat.Alignment = wdAlignParagraphRight
Selection.MoveRight Unit:=wdCell
Selection.MoveRight Unit:=wdCell
Selection.MoveRight Unit:=wdCell
Selection.MoveRight Unit:=wdCell
Selection.SelectColumn
Selection.ParagraphFormat.Alignment = wdAlignParagraphRight
Selection.MoveRight Unit:=wdCell
Selection.MoveRight Unit:=wdCell
Selection.SelectColumn
Selection.ParagraphFormat.Alignment = wdAlignParagraphRight
Selection.MoveRight Unit:=wdCell
Selection.MoveRight Unit:=wdCell
Selection.SelectColumn
Selection.ParagraphFormat.Alignment = wdAlignParagraphRight
Selection.MoveRight Unit:=wdCell
Selection.MoveRight Unit:=wdCell
Selection.SelectColumn
Selection.ParagraphFormat.Alignment = wdAlignParagraphRight
Selection.MoveRight Unit:=wdCell
Selection.SelectRow
Selection.ParagraphFormat.Alignment = wdAlignParagraphCenter
End Sub
unit MainU;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, ExtCtrls, DBCtrls, Grids, DBGrids, DB, DBTables, Buttons,
OleServer, Word2000;
type
TForm1 = class(TForm)
DataSource1: TDataSource;
qry: TQuery;
qryOrderNo: TFloatField;
qryCustNo: TFloatField;
qryEmpNo: TIntegerField;
qrySaleDate: TDateTimeField;
qryShipVIA: TStringField;
qryTerms: TStringField;
qryItemsTotal: TCurrencyField;
qryTaxRate: TFloatField;
qryFreight: TCurrencyField;
qryAmountpaid: TCurrencyField;
DBGrid1: TDBGrid;
DBNavigator1: TDBNavigator;
WordApplication1: TWordApplication;
SpeedButton1: TSpeedButton;
SaveDialog1: TSaveDialog;
procedure SpeedButton1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.SpeedButton1Click(Sender: TObject);
Var
NumLines: integer;
CellMove, ParamTrue, ParamFalse: OleVariant;
Separator,NumRows,NumCols,TableFormat: OleVariant;
SaveFileName: OleVariant;
begin
With SaveDialog1 do
begin
FileName := ';
Filter:= 'Word Files|*.DOC;All Files|*.*';
Title:= 'Export to Word';
if Execute then
begin
qry.Close;
qry.Open;
with WordApplication1 do
Begin
Connect;
try
Documents.Add(EmptyParam,EmptyParam,Emptyparam,EmptyParam);
visible := true;
//Step 1, prepare the page layout
with ActiveDocument.PageSetup do
begin
LineNumbering.Active := 0;
Orientation := wdOrientLandscape;
TopMargin := InchesToPoints(1.25);
BottomMargin := InchesToPoints(1.25);
LeftMargin := InchesToPoints(1);
RightMargin := InchesToPoints(1);
Gutter := InchesToPoints(0);
HeaderDistance := InchesToPoints(0.5);
FooterDistance := InchesToPoints(0.5);
PageWidth := InchesToPoints(11);
PageHeight := InchesToPoints(8.5);
FirstPageTray := wdPrinterDefaultBin ;
OtherPagesTray := wdPrinterDefaultBin ;
SectionStart := wdSectionNewPage ;
OddAndEvenPagesHeaderFooter := 0 ;
DifferentFirstPageHeaderFooter := 0;
VerticalAlignment := wdAlignVerticalTop;
SuppressEndnotes := 0;
MirrorMargins := 0;
end; //with
//Step 2, Preparing the Header with column names
Selection.TypeText('Order #;Cust #;'+
'Sale Date;Emp #;Shipment;Terms;Total;'+
'Tax Rate;Freight;Paid');
//Step 3 Fill in the data from the query
Numlines := 1;
qry.First;
while not qry.Eof do
begin
Selection.TypeParagraph;
Selection.TypeText(qryOrderNo.AsString+';'+
qryCustNo.AsString +';'+
qrySaleDate.AsString +';'+
qryEmpNo.AsString +';'+
qryShipVia.Value +';'+
qryTerms.Value +';'+
qryItemsTotal.DisplayText +';'+
qryTaxrate.AsString +';'+
qryFreight.DisplayText +';'+
qryAmountPaid.DisplayText);
Inc(NumLines);
qry.Next;
end; //while
//Step 4 -- 6
Selection.WholeStory;
ParamTrue := True;
ParamFalse := False;
Separator:=wdSeparateByDefaultListSeparator;
NumRows := NumLines ;
NumCols:=10;
TableFormat := wdTableFormatContemporary;
Selection.ConvertToTable(Separator,NumRows,NumCols,EmptyParam,
TableFormat,EmptyParam,EmptyParam,EmptyParam,EmptyParam,
EmptyParam,EmptyParam,EmptyParam,EmptyParam,EmptyParam,
EmptyParam, ParamTrue);
//Step 7
CellMove := wdCell;
NumCols := 1; //column 1
Selection.MoveRight(CellMove,NumCols,EmptyParam);
Selection.SelectColumn;
Selection.ParagraphFormat.Alignment := wdAlignParagraphRight;
NumCols:= 2; //column 2
Selection.MoveRight(CellMove,NumCols,EmptyParam);
Selection.SelectColumn;
Selection.ParagraphFormat.Alignment := wdAlignParagraphRight;
NumCols:= 3; //column4
Selection.MoveRight(CellMove,NumCols,EmptyParam);
Selection.SelectColumn;
Selection.ParagraphFormat.Alignment := wdAlignParagraphRight;
NumCols:= 4; //colum6
Selection.MoveRight(CellMove,NumCols,EmptyParam);
Selection.SelectColumn;
Selection.ParagraphFormat.Alignment := wdAlignParagraphRight;
NumCols:= 2; //column7
Selection.MoveRight(CellMove,NumCols,EmptyParam);
Selection.SelectColumn;
Selection.ParagraphFormat.Alignment := wdAlignParagraphRight;
Selection.MoveRight(CellMove,NumCols,EmptyParam);
Selection.SelectColumn;
Selection.ParagraphFormat.Alignment := wdAlignParagraphRight;
Selection.MoveRight(CellMove,NumCols,EmptyParam);
Selection.SelectColumn;
Selection.ParagraphFormat.Alignment := wdAlignParagraphRight;
Selection.MoveRight(CellMove,NumCols,EmptyParam);
Selection.SelectColumn;
Selection.ParagraphFormat.Alignment := wdAlignParagraphRight;
//selection of header
Selection.MoveRight(CellMove,EmptyParam,EmptyParam);
Selection.SelectRow;
Selection.ParagraphFormat.Alignment := wdAlignParagraphCenter;
Selection.Rows.HeadingFormat := wdToggle;
Quit;
finally
Disconnect;
end;
end;
end;
end;
end;
end.
NumCols:= 2;
Selection.MoveRight(CellMove,NumCols,EmptyParam);