Announcement

Collapse

Forum Guidelines

This forum is for finished source code that is working properly. If you have questions about this or any other source code, please post it in one of the Discussion Forums, not here.
See more
See less

Useful COM Word Interfaces; Page Setup, Headers/Footers, Hyperlinks, Fonts, etc

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

  • Useful COM Word Interfaces; Page Setup, Headers/Footers, Hyperlinks, Fonts, etc

    Code:
    'To make OWord.inc go to the 'Tools' menu and select 'PowerBASIC COM Browser'.
    'When the COM Browser opens go to the 'File' menu in the COM Browser and
    'choose 'Open Registered Library...'  You need to have some version of Word
    'installed on your machine to use this program.  Look for something like...
    '
    'Microsoft Word 9.0 Object Library 8.1
    '
    'If you have a different version of Word it may say Word 97 or Word 10.0 or
    'something else.  Whatever version you have after you select it the COM
    'Browser will display the COM Interfaces and Methods available in that
    'object in the two list boxes.  Next, once again on the 'File' menu choose
    'Select All Interfaces'.  Then on the 'File' menu choose "Save Interface
    'To File'.  When the 'Save File' dialog box appears save the file as
    'OWord.inc.  You can save it either in the PB Includes folder or in the
    'apps folder.  That's all there is to it. Should compile and run with either
    'Console Compiler or Windows Compiler
    
    #Compile Exe
    #Dim All
    #Include "OWord.inc"
    %FALSE = 0
    %TRUE  = 1
    
    Function PBMain () As Long
      Local vVnt,vBool,vFileName,vFileFmt,vText,vAnchor,vAddress,vAlignment As Variant
      Local oWord As WordApplication, oWordDoc As WordDocument
      Local oWordParaFmt As WordParagraphFormat
      Local oPageSetup As WordPageSetup
      Local oWordSel As WordSelection
      Local oRange As WordRange
      Local oFont As WordFont
      Register i As Long
    
      Set oWord=WordApplication In "Word.Application"
      If IsFalse IsObject(oWord) Then
         Set oWord=New WordApplication In "Word.Application"
      End If
      Object Call oWord.Documents.Add To vVnt
      Set oWordDoc=vVnt
      Object Get oWord.Selection To vVnt
      Set oWordSel=vVnt
    
      'Page Setup  ''Should Be 89 characters accross at Font = 10
      Object Get oWordDoc.PageSetup To vVnt
      Set oPageSetup = vVnt
      vVnt = 36
      Object Let oPageSetup.LeftMargin = vVnt
      Object Let oPageSetup.RightMargin = vVnt
      vVnt = 36
      Object Let oPageSetup.TopMargin = vVnt
      Object Let oPageSetup.BottomMargin = vVnt
    
      'Add page numbering in header (right aligned)
      vVnt=%wdSeekCurrentPageHeader
      Object Let oWord.ActiveWindow.ActivePane.View.SeekView=vVnt
      vAlignment=%wdAlignPageNumberRight
      vBool=%TRUE
      Object Call oWordSel.HeaderFooter.PageNumbers.Add(PageNumberAlignment=vAlignment,FirstPage=vBool)
    
      'Add footer with text and hyperlink
      vVnt=%wdSeekCurrentPageFooter                 'set active window to page footer
      Object Let oWord.ActiveWindow.ActivePane.View.SeekView=vVnt
      Object Get oWordSel.ParagraphFormat To vVnt   'get a reference to WordParagraphFormat object
      Set oWordParaFmt=vVnt                         'set reference to dimentioned object
      vVnt=%wdAlignParagraphCenter                  'equate to be fed to Paragraph Format Object
      Object Let oWordParaFmt.Alignment=vVnt        'set alignment in object
      vText="This and other prospectuses may be found on the WEB @"
      Object Call oWordSel.TypeText(vText)
      Object Call oWordSel.TypeParagraph
      Object Get oWordSel.Range To vAnchor          'One of the parameters of Hyperlinks.Add() method
      vAddress="http://www.state.pa.us/dcnr/forestry/timbersales"   'is a Selection.Range object
      Object Call oWord.ActiveDocument.Hyperlinks.Add(Anchor=vAnchor,Address=vAddress)
    
      'Set Font to Courier New and also set sizes
      vVnt=%wdSeekMainDocument
      Object Let oWord.ActiveWindow.ActivePane.View.SeekView=vVnt
      Object Get oWordSel.Font To vVnt
      Set oFont = vVnt
      Let vVnt = "Courier New"
      Object Let oFont.Name = vVnt
      Let vVnt=10.0
      Object Let oFont.Size=vVnt
      Let vBool=1
      Object Let oFont.Bold=vBool
    
      'Set text alignment, i.e., centering and so forth.
      Object Get oWordSel.ParagraphFormat To vVnt
      Set oWordParaFmt=vVnt
      vVnt=%wdAlignParagraphCenter
      Object Let oWordParaFmt.Alignment=vVnt
      vText = "Commonwealth of Pennsylvania"
      Object Call oWordSel.TypeText(vText)
      Object Call oWordSel.TypeParagraph
      vText = "Department of Conservation and Natural Resources"
      Object Call oWordSel.TypeText(vText)
      Object Call oWordSel.TypeParagraph
      Let vVnt=12.0
      Object Let oFont.Size=vVnt
      vText="Bureau of Forestry"
      Object Call oWordSel.TypeText(vText)
      Object Call oWordSel.TypeParagraph
      Object Call oWordSel.TypeParagraph
      Let vVnt=13.5
      Object Let oFont.Size=vVnt
      vText="PROSPECTUS"
      Object Call oWordSel.TypeText(vText)
      Object Call oWordSel.TypeParagraph
      Object Call oWordSel.TypeParagraph
      Object Call oWordSel.TypeParagraph
      Object Call oWordSel.TypeParagraph
    
      'Display bunch of text at different fonts
      vVnt=%wdAlignParagraphLeft
      Object Let oWordParaFmt.Alignment=vVnt
      For i=6 To 30
        Let vVnt=CSng(i)
        Object Let oFont.Size=vVnt
        Let vText = "Text With Font Size = " & Str$(i)
        Object Call oWordSel.TypeText(vText)
        Object Call oWordSel.TypeParagraph
      Next i
    
      'Save to file
      vFileName = CurDir$ & "\Junk.doc"
      Let vFileFmt=%wdFormatDocument
      Object Call oWordDoc.SaveAs(vFileName,vFileFmt)
      Let vBool=%TRUE
      Object Let oWord.Visible=vBool
      Set oPageSetup=Nothing
      Set oFont=Nothing
      Set oWordSel=Nothing
      Set oWordParaFmt=Nothing
      Set oWordDoc=Nothing
      Set oWord=Nothing
    
      PBMain=0
    End Function
    Fred
    "fharris"+Chr$(64)+"evenlink"+Chr$(46)+"com"

  • #2
    Some More Info And Updates For PB 50 and PB 90

    Some time after I posted the above code I found out that the program will fail if the last time Word was opened it was left in 'Normal View'. To do some of this fancy stuff with footers, hyperlinks and such one must first toggle Word into 'Print View'. Below is the updated program for PB 4 and PB 8.

    Code:
    'To make OWord.inc go to the 'Tools' menu and select 'PowerBASIC COM Browser'.
    'When the COM Browser opens go to the 'File' menu in the COM Browser and
    'choose 'Open Registered Library...'  You need to have some version of Word
    'installed on your machine to use this program.  Look for something like...
    '
    'Microsoft Word 9.0 Object Library 8.1
    '
    'If you have a different version of Word it may say Word 97 or Word 10.0 or
    'something else.  Whatever version you have after you select it the COM
    'Browser will display the COM Interfaces and Methods available in that
    'object in the two list boxes.  Next, once again on the 'File' menu choose
    'Select All Interfaces'.  Then on the 'File' menu choose "Save Interface
    'To File'.  When the 'Save File' dialog box appears save the file as
    'OWord.inc.  You can save it either in the PB Includes folder or in the
    'apps folder.  That's all there is to it. Should compile and run with either
    'Console Compiler or Windows Compiler
    
    'Use PB Win 80
    #Compile Exe
    #Dim All
    #Include "OWord.inc"
    %FALSE = 0
    %TRUE  = 1
    
    Function PBMain () As Long
      Local vVnt,vBool,vFileName,vFileFmt,vText,vAnchor,vAddress,vAlignment As Variant
      Local oWord As WordApplication, oWordDoc As WordDocument
      Local oWordParaFmt As WordParagraphFormat
      Local oPageSetup As WordPageSetup
      Local oWordSel As WordSelection
      Local oRange As WordRange
      Local oFont As WordFont
      Register i As Long
    
      Set oWord=WordApplication In "Word.Application"
      If IsFalse IsObject(oWord) Then
         Set oWord=New WordApplication In "Word.Application"
      End If
      Object Call oWord.Documents.Add To vVnt
      Set oWordDoc=vVnt
      Object Get oWord.Selection To vVnt
      Set oWordSel=vVnt
    
      'Page Setup  ''Should Be 89 characters accross at Font = 10
      Object Get oWordDoc.PageSetup To vVnt
      Set oPageSetup = vVnt
      vVnt = 36
      Object Let oPageSetup.LeftMargin = vVnt
      Object Let oPageSetup.RightMargin = vVnt
      vVnt = 36
      Object Let oPageSetup.TopMargin = vVnt
      Object Let oPageSetup.BottomMargin = vVnt
    
    
      'Add page numbering in header (right aligned)
      vVnt=%wdSeekCurrentPageHeader
      Object Let oWord.ActiveWindow.ActivePane.View.SeekView=vVnt
      vAlignment=%wdAlignPageNumberRight
      vBool=%TRUE
      Object Call oWordSel.HeaderFooter.PageNumbers.Add(PageNumberAlignment=vAlignment,FirstPage=vBool)
    
      'Add footer with text and hyperlink
      vVnt=%wdPrintView   'WOW!  Failing to have these two lines here really screws things up if the last time Word was used it
      'vVnt=%wdPageView   'this appears to be old
      Object Let oWord.ActiveWindow.ActivePane.View.Type=vVnt  'was left in 'Normal' view.  All the header/footer stuff fails!
      vVnt=%wdSeekCurrentPageFooter                 'set active window to page footer
      Object Let oWord.ActiveWindow.ActivePane.View.SeekView=vVnt
      Object Get oWordSel.ParagraphFormat To vVnt   'get a reference to WordParagraphFormat object
      Set oWordParaFmt=vVnt                         'set reference to dimentioned object
      vVnt=%wdAlignParagraphCenter                  'equate to be fed to Paragraph Format Object
      Object Let oWordParaFmt.Alignment=vVnt        'set alignment in object
      vText="This and other prospectuses may be found on the WEB @"
      Object Call oWordSel.TypeText(vText)
      Object Call oWordSel.TypeParagraph
      Object Get oWordSel.Range To vAnchor          'One of the parameters of Hyperlinks.Add() method
      vAddress="http://www.state.pa.us/dcnr/forestry/timbersales"   'is a Selection.Range object
      Object Call oWord.ActiveDocument.Hyperlinks.Add(Anchor=vAnchor,Address=vAddress)
    
      'Set Font to Courier New and also set sizes
      vVnt=%wdSeekMainDocument
      Object Let oWord.ActiveWindow.ActivePane.View.SeekView=vVnt
      Object Get oWordSel.Font To vVnt
      Set oFont = vVnt
      Let vVnt = "Courier New"
      Object Let oFont.Name = vVnt
      Let vVnt=10.0
      Object Let oFont.Size=vVnt
      Let vBool=1
      Object Let oFont.Bold=vBool
    
      'Set text alignment, i.e., centering and so forth.
      Object Get oWordSel.ParagraphFormat To vVnt
      Set oWordParaFmt=vVnt
      vVnt=%wdAlignParagraphCenter
      Object Let oWordParaFmt.Alignment=vVnt
      vText = "Commonwealth of Pennsylvania"
      Object Call oWordSel.TypeText(vText)
      Object Call oWordSel.TypeParagraph
      vText = "Department of Conservation and Natural Resources"
      Object Call oWordSel.TypeText(vText)
      Object Call oWordSel.TypeParagraph
      Let vVnt=12.0
      Object Let oFont.Size=vVnt
      vText="Bureau of Forestry"
      Object Call oWordSel.TypeText(vText)
      Object Call oWordSel.TypeParagraph
      Object Call oWordSel.TypeParagraph
      Let vVnt=13.5
      Object Let oFont.Size=vVnt
      vText="PROSPECTUS"
      Object Call oWordSel.TypeText(vText)
      Object Call oWordSel.TypeParagraph
      Object Call oWordSel.TypeParagraph
      Object Call oWordSel.TypeParagraph
      Object Call oWordSel.TypeParagraph
    
      'Display bunch of text at different fonts
      vVnt=%wdAlignParagraphLeft
      Object Let oWordParaFmt.Alignment=vVnt
      For i=6 To 30
        Let vVnt=CSng(i)
        Object Let oFont.Size=vVnt
        Let vText = "Text With Font Size = " & Str$(i)
        Object Call oWordSel.TypeText(vText)
        Object Call oWordSel.TypeParagraph
      Next i
    
      'Save to file
      vFileName = CurDir$ & "\Junk.doc"
      Let vFileFmt=%wdFormatDocument
      Object Call oWordDoc.SaveAs(vFileName,vFileFmt)
      Let vBool=%TRUE
      Object Let oWord.Visible=vBool
      Set oPageSetup=Nothing
      Set oFont=Nothing
      Set oWordSel=Nothing
      Set oWordParaFmt=Nothing
      Set oWordDoc=Nothing
      Set oWord=Nothing
    
      PBMain=0
    End Function
    Fred
    "fharris"+Chr$(64)+"evenlink"+Chr$(46)+"com"

    Comment


    • #3
      ...and here is the PB 50 / PB 90 compatible version

      Code:
      'To create PBWord.inc using the PowerBASIC COM Browser go to the
      'Tools menu and select PowerBASIC COM Browser.  Scroll through the
      'list of Type Library information until you locate something like
      'Microsoft Word 9.0 Object Library 8.1.  Depending on which version
      'of Word you have installed, your numbers may be different.  You
      'need to have some version of Microsoft Word installed to run this
      'program.  Next, go up to the Tools menu in the COM Browser and
      'select Options.  This will bring up a small dialog box where you
      'will need to fill in some stuff.  Make sure the radio button
      ''Always Use An Interface Prefix' is checked.  In the Interface
      'Prefix text box enter 'Word' (without the single quotes).  Then
      'finally check the check box entitles 'Generate Dispatch Interfaces
      'Only'.  You are finally done with this dialog box so close it
      'and return to the main COM Browser screen where you should have
      'the Microsoft Word Object Library highlighted.  Double click on
      'this library and the COM Browser will momentarily present you
      'with the generated Word, program ids, equates, and interface
      'declares.  Save this entire file in the right pane as PBWord.inc
      'and place it in the same folder as this program.
      #Compile Exe
      #Dim All                'always do this
      #Include "PBWord.inc"   'this is generated from the pb com browser as described above
      %FALSE = 0
      %TRUE  = 1
      
      Function PBMain () As Long
        Local vVnt,vBool,vFileName,vFileFmt,vText,vAnchor,vAddress,vAlignment As Variant
        Local oWord As Word_Application, oWordDoc As Word_Document
        Local oWordParaFmt As Word_ParagraphFormat
        Local oWordHyperLink As WordHyperlink
        Local oPageSetup As WordPageSetup
        Local oWordActiveDoc As IDispatch
        Local oWordSel As WordSelection
        Local oRange As WordRange
        Local oFont As Word_Font
        Register i As Long
      
        oWord = Anycom $PROGID_WordApplication
        If IsObject(oWord) Then
           Object Call oWord.Documents.Add To vVnt
           Set oWordDoc=vVnt
           Object Get oWord.Selection To vVnt
           Set oWordSel=vVnt
      
           'Page Setup  ''Should Be 89 characters accross at Font = 10
           Object Get oWordDoc.PageSetup To vVnt
           Set oPageSetup = vVnt
           vVnt = 36
           Object Let oPageSetup.LeftMargin = vVnt
           Object Let oPageSetup.RightMargin = vVnt
           vVnt = 36
           Object Let oPageSetup.TopMargin = vVnt
           Object Let oPageSetup.BottomMargin = vVnt
           'if left in 'Normal' view, all the header/footer stuff fails!
           'vVnt=%wdPageView   'this appears to be old
           vVnt=%wdPrintView
           Object Let oWord.ActiveWindow.ActivePane.View.Type=vVnt
      
           vVnt=%wdSeekCurrentPageHeader
           Object Let oWord.ActiveWindow.ActivePane.View.SeekView=vVnt
           vAlignment=%wdAlignPageNumberRight
           vBool=%TRUE
           Object Call oWordSel.HeaderFooter.PageNumbers.Add(PageNumberAlignment=vAlignment,FirstPage=vBool)
           vVnt=%wdSeekCurrentPageFooter                 'set active window to page footer
           Object Let oWord.ActiveWindow.ActivePane.View.SeekView=vVnt
           Object Get oWordSel.ParagraphFormat To vVnt   'get a reference to WordParagraphFormat object
           Set oWordParaFmt=vVnt                         'set reference to dimentioned object
           vVnt=%wdAlignParagraphCenter                  'equate to be fed to Paragraph Format Object
           Object Let oWordParaFmt.Alignment=vVnt        'set alignment in object
           vText="This and other prospectuses may be found on the WEB @"
           Object Call oWordSel.TypeText(vText)
           Object Call oWordSel.TypeParagraph
           Object Get oWordSel.Range To vAnchor          'One of the parameters of Hyperlinks.Add() method
           vAddress="http://www.state.pa.us/dcnr/forestry/timbersales"   'is a Selection.Range object
           Object Get oWord.ActiveDocument To vVnt
           Set oWordActiveDoc=vVnt
           If IsObject(oWordActiveDoc) Then
              'Print "oWordActiveDoc Is An Object!"
              Object Get oWordSel.Range To vAnchor          'One of the parameters of Hyperlinks.Add() method
              vAddress="http://www.state.pa.us/dcnr/forestry/timbersales"   'is a Selection.Range object
              Object Call oWordActiveDoc.Hyperlinks.Add(Anchor=vAnchor,Address=vAddress) To vVnt
              Set oWordHyperlink=vVnt
              If IsObject(oWordHyperlink) Then
                 'Print "oWordHyperlink Is An Object!"
                 Set oWordHyperlink=Nothing
              End If
              Set oWordActiveDoc=Nothing
           End If
           vVnt=%wdSeekMainDocument
           Object Let oWord.ActiveWindow.ActivePane.View.SeekView=vVnt
           Object Get oWordSel.Font To vVnt
           Set oFont = vVnt
           Let vVnt = "Courier New"
           Object Let oFont.Name = vVnt
           Let vVnt=10.0
           Object Let oFont.Size=vVnt
           Let vBool=1
           Object Let oFont.Bold=vBool
      
           Object Get oWordSel.ParagraphFormat To vVnt
           Set oWordParaFmt=vVnt
           vVnt=%wdAlignParagraphCenter
           Object Let oWordParaFmt.Alignment=vVnt
           vText = "Commonwealth of Pennsylvania"
           Object Call oWordSel.TypeText(vText)
           Object Call oWordSel.TypeParagraph
           vText = "Department of Conservation and Natural Resources"
           Object Call oWordSel.TypeText(vText)
           Object Call oWordSel.TypeParagraph
           Let vVnt=12.0
           Object Let oFont.Size=vVnt
           vText="Bureau of Forestry"
           Object Call oWordSel.TypeText(vText)
           Object Call oWordSel.TypeParagraph
           Object Call oWordSel.TypeParagraph
           Let vVnt=13.5
           Object Let oFont.Size=vVnt
           vText="PROSPECTUS"
           Object Call oWordSel.TypeText(vText)
           Object Call oWordSel.TypeParagraph
           Object Call oWordSel.TypeParagraph
           Object Call oWordSel.TypeParagraph
           Object Call oWordSel.TypeParagraph
      
           vVnt=%wdAlignParagraphLeft
           Object Let oWordParaFmt.Alignment=vVnt
           For i=6 To 30
             Let vVnt=CSng(i)
             Object Let oFont.Size=vVnt
             Let vText = "Text With Font Size = " & Str$(i)
             Object Call oWordSel.TypeText(vText)
             Object Call oWordSel.TypeParagraph
           Next i
           vFileName = Curdir$ & "\Junk.doc"
           Let vFileFmt=%wdFormatDocument
           Object Call oWordDoc.SaveAs(vFileName,vFileFmt)
           Let vBool=%TRUE
           Object Let oWord.Visible=vBool
           Set oWordSel=Nothing
           Set oWordParaFmt=Nothing
           Set oWordDoc=Nothing
           Set oWord=Nothing
        End If
        
        PBMain=0
      End Function
      Fred
      "fharris"+Chr$(64)+"evenlink"+Chr$(46)+"com"

      Comment

      Working...
      X