Results 1 to 4 of 4

Thread: Opening MS WOrd from a VB program

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Feb 2006
    Posts
    81

    Opening MS WOrd from a VB program

    Hi,
    When I click a button I have a MS Word doc to open.
    The first time when the program runs, I click, it just does some process but does not open the doc, but on the next clicks it works.

    How to make the Word doc, open when I click the first time.

  2. #2
    Shared Member
    Join Date
    May 2005
    Location
    Kashmir, India
    Posts
    2,277

    Re: Opening MS WOrd from a VB program

    Where is the code that you are using?
    Use [code] source code here[/code] tags when you post source code.

    My Articles

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Feb 2006
    Posts
    81

    Re: Opening MS WOrd from a VB program

    VB Code:
    1. Private Sub printpo_Click()
    2. Dim i, j, n, s, r, c
    3. Dim count1, qid
    4. Dim response As Integer
    5. Dim msg, select_statement
    6. Dim myrange
    7. Dim oPara1 As Word.Paragraph, oPara2 As Word.Paragraph
    8.    
    9.  
    10. 'Writing to word doc for Purchase Order
    11.  
    12.     Set oApp = New Word.Application
    13.     Dim oTable As Word.Table
    14.     Set oDoc = oApp.Documents.Open("C:\Pax Spices\Order Entry\PO.doc")
    15.     oDoc.Select
    16.     'oApp.ActiveDocument.Paragraphs(2).Range.Start
    17.    
    18.  
    19.     oApp.Selection.Font.Name = "Times New Roman"
    20.     oApp.Selection.Font.Size = "24"
    21.     oApp.Selection.Font.Color = wdColorBlue
    22.        
    23.         oApp.Selection.TypeParagraph
    24.     oApp.Selection.TypeParagraph
    25.     oApp.Selection.Font.Name = "Times New Roman"
    26.     oApp.Selection.Font.Size = "12"
    27.     oApp.Selection.Font.Color = wdColorBlack
    28.    
    29.     oApp.Selection.ParagraphFormat.Alignment = wdAlignParagraphCenter
    30.     oApp.Selection.Font.Bold = wdToggle
    31.     oApp.Selection.Font.Underline = wdUnderlineSingle
    32.     oApp.Selection.TypeText Text:="Purchase Order"
    33.     oApp.Selection.Font.Underline = wdUnderlineNone
    34.     oApp.Selection.Font.Bold = wdToggle
    35.     oApp.Selection.TypeParagraph
    36.     oApp.Selection.ParagraphFormat.Alignment = wdAlignParagraphLeft
    37.     oApp.Selection.Font.Bold = wdToggle
    38.     oApp.Selection.TypeText Text:="Purchase Order#:  "
    39.     oApp.Selection.Font.Bold = wdToggle
    40.     oApp.Selection.TypeText Text:=pono.Text
    41.     oApp.Selection.TypeText Text:=vbTab & vbTab & vbTab
    42.     oApp.Selection.Font.Bold = wdToggle
    43.     oApp.Selection.TypeText Text:="Date of Purchase Order  "
    44.     oApp.Selection.Font.Bold = wdToggle
    45.     oApp.Selection.TypeText Text:=podate.Text
    46.     oApp.Selection.TypeParagraph
    47.     oApp.Selection.TypeParagraph
    48.     oApp.Selection.ParagraphFormat.Alignment = wdAlignParagraphLeft
    49.     oApp.Selection.Font.Bold = wdToggle
    50.     oApp.Selection.TypeText Text:="Supplier Name:  "
    51.     oApp.Selection.Font.Bold = wdToggle
    52.      oApp.Selection.TypeParagraph
    53.     oApp.Selection.TypeText Text:=suppname.Text
    54.    
    55.     select_statement = "select * from supplier where suppname='" & suppname.Text & "' "
    56.  
    57.     qry (select_statement)
    58.    oApp.Selection.TypeParagraph
    59.    oApp.Selection.TypeText Text:="" & rs!Address
    60.    oApp.Selection.TypeParagraph
    61.    oApp.Selection.TypeText Text:="" & rs!City
    62.    oApp.Selection.TypeParagraph
    63.    oApp.Selection.TypeText Text:="" & rs!state
    64.    oApp.Selection.TypeParagraph
    65.   oApp.Selection.TypeText Text:="" & rs!zip
    66.   oApp.Selection.TypeParagraph
    67.    oApp.Selection.TypeText Text:="" & rs!country
    68.    oApp.Selection.TypeParagraph
    69.    
    70.    
    71.    oApp.Selection.ParagraphFormat.Alignment = wdAlignParagraphLeft
    72.    
    73.     oApp.Selection.TypeParagraph
    74.    oApp.Selection.Font.Bold = wdToggle
    75.        oApp.Selection.TypeText Text:="Bill To: "
    76.        oApp.Selection.TypeText Text:=vbTab & vbTab & vbTab & vbTab & vbTab & vbTab & vbTab
    77.        oApp.Selection.TypeText Text:="Ship To: "
    78.        oApp.Selection.TypeParagraph
    79.        oApp.Selection.Font.Bold = wdToggle
    80.        
    81.         oApp.Selection.TypeText Text:=billtoname.Text
    82.         'oApp.Selection.TypeText Text:="                                                 "
    83.         oApp.Selection.TypeText Text:=vbTab & vbTab & vbTab & vbTab & vbTab
    84.         oApp.Selection.TypeText Text:=custname.Text
    85.         oApp.Selection.TypeParagraph
    86.         oApp.Selection.TypeText Text:=billtoadd.Text
    87.         'oApp.Selection.TypeText Text:="                                                "
    88.         oApp.Selection.TypeText Text:=vbTab & vbTab & vbTab & vbTab & vbTab & vbTab
    89.         oApp.Selection.TypeText Text:=shiptoadd.Text
    90.         oApp.Selection.TypeParagraph
    91.        
    92.         oApp.Selection.TypeText Text:=billtocity.Text
    93.        ' oApp.Selection.TypeText Text:="                                                   "
    94.         oApp.Selection.TypeText Text:=vbTab & vbTab & vbTab & vbTab & vbTab & vbTab & vbTab
    95.         oApp.Selection.TypeText Text:=city1.Text
    96.         oApp.Selection.TypeParagraph
    97.        
    98.         oApp.Selection.TypeText Text:=billtostate.Text
    99.         'oApp.Selection.TypeText Text:="                                                   "
    100.         oApp.Selection.TypeText Text:=vbTab & vbTab & vbTab & vbTab & vbTab & vbTab & vbTab
    101.         oApp.Selection.TypeText Text:=state.Text
    102.         oApp.Selection.TypeParagraph
    103.        
    104.        
    105.         oApp.Selection.TypeText Text:=billtocountry.Text
    106.         'oApp.Selection.TypeText Text:="                                                   "
    107.         oApp.Selection.TypeText Text:=vbTab & vbTab & vbTab & vbTab & vbTab & vbTab & vbTab
    108.         oApp.Selection.TypeText Text:=country.Text
    109.         oApp.Selection.TypeParagraph
    110.        
    111.          oApp.Selection.Font.Bold = wdToggle
    112.         oApp.Selection.TypeText Text:="Phone "
    113.        
    114.         oApp.Selection.Font.Bold = wdToggle
    115.        
    116.         oApp.Selection.TypeText Text:=billtoph.Text
    117.         'oApp.Selection.TypeText Text:="                                                   "
    118.         oApp.Selection.TypeText Text:=vbTab & vbTab & vbTab & vbTab & vbTab & vbTab
    119.         oApp.Selection.TypeText Text:=phone.Text
    120.        
    121.         oApp.Selection.TypeParagraph
    122.          oApp.Selection.TypeParagraph
    123.     oApp.Selection.ParagraphFormat.Alignment = wdAlignParagraphLeft
    124.     oApp.Selection.Font.Bold = wdToggle
    125.     oApp.Selection.Font.Underline = wdUnderlineSingle
    126.     oApp.Selection.TypeText Text:="REQ BY"
    127.     oApp.Selection.Font.Underline = wdUnderlineNone
    128.     oApp.Selection.TypeText Text:=vbTab
    129.     oApp.Selection.Font.Underline = wdUnderlineSingle
    130.     oApp.Selection.TypeText Text:="SHIP WHEN"
    131.     oApp.Selection.Font.Underline = wdUnderlineNone
    132.     oApp.Selection.TypeText Text:=vbTab & vbTab
    133.     oApp.Selection.Font.Underline = wdUnderlineSingle
    134.     oApp.Selection.TypeText Text:="SHIP VIA "
    135.     oApp.Selection.Font.Underline = wdUnderlineNone
    136.     oApp.Selection.TypeText Text:=vbTab
    137.     oApp.Selection.Font.Underline = wdUnderlineSingle
    138.     oApp.Selection.TypeText Text:="PICK UP"
    139.     oApp.Selection.Font.Underline = wdUnderlineNone
    140.     oApp.Selection.TypeText Text:=vbTab
    141.     oApp.Selection.Font.Underline = wdUnderlineNone
    142.     oApp.Selection.Font.Underline = wdUnderlineSingle
    143.     oApp.Selection.TypeText Text:="TERMS "
    144.     oApp.Selection.Font.Underline = wdUnderlineNone
    145.     oApp.Selection.TypeText Text:=vbTab
    146.     oApp.Selection.Font.Underline = wdUnderlineNone
    147.     oApp.Selection.Font.Underline = wdUnderlineSingle
    148.     oApp.Selection.TypeText Text:="FOB"
    149.     oApp.Selection.Font.Underline = wdUnderlineNone
    150.     oApp.Selection.TypeText Text:=vbTab
    151.     oApp.Selection.Font.Bold = wdToggle
    152.     oApp.Selection.TypeParagraph
    153.     oApp.Selection.ParagraphFormat.Alignment = wdAlignParagraphLeft
    154.  
    155.    
    156.     oApp.Selection.TypeText Text:=reqby.Text
    157.     oApp.Selection.TypeText Text:=vbTab & vbTab
    158.     oApp.Selection.TypeText Text:=shipwhen.Text
    159.     oApp.Selection.TypeText Text:=vbTab & vbTab & vbTab
    160.     oApp.Selection.TypeText Text:=shipvia.Text
    161.     oApp.Selection.TypeText Text:=vbTab
    162.     oApp.Selection.TypeText Text:=pickupdate.Text
    163.     oApp.Selection.TypeText Text:=vbTab
    164.      oApp.Selection.TypeText Text:=terms.Text
    165.     oApp.Selection.TypeText Text:=vbTab
    166.      oApp.Selection.TypeText Text:=fob.Text
    167.    oApp.Selection.TypeParagraph
    168.         oApp.Selection.TypeParagraph
    169.         oApp.Selection.TypeParagraph
    170.         oApp.Selection.Font.Bold = wdToggle
    171.         oApp.Selection.TypeText Text:="Remarks "
    172.         oApp.Selection.TypeParagraph
    173.           oApp.Selection.Font.Bold = wdToggle
    174.         oApp.Selection.TypeText Text:=remarks.Text
    175.         oApp.Selection.TypeParagraph
    176.         oApp.Selection.TypeParagraph
    177.        
    178.     oApp.Selection.ParagraphFormat.Alignment = wdAlignParagraphLeft
    179.     oApp.Selection.Font.Bold = wdToggle
    180.    
    181.     oApp.Selection.TypeText Text:="Item"
    182.    
    183.     oApp.Selection.TypeText Text:=vbTab & vbTab & vbTab
    184.    
    185.     oApp.Selection.TypeText Text:="Description"
    186.    
    187.     oApp.Selection.TypeText Text:=vbTab & vbTab
    188.    
    189.     oApp.Selection.TypeText Text:="Quantity"
    190.    
    191.      oApp.Selection.TypeText Text:=vbTab & vbTab
    192.    
    193.     oApp.Selection.TypeText Text:="Rate/lb "
    194.    
    195.     oApp.Selection.TypeText Text:=vbTab
    196.  
    197.  
    198.     oApp.Selection.TypeText Text:="Total"
    199.    
    200.     oApp.Selection.TypeText Text:=vbTab
    201.  
    202.     oApp.Selection.Font.Bold = wdToggle
    203.  
    204.          
    205.        
    206.        
    207.     select_statement = "select itemno,proddesc,qty,ratelb,total from podetails where pono='" & pono.Text & "';"
    208.         qry (select_statement)
    209.         count1 = rs.RecordCount
    210.      If count1 <> 0 Then
    211.     Set myrange = oApp.ActiveDocument.Range(Start:=oApp.Selection.Start, End:=oApp.Selection.End)
    212.    
    213.          Set oTable = oDoc.Tables.add(myrange, count1, 5)
    214.         oTable.Range.ParagraphFormat.SpaceAfter = 6
    215.      
    216.  
    217. End Sub

  4. #4
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: Opening MS WOrd from a VB program

    oApp.Visible = True.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width