Jump to content

Using Shape Object in Word.UDF


Recommended Posts

I wrote a code to generate QR Codes and save them to an image.

Now I am looking for a fast and easy way to import all those images to Word and label/print them.

So far I have gotten this far:

#Include <Word.au3>
#Include <Array.au3>
#Include <File.au3>

Local $oWord = _Word_Create()
Local $oDoc = _Word_DocAdd($oWord)
;Local $oDoc = _Word_DocOpen($oWord, @ScriptDir & "\Test.doc")
Local $oRange = _Word_DocRangeSet($oDoc, 0)

$aFList = _FileListToArray(@ScriptDir & "\GeneratedCodes", "*", 0, True)
$aFListShort = _FileListToArray(@ScriptDir & "\GeneratedCodes", "*", 0, False)
;_ArrayDisplay($aFList)

For $i = 1 to $aFList[0]
    $oRange.InsertBefore($aFListShort[$i])
    $oRange.InsertBefore(@CRLF)
    _Word_DocPictureAdd($oDoc, $aFList[$i], True, True, $oRange)    
    $oRange.InsertBefore(@CRLF)
Next

Issue#1 no matter what I do as far as range and .InsertBefore vs .InsertAfter I cant get the "label" above the QR Code but that is OK I am fine with it below just curious why I could not get it working.

Issue#2 The QR codes are a bit too big, so looking to resize them all.  Was looking at the .ScaleWidth method to do this (https://msdn.microsoft.com/en-us/library/office/aa220744(v=office.11).aspx?f=255&MSPPError=-2147217396) so I though I could assign a varible to my DocPictureAdd and send it to this method and while I get no errors I do not see any changes to the size of the images.

The last thing I would want to do is have the label and the image centered upon each other.  I suppose I can select all and center the page but bonus points if I can split the page into 2 columns to print more on a single piece of paper.

Cheers for the help.

Link to comment
Share on other sites

Could you please create a page of QR codes by hand and add a screenshot plus one QR image to this thread so I can play around with them?

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2024-07-28 - Version 1.6.3.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
Share on other sites

How it should look. 

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2024-07-28 - Version 1.6.3.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
Share on other sites

I have been playing a bit and I would suggest to create a table where you then can insert the QR codes plus the text to your liking.
Shouldn't be too hard as soon as I know how your page should look like.

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2024-07-28 - Version 1.6.3.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
Share on other sites

@water

Attached is Test.docx this would be the most basic way for a layout that I was trying for.

Also is Test2.docx using a table, and this does make more sense as I can fit multiples to the page and center the text above the barcode per cell rather than per page.

All the barcodes are scaled to 50% and the label is a Shift+ Enter single line break above them.

Test.docx

Test2.docx

 

As far as resizing this is what I am trying:

$oShape = _Word_DocPictureAdd($oDoc, $aFList[$i], True, True, $oRange)
$oShape.Select
    $oShape.ScaleWidth(.5, True)

Not sure whats wrong with that as $oShape should have a shape object and .scalewidth is a method for that object type.  I look over at the MS site and various VB code sites but most of the code is in a state that does not work with autoit :/

Edited by ViciousXUSMC
Link to comment
Share on other sites

Exactly what I've coded :)
This example inserts 3 QR files plus the text on top of it. You need to prepare the doc with the table plus the formatting of the content (center).
Change the script to process the files in a loop - that's it.
If you do not know how many QR files you need to add to the document then it should be possible to change the script so the word template has only one cell and expands the table as needed.
 

#include <Word.au3>
#include <Array.au3>
#include <File.au3>

Local $oWord = _Word_Create()
Local $oDoc = _Word_DocOpen($oWord, @ScriptDir & "\test.docx")
Local $oRange = _Word_DocRangeSet($oDoc, -1) ; Collapse the range to the start of the document

_Word_DocPictureAdd($oDoc, @ScriptDir & "\QR.jpg", True, True, $oRange)
$oRange.Insertafter("Test")
$oRange = _Word_DocRangeSet($oDoc, $oRange, Default, Default, -1) ; Collapse the range to the end of the range
$oRange.InsertBreak($wdLineBreak) ; Insert a line break

$oRange = _Word_DocRangeSet($oDoc, $oRange, $WdCell, 1) ; Move the range one cell to the right
_Word_DocPictureAdd($oDoc, @ScriptDir & "\QR1.jpg", True, True, $oRange)
$oRange.Insertafter("Test1")
$oRange = _Word_DocRangeSet($oDoc, $oRange, Default, Default, -1) ; Collapse the range to the end of the range
$oRange.InsertBreak($wdLineBreak) ; Insert a line break

$oRange = _Word_DocRangeSet($oDoc, $oRange, $WdCell, 1) ; Move the range one cell to the right
_Word_DocPictureAdd($oDoc, @ScriptDir & "\QR1.jpg", True, True, $oRange)
$oRange.Insertafter("Test1")
$oRange = _Word_DocRangeSet($oDoc, $oRange, Default, Default, -1) ; Collapse the range to the end of the range
$oRange.InsertBreak($wdLineBreak) ; Insert a line break

 

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2024-07-28 - Version 1.6.3.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
Share on other sites

Yeah I was looking at the Tables.Add earlier to do it from a code standpoint.

I'll see what I can do with what you have there, and if needed see about learning how to auto expand the document as that would be nice given we will not always have a full page of stuff to print. 

 

Edit: Oh Water.  Can you tell me what is wrong with my resize method?  I do not see any resize in your example so still stuck on that part. 

Edited by ViciousXUSMC
Link to comment
Share on other sites

Insert the following lines after _Word_DocPictureAdd to lock the aspect ratio and resize the image to 120pt.

$oShape.LockAspectRatio = True
$oShape.Height = 120

If you need a relative size (let's say to the page size) please let me know.

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2024-07-28 - Version 1.6.3.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
Share on other sites

I looked up the properties and saw some of the relative size properties I could play with.  I am ok with static resizing as long as it keeps the image sharp I figured doing a 50% resize was a smart way and it was a good size for me in my  manual testing.

 

I created this as a single sheet import (Needed to at the @CRLF)

#Include <Word.au3>
#Include <Array.au3>
#Include <File.au3>

Local $oWord = _Word_Create()
Local $oDoc = _Word_DocAdd($oWord)
;Local $oDoc = _Word_DocOpen($oWord, @ScriptDir & "\Test.doc")
Local $oRange = _Word_DocRangeSet($oDoc, -1)

$aFList = _FileListToArray(@ScriptDir & "\GeneratedCodes", "*", 0, True)
$aFListShort = _FileListToArray(@ScriptDir & "\GeneratedCodes", "*", 0, False)
;_ArrayDisplay($aFList)



For $i = 1 to $aFList[0]
    $oShape = _Word_DocPictureAdd($oDoc, $aFList[$i], True, True, $oRange)
    $oShape.LockAspectRatio = True
    $oShape.Height = 100
    $oRange.InsertAfter($aFListShort[$i])
    $oRange = _Word_DocRangeSet($oDoc, $oRange, Default, Default, -1) ; Collapse the range to the end of the range
    $oRange.InsertBreak($wdLineBreak) ; Insert a line break
    $oRange.InsertAfter(@CRLF)
Next

As far as the Table version I created this, not sure if its sloppy but its working.  I do have to add the Center Alignment to the document after the fact but that is no big deal.

#include <Word.au3>
#include <Array.au3>
#include <File.au3>

Local $oWord = _Word_Create()
Local $oDoc = _Word_DocOpen($oWord, @ScriptDir & "\Test.docx")
Local $oRange = _Word_DocRangeSet($oDoc, -1) ; Collapse the range to the start of the document


$aFList = _FileListToArray(@ScriptDir & "\GeneratedCodes", "*", 0, True)
$aFListShort = _FileListToArray(@ScriptDir & "\GeneratedCodes", "*", 0, False)
;_ArrayDisplay($aFList)


For $i = 1 to $aFList[0] Step 3
$oShape =_Word_DocPictureAdd($oDoc, $aFList[$i], True, True, $oRange)
$oShape.LockAspectRatio = True
$oShape.Height = 100
$oRange.Insertafter("Test")
$oRange = _Word_DocRangeSet($oDoc, $oRange, Default, Default, -1) ; Collapse the range to the end of the range
$oRange.InsertBreak($wdLineBreak) ; Insert a line break

$oRange = _Word_DocRangeSet($oDoc, $oRange, $WdCell, 1) ; Move the range one cell to the right
$oShape = _Word_DocPictureAdd($oDoc, $aFList[$i+1], True, True, $oRange)
$oShape.LockAspectRatio = True
$oShape.Height = 100
$oRange.Insertafter("Test1")
$oRange = _Word_DocRangeSet($oDoc, $oRange, Default, Default, -1) ; Collapse the range to the end of the range
$oRange.InsertBreak($wdLineBreak) ; Insert a line break

$oRange = _Word_DocRangeSet($oDoc, $oRange, $WdCell, 1) ; Move the range one cell to the right
$oShape = _Word_DocPictureAdd($oDoc, $aFList[$i+2], True, True, $oRange)
$oShape.LockAspectRatio = True
$oShape.Height = 100
$oRange.Insertafter("Test2")
$oRange = _Word_DocRangeSet($oDoc, $oRange, Default, Default, -1) ; Collapse the range to the end of the range
$oRange.InsertBreak($wdLineBreak) ; Insert a line break

$oRange = _Word_DocRangeSet($oDoc, $oRange, $WdCell, 1)
Next

 

The only Issue I have here is if I try to grab an array value that does not exist, but despite the error all the importing would be done at that point.

Link to comment
Share on other sites

Before inserting the second or third QR check if the array entry exists:

If $i+1 > $aFList[0] Then ExitLoop ; Or
If $i+2 > $aFList[0] Then ExitLoop

 

 

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2024-07-28 - Version 1.6.3.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
Share on other sites

Great :)

Cleaned up a bit

#include <Word.au3>
#include <Array.au3>
#include <File.au3>

Local $oWord = _Word_Create()
Local $oDoc = _Word_DocOpen($oWord, @ScriptDir & "\Test.docx")
Local $oRange = _Word_DocRangeSet($oDoc, -1) ; Collapse the range to the start of the document


$aFList = _FileListToArray(@ScriptDir & "\GeneratedCodes", "*", 0, True)
$aFListShort = _FileListToArray(@ScriptDir & "\GeneratedCodes", "*", 0, False)
;_ArrayDisplay($aFList)


For $i = 1 to $aFList[0] Step 3
$oShape =_Word_DocPictureAdd($oDoc, $aFList[$i], True, True, $oRange)
$oShape.LockAspectRatio = True
$oShape.Height = 100
$oRange.Insertafter(StringTrimRight($aFListShort[$i], 4))
$oRange = _Word_DocRangeSet($oDoc, $oRange, Default, Default, -1) ; Collapse the range to the end of the range
$oRange.InsertBreak($wdLineBreak) ; Insert a line break

If $i+1 > $aFList[0] Then ExitLoop
$oRange = _Word_DocRangeSet($oDoc, $oRange, $WdCell, 1) ; Move the range one cell to the right
$oShape = _Word_DocPictureAdd($oDoc, $aFList[$i+1], True, True, $oRange)
$oShape.LockAspectRatio = True
$oShape.Height = 100
$oRange.Insertafter(StringTrimRight($aFListShort[$i+1], 4))
$oRange = _Word_DocRangeSet($oDoc, $oRange, Default, Default, -1) ; Collapse the range to the end of the range
$oRange.InsertBreak($wdLineBreak) ; Insert a line break

If $i+2 > $aFList[0] Then ExitLoop
$oRange = _Word_DocRangeSet($oDoc, $oRange, $WdCell, 1) ; Move the range one cell to the right
$oShape = _Word_DocPictureAdd($oDoc, $aFList[$i+2], True, True, $oRange)
$oShape.LockAspectRatio = True
$oShape.Height = 100
$oRange.Insertafter(StringTrimRight($aFListShort[$i+2], 4))
$oRange = _Word_DocRangeSet($oDoc, $oRange, Default, Default, -1) ; Collapse the range to the end of the range
$oRange.InsertBreak($wdLineBreak) ; Insert a line break

$oRange = _Word_DocRangeSet($oDoc, $oRange, $WdCell, 1)
Next

 

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...