Jump to content

Microsoft Word Automation Library


big_daddy
 Share

Recommended Posts

$objWord = ObjCreate("Word.Application")

$objWord.Visible = 1

$objDoc = $objWord.Documents.Add.Range()

$objSelection = $objWord.Selection

;$objSelection.InsertBreak.Type=7

With $objSelection.Paragraphs(1)

.Borders(-3).LineStyle = 1

.Borders(-3).LineWidth = 18

.Borders(-3).LineStyle.RGB = 255;;;;????????

EndWith

Link to comment
Share on other sites

  • Replies 115
  • Created
  • Last Reply

Top Posters In This Topic

Top Posters In This Topic

  • Moderators

@xhyxsl - I guess you were asking a question?

#include <Word.au3>

$wdColorRed = 255

_WordErrorHandlerRegister()
$oWordApp = _WordCreate()
$oDoc = _WordDocGetCollection($oWordApp, 0)
$oSelection = $oWordApp.Selection

With $oSelection.Paragraphs (1)
    .Borders (-3).LineStyle = 1
    .Borders (-3).LineWidth = 18
    .Borders (-3).Color = $wdColorRed
EndWith
Link to comment
Share on other sites

Thanks!!!

What a great answer you've given! You really do me a big favor,I just want to thank you again.

Please check the scripts below:

#include <Word.au3>
_WordErrorHandlerRegister()
$oWordApp = _WordCreate()
$oDoc = _WordDocGetCollection($oWordApp, 0)
$myShape = $oDoc.Shapes.AddLine(90, 145, 500, 145)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
With $myShape
    .Line.Weight = 1.8
    .Line.DashStyle = 1
    .Line.Style = 1
    .Line.Transparency = 0
    .Line.Visible = -1
    .Line.ForeColor.RGB = 255
    .Line.BackColor.RGB = 1677215
    .Line.BeginArrowheadLength = 2
    .Line.BeginArrowheadWidth = 2
    .Line.BeginArrowheadStyle = 1
    .Line.EndArrowheadLength = 1
    .Line.EndArrowheadWidth = 2
    .Line.EndArrowheadStyle = 1
EndWith

The above scripts are followed your suggestion and I made it when I run it.

Now I just want you to give me your advise about my adaptation.

(For instance,Is there any mistakes in it?)

I would really appreciate about that.

(Please forgive me, I'm poor in English)

Link to comment
Share on other sites

  • Moderators

Your welcome!

As for the code, I don't see any mistakes. The only thing I would change is to rename $myShape to $oShape, but that is just due to my habits.

Edit: Spelling

Edited by big_daddy
Link to comment
Share on other sites

Thanks!!!

#include <Word.au3>
$oWordApp = _WordCreate ("", 0, 1)
$oDoc = _WordDocGetCollection ($oWordApp, 0)
$my_Text = "Microsoft W o r d Included in the beta version as of v3.2.3.9"
_WordDocParagraphFormat($oDoc,$my_Text,255,26,18)

$objSelection = $oWordApp.Selection
_WordDocFind($objSelection,$my_Text);;;look up string
$objSelection.Font.Spacing = -2.5;;;;adjust the distance between words to 2.5
_WordDocFind($objSelection,"v3.2.3.9")
$objSelection.Font.Color = 0

Func _WordDocParagraphFormat($s_oDoc,$s_Text,$s_Color,$s_Size,$s_Number)
    With $oDoc.Range
        .ParagraphFormat.Alignment = 1
        .ParagraphFormat.LineSpacing = 36
        .InsertAfter ($s_Text)
        .Font.Name = "Arial"
        .Font.Color = $s_Color
        .Font.Size = $s_Size
        .SetRange (0, $s_Number)
        .TwoLinesInOne = 1
    EndWith
EndFunc

Func _WordDocFind($s_objSelection,$s_Text)
    With $s_objSelection.Find
        .Text = $s_Text
        .Execute
    EndWith
EndFunc

I made two other scripts above. I'm begging you to help me design a more perfect UDF.

Link to comment
Share on other sites

  • 2 weeks later...

Excuse me man, I have to bother you again.

I got a problem about "PageSetup".

#include <Word.au3>
Const $wdOrientPortrait = 0
Const $wdOrientLandscape = 1
Const $wdPrinterDefaultBin = 0
Const $wdPrinterUpperBin = 1
Const $wdPrinterOnlyBin = 1
Const $wdPrinterLowerBin = 2
Const $wdPrinterMiddleBin = 3
Const $wdPrinterManualFeed = 4
Const $wdPrinterEnvelopeFeed = 5
Const $wdPrinterManualEnvelopeFeed = 6
Const $wdPrinterAutomaticSheetFeed = 7
Const $wdPrinterTractorFeed = 8
Const $wdPrinterSmallFormatBin = 9
Const $wdPrinterLargeFormatBin = 10
Const $wdPrinterLargeCapacityBin = 11
Const $wdPrinterPaperCassette = 14
Const $wdPrinterFormSource = 15
Const $wdSectionContinuous = 0
Const $wdSectionNewColumn = 1
Const $wdSectionNewPage = 2
Const $wdSectionEvenPage = 3
Const $wdSectionOddPage = 4
Const $wdAlignVerticalTop = 0
Const $wdAlignVerticalCenter = 1
Const $wdAlignVerticalJustify = 2
Const $wdAlignVerticalBottom = 3
Const $wdGutterPosLeft = 0
Const $wdGutterPosTop = 1
Const $wdGutterPosRight = 2
Const $wdLayoutModeDefault = 0
Const $wdLayoutModeGrid = 1
Const $wdLayoutModeLineGrid = 2
Const $wdLayoutModeGenko = 3
Const $wdLineSpaceExactly = 4
Const $wdAlignParagraphJustify = 3
Const $wdOutlineLevelBodyText = 10
Const $wdBaselineAlignAuto = 4
Const $wdStyleNormal = -1;(&HFFFFFFFF)

$oWordApp =  _WordCreate ("", 0, 1)
$oDoc = _WordDocGetCollection($oWordApp, 0)
$objSelection = $oWordApp.Selection
Word_ParagraphFormat($objSelection,0);;;;;;;;;;;correct
$objSelection.TypeParagraph
Word_ParagraphFormat($objSelection,1);;;;;;;;;;;correct
$objSelection.TypeParagraph
Word_ParagraphFormat($objSelection,0);;;;;;;;;;;Why does the result change to 15.6 pound which the correct one should be 0 ???
Func Word_ParagraphFormat($obj,$s_Line)
    With $obj.ParagraphFormat
        .LeftIndent = CentimetersToPoints(0)
        .RightIndent = CentimetersToPoints(0)
        .SpaceBefore = 0
        .SpaceBeforeAuto = False
        .SpaceAfter = 0
        .SpaceAfterAuto = False
        .LineSpacingRule = $wdLineSpaceExactly
        .LineSpacing = 28
        .Alignment = $wdAlignParagraphJustify
        .WidowControl = False
        .KeepWithNext = False
        .KeepTogether = False
        .PageBreakBefore = False
        .NoLineNumber = False
        .Hyphenation = -1
        .FirstLineIndent = CentimetersToPoints(0)
        .OutlineLevel = $wdOutlineLevelBodyText
        .CharacterUnitLeftIndent = 0
        .CharacterUnitRightIndent = 0
        .CharacterUnitFirstLineIndent = 0
        .LineUnitBefore = $s_Line
        .LineUnitAfter = 0
        .AutoAdjustRightIndent = -1
        .DisableLineHeightGrid = False
        .FarEastLineBreakControl = -1
        .WordWrap = -1
        .HangingPunctuation = -1
        .HalfWidthPunctuationOnTopOfLine = False
        .AddSpaceBetweenFarEastAndAlpha = -1
        .AddSpaceBetweenFarEastAndDigit = -1
        .BaseLineAlignment = $wdBaselineAlignAuto
    EndWith
EndFunc
Func CentimetersToPoints($i_Cent)
    Return ($i_Cent * 28.35)
EndFunc  ;==>CentimetersToPoints
Link to comment
Share on other sites

  • 2 weeks later...

You seem like you know what your doing :)) Thanks for this btw... I have an issue tho, I added a WWW link to a word doc but I need to change the font,font size, and bold it.... not to sure how I would go about doing that... any help would be GREAT!

Link to comment
Share on other sites

  • Moderators

@xhyxsl - I really don't understand what you are trying to accomplish. Could you explain in more detail your goal, and what the code posted does incorrectly.

---

@revtex - Due to your question I realized _WordDocAddLink() needs enhanced to return the hyperlink object. This will be fixed in the next release, but until then you can accomplish it like this...

#include <Word.au3>

$oWordApp = _WordCreate()
$oDoc = _WordDocGetCollection($oWordApp, 0)
_WordDocAddLink($oDoc, "", "www.AutoIt3.com", "", "This is a link to AutoIt's homepage.", "AutoIt Homepage")
$oLink = _WordDocLinkGetCollection($oDoc, 1)
With $oLink.Range.Font
    .Name = "Arial"
    .Size = 24
    .Bold = True
EndWith
Link to comment
Share on other sites

@xhyxsl - I really don't understand what you are trying to accomplish. Could you explain in more detail your goal, and what the code posted does incorrectly.

My Word2003 is Chinese version, maybe that's why you didn't understand some parts.

Word_ParagraphFormat($objSelection,0);;;;;;;;;;;First, I set "0" and it runs correctly
$objSelection.TypeParagraph
Word_ParagraphFormat($objSelection,1);;;;;;;;;;;Second, I change it to "1" and it also runs correctly("1" means the distant between paragraphes is one line)
$objSelection.TypeParagraph
Word_ParagraphFormat($objSelection,0);;;;;;;;;;;But when I try to change it back to "0", it oddly sets "15.6 pound" .
;("pound" and "line" are two different measurements in Chinese version)

I just want you to help me find out which script that I used is incorrectly.

Or you can help me make an English version script.

That would be great help,thanks.

Link to comment
Share on other sites

#include <Word.au3>

$oWordApp = _WordCreate ("", 0, 1)

$oSel = _WordDocGetCollection ($oWordApp, 2)

;$oSel <<=>> $objSelection = $oWordApp.Selection !!!! :)

;

Func _WordDocGetCollection(ByRef $o_object, $v_index = -1)

If Not IsObj($o_object) Then

__WordErrorNotify("Error", "_WordDocGetCollection", "$_WordStatus_InvalidDataType")

SetError($_WordStatus_InvalidDataType, 1)

Return 0

EndIf

;

If Not __WordIsObjType($o_object, "application") Then

__WordErrorNotify("Error", "_WordDocGetCollection", "$_WordStatus_InvalidObjectType")

SetError($_WordStatus_InvalidObjectType, 1)

Return 0

EndIf

;

If IsNumber($v_index) Then

Select

Case $v_index = -1

SetError($_WordStatus_Success)

SetExtended($o_object.Documents.Count)

Return $o_object.Documents

Case $v_index = 0

SetError($_WordStatus_Success)

SetExtended($o_object.Documents.Count)

Return $o_object.ActiveDocument

Case $v_index > 0 And $v_index <= $o_object.Documents.Count And $v_index < 2

SetError($_WordStatus_Success)

SetExtended($o_object.Documents.Count)

Return $o_object.Documents ($v_index)

Case $v_index = 2

SetError($_WordStatus_Success)

SetExtended($o_object.Selection)

Return $o_object.Selection

Case $v_index < -1

__WordErrorNotify("Error", "_WordDocGetCollection", "$_WordStatus_InvalidValue")

SetError($_WordStatus_InvalidValue, 2)

Return 0

Case Else

__WordErrorNotify("Warning", "_WordDocGetCollection", "$_WordStatus_NoMatch")

SetError($_WordStatus_NoMatch, 2)

Return 0

EndSelect

Else

For $o_doc In $o_object.Documents

If $o_doc.Name = $v_index Then

SetError($_WordStatus_Success)

SetExtended($o_object.Documents.Count)

Return $o_doc

EndIf

Next

__WordErrorNotify("Warning", "_WordDocGetCollection", "$_WordStatus_NoMatch")

SetError($_WordStatus_NoMatch, 2)

Return 0

EndIf

EndFunc ;==>_WordDocGetCollection

Edited by xhyxsl
Link to comment
Share on other sites

Is there a way to do this...

I need to open a html file on our Intranet that has graphs in JPG format. When trying to print these from IE, the graphs will get split between pages when printed. Word is smart enough not to split the image, but put the whole thing on the next page. This is how I do this manually:

Open Word

File -> Open -> data.html

Page Setup, set all 4 margins to .3

Print

The only thing that I did not see in Word.au3 was how to change the margins. How can I do this? I need to do this for about 50 different html files. Any help would be greatly appreciated.

Thanks,

-John

Edited by jftuga
Link to comment
Share on other sites

  • Moderators

@jftuga - This should get you headed in the right direction.

#include <Word.au3>

; Path to the directory containing the .html files
$sDir = "c:\test\"
; Create an invisible MS Word Application
$oWordApp = _WordCreate("", 0, 0)

; Searches for filenames of all .html files in the specified directory.
$search = FileFindFirstFile($sDir & "*.html")
; Check if the search was successful
If $search = -1 Then
    MsgBox(0, "Error", "No files/directories matched the search pattern")
    Exit
EndIf

While 1
    $file = FileFindNextFile($search)
    If @error Then ExitLoop

    $oDoc = _WordDocOpen($oWordApp, $sDir & $file)
    If @error Then
        MsgBox(0, "Error", "There was an error trying to open " & $file)
        ContinueLoop
    EndIf
    With $oDoc.PageSetup
        .TopMargin = _InchesToPoints(0.3)
        .BottomMargin = _InchesToPoints(0.3)
        .LeftMargin = _InchesToPoints(0.3)
        .RightMargin = _InchesToPoints(0.3)
    EndWith
    _WordDocPrint($oDoc)
    _WordDocClose($oDoc, 0)
WEnd

; Close the search handle
FileClose($search)
; Close the MS Word Application
_WordQuit($oWordApp)

Func _InchesToPoints($i)
    Return ($i * 72)
EndFunc   ;==>_InchesToPoints
Link to comment
Share on other sites

  • 2 weeks later...

Hey Bob,

Wanted to let you know some functionality I'd love to see in this great UDF.

One of the things I typically want to do is to create a docuement from scratch, programatically. This would mean 1) create an empty doc, 2) typeText into it, 3) apply formatting to that text and 4) repeat 2 and 3 and eventually close the document. I may often want to set an insertion point or create a selection and then perform steps 2 and/or 3.

Have you already considered how you might do this? I can put together some prototype code if you want it, but I suspect you don't need it.

thanks,

Dale

Free Internet Tools: DebugBar, AutoIt IE Builder, HTTP UDF, MODIV2, IE Developer Toolbar, IEDocMon, Fiddler, HTML Validator, WGet, curl

MSDN docs: InternetExplorer Object, Document Object, Overviews and Tutorials, DHTML Objects, DHTML Events, WinHttpRequest, XmlHttpRequest, Cross-Frame Scripting, Office object model

Automate input type=file (Related)

Alternative to _IECreateEmbedded? better: _IECreatePseudoEmbedded  Better Better?

IE.au3 issues with Vista - Workarounds

SciTe Debug mode - it's magic: #AutoIt3Wrapper_run_debug_mode=Y

Doesn't work needs to be ripped out of the troubleshooting lexicon. It means that what you tried did not produce the results you expected. It begs the questions 1) what did you try?, 2) what did you expect? and 3) what happened instead?

Reproducer: a small (the smallest?) piece of stand-alone code that demonstrates your trouble

Link to comment
Share on other sites

Hey Bob,

Wanted to let you know some functionality I'd love to see in this great UDF.

One of the things I typically want to do is to create a docuement from scratch, programatically. This would mean 1) create an empty doc, 2) typeText into it, 3) apply formatting to that text and 4) repeat 2 and 3 and eventually close the document. I may often want to set an insertion point or create a selection and then perform steps 2 and/or 3.

Have you already considered how you might do this? I can put together some prototype code if you want it, but I suspect you don't need it.

thanks,

Dale

Dale, I already have that. I'll see if I can find it and send it to you by PM. Probably needs some error hanling routines built into it though. I also have one for building a table and filling in the fields.

George

Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.

Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.***

The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number.

Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else.

"Old age and treachery will always overcome youth and skill!"

Link to comment
Share on other sites

  • 2 months later...

See post #92 in this thread.

I needed to change my program to simply open a Word doc. The end user will then make edits and then print themselves, with the margins already set by AutoIt. So I commented out the WordPrint, Close, Quit, etc., functions since the user will do this.

The problem is that Word 2003 open 2 times, once with an empty document and once with the desired file. To fix this, I changed the _WordCreate() function in Word.au3 (version 1.0-1, 4/14/07).

Towards the end of this function, line 147:

from:

If $s_FilePath = "" Then
        _WordDocAdd($o_object)
    Else
        _WordDocOpen($o_object, $s_FilePath)
    EndIf

The only difference being the return in front of the _WordDocOpen() call.

So is there a cleaner way to do this?

Thanks,

-John

Link to comment
Share on other sites

Dale, I already have that. I'll see if I can find it and send it to you by PM. Probably needs some error hanling routines built into it though. I also have one for building a table and filling in the fields.

Hey...

I'm curious, if it's possible, I would also love to get my hands on this code. I too am looking for some word document creation automation, and this looks like it's perfect for what I'm trying to accomplish.

Anyways, if you could get back to me, I would very much appreciate it.

thanks

Link to comment
Share on other sites

  • 2 weeks later...

I'm trying to open a word doc (sample.dot) which has a word macro to install a word macro if it doesn't Exist.

If I use _wordcreate("sample.dot") the wordmacro starts, but the Autoit macro sits at _wordcreate until the macro is done.

I know this because I added a consolewrite command right after it.

I need to automate this word macro, since it ask lots of questions.

Thanks

Hazed

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...