Announcement

Collapse
No announcement yet.

COM and MS Word

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • COM and MS Word

    I am trying set a page to landscape using COM, but have failed (many times).Everything works OK. I have had a look on the net, and it does seem that others have had this problem. I cannot see where I am going wrong - any experts out there that can help an old man?

    Iain Johnstone


    <code>
    '##########################################
    'from PB samples
    FUNCTION OutputToWord (message AS STRING) AS LONG 'add orientation as long, typeface as string to finished code

    DIM oWordApp AS WordApplication ' Application Interface
    DIM oWordDoc AS WordDocument ' Document Interface
    DIM oWordSel AS WordSelection ' Selection
    DIM oWordFont AS WordFont 'font
    DIM oWordPageSetup AS WordPageSetup 'set orientation etc
    DIM sProgID_Word AS STRING

    DIM vBool AS VARIANT
    DIM vText AS VARIANT
    DIM vFile AS VARIANT
    DIM vFileFmt AS VARIANT
    DIM vVnt AS VARIANT
    DIM Result AS STRING

    '----------------------------------------------------------------
    ' Note: For some components, the full version needs to be used,
    ' but else a version independent part of it is enough.
    sProgID_Word = PROGID$(CLSID$("Word.Application"))
    IF LEN(sProgID_Word) = 0 THEN sProgID_Word = "Word.Application"
    '----------------------------------------------------------------

    ' Open an instance of MSWORD
    SET oWordApp = WordApplication IN sProgID_Word
    IF ISFALSE ISOBJECT(oWordApp) THEN _
    SET oWordApp = NEW WordApplication IN sProgID_Word

    ' Could MSWORD be opened? If not, terminate this app
    IF ISFALSE ISOBJECT(oWordApp) THEN
    MSGBOX "Unable to open or start MSWORD!",%MB_TASKMODAL,"Masterdrain"
    EXIT FUNCTION
    END IF

    ' Make MSWORD visible in for example normal show state
    LET vBool = %wdWindowStateNormal ' or %wdWindowStateMaximize
    OBJECT LET oWordApp.WindowState = vBool
    LET vBool = 1
    OBJECT LET oWordApp.Visible = vBool
    '----------------------------------------------------------------

    ' Get the current document count from the "documents collection"
    LET vVnt = 0
    OBJECT GET oWordApp.Documents.Count TO vVnt

    ' Add a document to the "documents collection", and obtain a reference
    ' to the document Interface for the new document
    LET vVnt = 0
    OBJECT CALL oWordApp.Documents.ADD TO vVnt
    IF OBJRESULT OR ERR THEN
    MSGBOX "MSWORD was not able to start a new document." & $CR & _
    "Please ensure MSWord and VBA are installed.",%MB_TASKMODAL,"Masterdrain"
    ELSE
    SET oWordDoc = vVnt

    ' Get the current document count from the "documents collection"
    ' This should be one value higher than before
    LET vVnt = 0
    OBJECT GET oWordApp.Documents.Count TO vVnt


    'set orientation and margins
    '###############################################
    ' VB example code below
    'Sub InsertLand()
    '
    '' This macro inserts a landscaped page, followed by a portrait page; another favorite.
    '
    ' Selection.InsertBreak Type:=wdSectionBreakNextPage
    ' Selection.InsertBreak Type:=wdSectionBreakNextPage
    ' Selection.MoveUp Unit:=wdLine, Count:=1
    '
    ' With Selection.PageSetup
    '
    ' .Orientation = wdOrientLandscape
    ' .TopMargin = InchesToPoints(1)
    ' .BottomMargin = InchesToPoints(1.25)
    ' .LeftMargin = InchesToPoints(1)
    ' .RightMargin = InchesToPoints(1)
    ' .Gutter = InchesToPoints(0)
    ' .PageWidth = InchesToPoints(11)
    ' .PageHeight = InchesToPoints(8.5)
    ' .SectionStart = wdSectionNewPage
    '
    ' End With
    '
    ' Selection.TypeParagraph
    ' Selection.TypeParagraph
    ' Selection.MoveUp Unit:=wdLine, Count:=1
    '
    'End Sub
    '###############################################

    ' %wdOrientPortrait =0
    ' %wdOrientLandscape=1
    ' landscape=1, portrait=0

    LET vVnt = %wdSectionNewPage
    OBJECT LET oWordPageSetup.SectionStart = vVnt

    OBJECT GET oWordPageSetup.ORIENTATION TO vVnt '}these lines just in case this is needed
    SET oWordPageSetup = vVnt '}does not seem to make any difference if included or not
    LET vVnt = %wdOrientLandscape
    OBJECT LET oWordPageSetup.ORIENTATION=vVnt '<------ this does not work

    '~~~~~~~~~~~~~~~~~~ all OK below here ~~~~~~~~~~~~~~~~
    'paper set at A4
    'measurements in points- this is all OK
    LET vVnt =(72*1)
    OBJECT LET oWordPageSetup.TopMargin = vVnt
    LET vVnt =(72*1.25)
    OBJECT LET oWordPageSetup.BottomMargin = vVnt
    LET vVnt = (72*1)
    OBJECT LET oWordPageSetup.LeftMargin = vVnt
    LET vVnt = (72*1)
    OBJECT LET oWordPageSetup.RightMargin = vVnt
    LET vVnt = (72*0)
    OBJECT LET oWordPageSetup.Gutter = vVnt
    LET vVnt = (72*11.66)
    OBJECT LET oWordPageSetup.PageWidth = vVnt
    LET vVnt = (72*8.25)
    OBJECT LET oWordPageSetup.PageHeight = vVnt
    ' NOTE - margins set as if page was in landscape mode i.e. left margin > top margin

    'set font - this works ok
    OBJECT GET oWordSel.Font TO vVnt
    SET oWordFont = vVnt
    LET vVnt = 12
    OBJECT LET oWordFont.Size = vVnt
    LET vText = "Arial"
    OBJECT LET oWordFont.Name = vText

    ' Get the Interface to the currect selection of the document
    OBJECT GET oWordApp.Selection TO vVnt
    SET oWordSel = vVnt


    ' Enter some text into the document at the selection position
    LET vText = message '"PowerBASIC controls Microsoft Word!"
    OBJECT CALL oWordSel.TypeText(vText)

    ' Set a paragraph marker (end of paragraph)
    OBJECT CALL oWordSel.TypeParagraph

    ' Another paragraph
    LET vText = "Printed from Masterdrain"
    OBJECT CALL oWordSel.TypeText(vText)
    OBJECT CALL oWordSel.TypeParagraph

    ' Save the new document to disk

    result=INPUTBOX$("Enter filename to save","About to save the document...", "Output.doc") 'this is untidy
    IF RIGHT$(result,4)<>".doc" THEN result=result+".doc"
    LET vFile = result
    LET vFileFmt = %wdFormatDocument
    OBJECT CALL oWordDoc.SaveAs(vFile, vFileFmt)

    '----------------------------------------------------------------
    END IF
    ' Close the current document and then close MSWORD completely
    templong=MSGBOX ("Close MSWORD?",%MB_yesno OR %MB_TASKMODAL,"Masterdrain")
    IF templong<>%IDYES THEN
    ' Close all of the Interfaces
    SET oWordSel = NOTHING
    SET oWordDoc = NOTHING
    SET oWordApp = NOTHING
    SET oWordFont = NOTHING
    SET oWordPageSetup = NOTHING
    ELSE
    OBJECT CALL oWordApp.ActiveWindow.CLOSE
    OBJECT CALL oWordApp.Quit
    ' Close all of the Interfaces
    SET oWordSel = NOTHING
    SET oWordDoc = NOTHING
    SET oWordApp = NOTHING
    SET oWordFont = NOTHING
    SET oWordPageSetup = NOTHING
    END IF

    END FUNCTION
    '======================================================
    </code>
    “None but those who have experienced them can conceive of the enticements of science” - Mary Shelley

  • #2
    I haven't tried all your code but I added:

    LET vVnt = %wdOrientLandscape
    OBJECT LET WordApp.ActiveDocument.PageSetup.ORIENTATION=vVnt

    to one of my existing programs and it switched the page from portait to Landscape.

    Bob

    Comment


    • #3
      That did the trick - many thanks. Now all I have to do is understand what it is doing!

      Iain
      “None but those who have experienced them can conceive of the enticements of science” - Mary Shelley

      Comment

      Working...
      X