Page 1 of 1 1
Topic Options
#208035 - 2013-11-18 10:02 AM Use word to print some asset info
Sam_B Offline
Getting the hang of it

Registered: 2005-10-25
Posts: 68
Loc: Bern, Switzerland
Hi all,

I'd like to print out a little inventory sheet of a PC. My idea is to use word to design the layout of the document and use docvars to print out the content of the corresponding variables.

The content of the word template looks like that:

Asset Tag: {DOCVARIABLE AssetTag \* MERGEFORMAT}
Serial #: {DOCVARIABLE SerialNumber \* MERGEFORMAT}

My code goes here:

 Code:
;=======================================================================================
;	Name	    	: PCInventory.kix
;	Author	  	: SBLAETTL
;	Date		: 14.11.2013
;	Purpose 	: Prints a paper with all relevant data of a redeployable asset
;	Ext. Dependencies :	
;	
;-------------------------------------------------------------------------------
;	Changes:
;	Author		Date
;=========================================================================================
Break on
Dim $Ret
$Ret = SetOption("Explicit", "on")

Dim $objWordApp
Dim $objWordDoc
Dim $objDoc
Dim $Template

$objWordApp = CreateObject("Word.Application")
If @ERROR <> 0
  $ret = MessageBox("unable to start Word on this device, terminating", "Problem with Office")
  Quit
EndIf
$Template = @SCRIPTDIR + "\Asset Tag.dotx"
If Not(Exist($Template))
  $ret = MessageBox("unable to find template " + $Template + ", terminating", "Problem with configuration")
  Quit
EndIf

$ObjWordDoc = $objWordApp.Application.Documents.Add($Template)

Dim $oField
For Each $oField in $objWordDoc.Fields
 ? "Field name :" + $ofield.name
Next

$Ret = $objWordDoc.Variables.item["AssetTag"].Value = "SomeAssetTagInfo"
$Ret = $objWordDoc.Variables.item["SerialNumber"] .Value = "Here goes Serial Number"
$Ret = $objWordDoc.Fields.Update()

$Ret = $objWordApp.Documents(1).SaveAs(@SCRIPTDIR + "\Useless Text.doc")
$Ret = $objWordApp.quit()



Now my problems... My code finds the two fields (it loops twice) but I'm unable to print out the field names :-( Futhermore, I'm also not able to fill the two docvars... I'm stuck...

Thanks once again for your help

Sam


Edited by Sam_B (2013-11-18 10:03 AM)

Top
#208036 - 2013-11-18 04:34 PM Re: Use word to print some asset info [Re: Sam_B]
Lonkero Administrator Offline
KiX Master Guru
*****

Registered: 2001-06-05
Posts: 22346
Loc: OK
$objWordDoc.Variables.item["AssetTag"].Value should never work.

$objWordDoc.Variables.item("AssetTag").Value

most likely will.
you saying you are actually using this script without causing kix to crash with syntax error???
_________________________
!

download KiXnet

Top
#208080 - 2013-11-28 09:02 AM Re: Use word to print some asset info [Re: Lonkero]
Sam_B Offline
Getting the hang of it

Registered: 2005-10-25
Posts: 68
Loc: Bern, Switzerland
I'm affraid, $objWordDoc.Variables.item("AssetTag").Value did not work either. No value...

I implemented now a workaround by creating a csv file and then call word to do a mail merge. Not that elegant than my first approach, but it is working
 Code:
; ---------------------------------------------------------------------------------------------------------------------------------------
; Merge data of asset into corresponding fields and generate a PDF file
; ---------------------------------------------------------------------------------------------------------------------------------------
Function GenerateAssetPage($strPath, $strDataSource, $strTemplate)
  Dim $Ret
  Dim $objDoc
  Dim $objWordApp
            
  Dim $wdDoNotSaveChanges
  $wdDoNotSaveChanges = 0 ;Do not save pending changes
          
  $objWordApp = CreateObject("Word.Application")
  If @ERROR <> 0
    $Ret = MessageBox(@SERROR + " at CreateObject(Word.Application), terminating", "Problem with Office", 16)
    $objWordApp.quit($wdDoNotSaveChanges)
    Quit
  EndIf
  ; ---------------------------------------------------------------------------------------------------------------------------------------
  ; Start Word using mailmerge template
  ; ---------------------------------------------------------------------------------------------------------------------------------------
  $objDoc = $objWordApp.Documents.Add(@SCRIPTDIR + "\" + $strTemplate)
  If @ERROR <> 0
    $Ret = MessageBox("Unable to open template " + $strPath + $strTemplate + ", terminating", "Problem with Office", 16)
    $objWordApp.quit($wdDoNotSaveChanges)
    Quit
  EndIf
  ; ---------------------------------------------------------------------------------------------------------------------------------------
  ;Do the mail merge to a new document.
  ; ---------------------------------------------------------------------------------------------------------------------------------------
  $objDoc.MailMerge.OpenDataSource($strPath + $strDataSource)
  If @ERROR <> 0
    $Ret = MessageBox("Unable to open datasource " + $strPath + $strDataSource + ", terminating", "Problem with Office", 16)
    $objWordApp.quit($wdDoNotSaveChanges)
    Quit
  EndIf
  ; ---------------------------------------------------------------------------------------------------------------------------------------
  ;checking value of named constant: wdSendToNewDocument
  ; ---------------------------------------------------------------------------------------------------------------------------------------
  $objDoc.MailMerge.Destination = 0
  $objDoc.MailMerge.SuppressBlankLines = 1
  ; ---------------------------------------------------------------------------------------------------------------------------------------        
  ;setting to values of named constants: wdDefaultFirstRecord and wdDefaultLastRecord
  ; ---------------------------------------------------------------------------------------------------------------------------------------
  Dim $wdDefaultFirstRecord, $wdDefaultLastRecord
  $wdDefaultFirstRecord = 1
  $wdDefaultLastRecord = -16
          
  $objDoc.MailMerge.DataSource.FirstRecord = $wdDefaultFirstRecord
  $objDoc.MailMerge.DataSource.LastRecord = $wdDefaultLastRecord
  ; ---------------------------------------------------------------------------------------------------------------------------------------
  ;checking value of named constant: wdMainAndDataSource
  ; ---------------------------------------------------------------------------------------------------------------------------------------
  If $objDoc.MailMerge.State = 2 
    $objDoc.MailMerge.Execute
    If @ERROR <> 0
      $Ret = MessageBox("Unable to execute mailmerge, terminating", "Problem with Office", 16)
      $objWordApp.quit($wdDoNotSaveChanges)
      Quit
    EndIf
  EndIf
  ; ---------------------------------------------------------------------------------------------------------------------------------------      
  ;Do not display the mail merge document
  ; ---------------------------------------------------------------------------------------------------------------------------------------
  $objWordApp.Visible = False
  ; ---------------------------------------------------------------------------------------------------------------------------------------
  ;Save mail merge document as PDF
  ; ---------------------------------------------------------------------------------------------------------------------------------------
  Dim $wdFormatPDF
  $wdFormatPDF = 17
  $objWordApp.ActiveDocument.SaveAs($strPath + @WKSTA + ".pdf", $wdFormatPDF)
  If @ERROR <> 0
    $Ret = MessageBox("Unable to save PDF, terminating", "Problem with Office", 16)
    $objWordApp.quit($wdDoNotSaveChanges)
    Quit
  EndIf
  $objWordApp.quit($wdDoNotSaveChanges)
EndFunction


Edited by Sam_B (2013-11-28 09:03 AM)

Top
Page 1 of 1 1


Moderator:  Shawn, ShaneEP, Ruud van Velsen, Arend_, Jochen, Radimus, Glenn Barnas, Allen, Mart 
Hop to:
Shout Box

Who's Online
0 registered and 456 anonymous users online.
Newest Members
M_Moore, BeeEm, min_seow, Audio, Hoschi
17883 Registered Users

Generated in 0.099 seconds in which 0.072 seconds were spent on a total of 13 queries. Zlib compression enabled.

Search the board with:
superb Board Search
or try with google:
Google
Web kixtart.org