Hi
Hi
I need help with word automation. I need to open a word file, add a watermak and save it as pdf.
I can Open it and save it to pdf with the below code but couldn't manage to add a watermark. I saved a macro while adding the watermark but I'm having difficulty changing the macro to delphi code.
Any help will be appreciated
var
WordApp, Doc: Variant;
begin
WordApp := CreateOleObject('Word.Application');
Doc := WordApp.Documents.Open('D:\document.doc');
// addwatermark???
Doc.ExportAsFixedFormat('D:\document.pdf', 17);
WordApp.Quit;
Doc := Unassigned;
WordApp := Unassigned;
The macro:
Sub watermark()
ActiveDocument.Sections(1).Range.Select
ActiveWindow.ActivePane.View.SeekView = wdSeekCurrentPageHeader
Selection.HeaderFooter.Shapes.AddTextEffect(PowerPlusWaterMarkObject, "URGENT", "Calibri", 1, False, False, 0, 0).Select
Selection.ShapeRange.Name = "PowerPlusWaterMarkObject"
Selection.ShapeRange.TextEffect.NormalizedHeight = False
Selection.ShapeRange.Line.Visible = False
Selection.ShapeRange.Fill.Visible = True
Selection.ShapeRange.Fill.Solid
Selection.ShapeRange.Fill.ForeColor.RGB = KYM(192, 192, 192)
Selection.ShapeRange.Fill.Transparency = 0.5
Selection.ShapeRange.Rotation = 315
Selection.ShapeRange.LockAspectRatio = True
Selection.ShapeRange.Height = CentimetersToPoints(3.76)
Selection.ShapeRange.Width = CentimetersToPoints(18.8)
Selection.ShapeRange.WrapFormat.AllowOverlap = True
Selection.ShapeRange.WrapFormat.Side = wdWrapNone
Selection.ShapeRange.WrapFormat.Type = 3
Selection.ShapeRange.RelativeHorizontalPosition = wdRelativeVerticalPositionMargin
Selection.ShapeRange.RelativeVerticalPosition = wdRelativeVerticalPositionMargin
Selection.ShapeRange.Left = wdShapeCenter
Selection.ShapeRange.Top = wdShapeCenter
ActiveWindow.ActivePane.View.SeekView = wdSeekMainDocument
End Sub
I need help with word automation. I need to open a word file, add a watermak and save it as pdf.
I can Open it and save it to pdf with the below code but couldn't manage to add a watermark. I saved a macro while adding the watermark but I'm having difficulty changing the macro to delphi code.
Any help will be appreciated
var
WordApp, Doc: Variant;
begin
WordApp := CreateOleObject('Word.Application');
Doc := WordApp.Documents.Open('D:\document.doc');
// addwatermark???
Doc.ExportAsFixedFormat('D:\document.pdf', 17);
WordApp.Quit;
Doc := Unassigned;
WordApp := Unassigned;
The macro:
Sub watermark()
ActiveDocument.Sections(1).Range.Select
ActiveWindow.ActivePane.View.SeekView = wdSeekCurrentPageHeader
Selection.HeaderFooter.Shapes.AddTextEffect(PowerPlusWaterMarkObject, "URGENT", "Calibri", 1, False, False, 0, 0).Select
Selection.ShapeRange.Name = "PowerPlusWaterMarkObject"
Selection.ShapeRange.TextEffect.NormalizedHeight = False
Selection.ShapeRange.Line.Visible = False
Selection.ShapeRange.Fill.Visible = True
Selection.ShapeRange.Fill.Solid
Selection.ShapeRange.Fill.ForeColor.RGB = KYM(192, 192, 192)
Selection.ShapeRange.Fill.Transparency = 0.5
Selection.ShapeRange.Rotation = 315
Selection.ShapeRange.LockAspectRatio = True
Selection.ShapeRange.Height = CentimetersToPoints(3.76)
Selection.ShapeRange.Width = CentimetersToPoints(18.8)
Selection.ShapeRange.WrapFormat.AllowOverlap = True
Selection.ShapeRange.WrapFormat.Side = wdWrapNone
Selection.ShapeRange.WrapFormat.Type = 3
Selection.ShapeRange.RelativeHorizontalPosition = wdRelativeVerticalPositionMargin
Selection.ShapeRange.RelativeVerticalPosition = wdRelativeVerticalPositionMargin
Selection.ShapeRange.Left = wdShapeCenter
Selection.ShapeRange.Top = wdShapeCenter
ActiveWindow.ActivePane.View.SeekView = wdSeekMainDocument
End Sub
Qing-Shan Xiao You saved my day :) Thanks alot.
ReplyDeleteI added open file and ExportAsFixedFormat to the code. The codes are below for the curious. It took me a while to figure out the parameters.
doc := wordAp.Documents.Open('c:\doc_den.doc', EmptyParam, EmptyParam, EmptyParam, EmptyParam, EmptyParam,
EmptyParam, EmptyParam, EmptyParam, varFalse, EmptyParam, EmptyParam, EmptyParam, EmptyParam, EmptyParam, EmptyParam);
wordAp.Visible := true;
...
... add watermark
...
...
...
doc.ExportAsFixedFormat('c:\doc_den.pdf', wdExportFormatPDF,
varFalse, wdExportOptimizeForPrint,
wdExportAllDocument, 1, 999,
wdExportDocumentContent, varFalse, varFalse,
wdExportCreateNoBookmarks,
varFalse, varFalse,
varFalse, EmptyParam);