Results 1 to 9 of 9

Thread: New page in Word

  1. #1

    Thread Starter
    Registered User
    Join Date
    Jun 2005
    Posts
    87

    New page in Word

    Ok,

    My program writes address labels to a Word template from a database. Avery 5160 standard labels come 30 to a page... There will be days where we have more than 30 labels, though...

    My question is:

    How do I create a new page in the current open template in Word, but use the same bookmarks as page 1?

    VB Code:
    1. Private Sub cmdGetLabels_Click()
    2.  
    3.     Dim template As String
    4.     Dim WordApp As Word.Application
    5.     Dim WordDoc As Word.Document
    6.     Dim WordTemp As Word.Document
    7.     Dim docPath As String
    8.     Dim intTimes As Integer
    9.     Dim strLetter As String
    10.     Dim strUser As String
    11.     Dim strStatus As String
    12.     Dim docPageCount As Integer
    13.    
    14.     '--> Get template and document storage path
    15.     template$ = getTemplate
    16.  
    17.     ConnectDatabase
    18.        
    19.     '--> Start MS Word and open template
    20.     Set WordApp = CreateObject("Word.Application")
    21.     WordApp.Visible = False
    22.     'Set WordTemp = WordApp.Documents.Open("C:\Documents and Settings\" & Environ("Username") & "\Application Data\Microsoft\Templates\Normal.dot")
    23.     Set WordDoc = WordApp.Documents.Open(FileName:=template$, Revert:=True)
    24.    
    25.    
    26. intTimes = 1
    27.    
    28. With rsCustomerTable
    29.  
    30. Do Until .EOF
    31.    
    32.     Select Case intTimes
    33.    
    34.         Case "1"
    35.             strLetter = "A"
    36.         Case "2"
    37.             strLetter = "B"
    38.         Case "3"
    39.             strLetter = "C"
    40.         Case "4"
    41.             strLetter = "D"
    42.         Case "5"
    43.             strLetter = "E"
    44.         Case "6"
    45.             strLetter = "F"
    46.         Case "7"
    47.             strLetter = "G"
    48.         Case "8"
    49.             strLetter = "H"
    50.         Case "9"
    51.             strLetter = "I"
    52.         Case "10"
    53.             strLetter = "J"
    54.         Case "11"
    55.             strLetter = "K"
    56.         Case "12"
    57.             strLetter = "L"
    58.         Case "13"
    59.             strLetter = "M"
    60.         Case "14"
    61.             strLetter = "N"
    62.         Case "15"
    63.             strLetter = "O"
    64.         Case "16"
    65.             strLetter = "P"
    66.         Case "17"
    67.             strLetter = "Q"
    68.         Case "18"
    69.             strLetter = "R"
    70.         Case "19"
    71.             strLetter = "S"
    72.         Case "20"
    73.             strLetter = "T"
    74.         Case "21"
    75.             strLetter = "U"
    76.         Case "22"
    77.             strLetter = "V"
    78.         Case "23"
    79.             strLetter = "W"
    80.         Case "24"
    81.             strLetter = "X"
    82.         Case "25"
    83.             strLetter = "Y"
    84.         Case "26"
    85.             strLetter = "Z"
    86.         Case "27"
    87.             strLetter = "AA"
    88.         Case "28"
    89.             strLetter = "BB"
    90.         Case "29"
    91.             strLetter = "CC"
    92.         Case "30"
    93.             strLetter = "DD"
    94.        
    95.    End Select
    96.  
    97.            '--> Insert data in MS Word template
    98.            
    99.             If Len(rsCustomerTable!Name) <> 0 Then
    100.                 WordDoc.Bookmarks("" & strLetter & "1").Range.Text = rsCustomerTable!Name
    101.                 Debug.Print rsCustomerTable!Name
    102.             End If
    103.            
    104.             If Len(rsCustomerTable!Address) <> 0 Then
    105.             WordDoc.Bookmarks("" & strLetter & "2").Range.Text = rsCustomerTable!Address
    106.                 Debug.Print rsCustomerTable!Address
    107.             End If
    108.            
    109.             If Len(rsCustomerTable!City) <> 0 Then
    110.             WordDoc.Bookmarks("" & strLetter & "3").Range.Text = rsCustomerTable!City
    111.                 Debug.Print rsCustomerTable!City
    112.             End If
    113.            
    114.             If Len(rsCustomerTable!State) <> 0 Then
    115.             WordDoc.Bookmarks("" & strLetter & "4").Range.Text = rsCustomerTable!State
    116.                 Debug.Print rsCustomerTable!State
    117.             End If
    118.                
    119.             If Len(rsCustomerTable!Zip) <> 0 Then
    120.             WordDoc.Bookmarks("" & strLetter & "5").Range.Text = rsCustomerTable!Zip
    121.                 Debug.Print rsCustomerTable!Zip
    122.             End If
    123.            
    124.             rsCustomerTable.MoveNext
    125.            
    126.             vbalProgressBar1.Max = rsCustomerTable.RecordCount
    127.             vbalProgressBar1.Value = intTimes
    128.            
    129.             intTimes = intTimes + 1
    130.            
    131.         Loop
    132. End With
    133.  
    134.         '--> Save new document and quit MS Word
    135.        
    136.         MsgBox "There are " & rsCustomerTable.RecordCount & " labels to Print, " & strUser & "", vbOKOnly, "Label Count"
    137.        
    138.         WordDoc.SaveAs "F:\Shared\Cust Serv\Programs\LabelCreatorPlus\testLabels.doc"
    139.         'WordDoc.PrintOut
    140.         WordDoc.Close (Word.WdSaveOptions.wdDoNotSaveChanges)
    141.         WordApp.Quit
    142.  
    143.     Set WordDoc = Nothing
    144.     Set WordTemp = Nothing
    145.     Set WordApp = Nothing
    146.    
    147.     CloseDatabase
    148.    
    149.         End
    150.        
    151. End Sub

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

    Re: New page in Word

    You cant as a new page would require you to add/create new bookmarks on the new page. Think of it as a website. If you add a new page can you use links on another page on the new page to point to the new page? Nope.
    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

  3. #3

    Thread Starter
    Registered User
    Join Date
    Jun 2005
    Posts
    87

    Re: New page in Word

    Would it be best just to expand the template to accept 60 labels with preset bookmarks?

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

    Re: New page in Word

    Either expand the template (but what if they add a third page?) or write code to dynamically create new additional bookmarks, if possible, if there is text already on the new page.
    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

  5. #5
    Banned dglienna's Avatar
    Join Date
    Jun 2004
    Location
    Center of it all
    Posts
    17,901

    Re: New page in Word

    You could take the top 30 off the list for each page.

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

    Re: New page in Word

    That makes no sense as pages are not separate from the document object. I dont think you understand the question or the code. The code is in VB6 if you didnt notice and the loop populates the bookmarks across the document and not per page. If you dont have the bookmarks pre-created for additional pages then you will generate an error when trying to access a non-existant bookmark.
    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

  7. #7

    Thread Starter
    Registered User
    Join Date
    Jun 2005
    Posts
    87

    Re: New page in Word

    You're right, Rob.... This is what I've decided to do....

    If there are more than 30 records, I want to loop through until it reaches 30, SavePrintClose (Through a function). With the document closed but the App still open, I want to reopen the same template (with the same bookmarks)start the loop again, but this time start at record 31, then re-save the doc with an incrimented number....

    Here's the code:

    VB Code:
    1. Public Sub cmdGetLabels_Click()
    2.  
    3.     '--> Get template and document storage path
    4.  
    5.     template$ = getTemplate
    6.  
    7.     ConnectDatabase
    8.  
    9.     '--> Start MS Word and open template
    10.  
    11.  
    12.     OpenWordDoc 1
    13.    
    14.     intTimes = 1
    15.  
    16. With rsCustomerTable
    17.  
    18.     Do Until intTimes = .EOF
    19.        
    20.     Select Case intTimes
    21.  
    22.         Case "1"
    23.             strLetter = "A"
    24.         Case "2"
    25.             strLetter = "B"
    26.         Case "3"
    27.             strLetter = "C"
    28.         Case "4"
    29.             strLetter = "D"
    30.         Case "5"
    31.             strLetter = "E"
    32.         Case "6"
    33.             strLetter = "F"
    34.         Case "7"
    35.             strLetter = "G"
    36.         Case "8"
    37.             strLetter = "H"
    38.         Case "9"
    39.             strLetter = "I"
    40.         Case "10"
    41.             strLetter = "J"
    42.         Case "11"
    43.             strLetter = "K"
    44.         Case "12"
    45.             strLetter = "L"
    46.         Case "13"
    47.             strLetter = "M"
    48.         Case "14"
    49.             strLetter = "N"
    50.         Case "15"
    51.             strLetter = "O"
    52.         Case "16"
    53.             strLetter = "P"
    54.         Case "17"
    55.             strLetter = "Q"
    56.         Case "18"
    57.             strLetter = "R"
    58.         Case "19"
    59.             strLetter = "S"
    60.         Case "20"
    61.             strLetter = "T"
    62.         Case "21"
    63.             strLetter = "U"
    64.         Case "22"
    65.             strLetter = "V"
    66.         Case "23"
    67.             strLetter = "W"
    68.         Case "24"
    69.             strLetter = "X"
    70.         Case "25"
    71.             strLetter = "Y"
    72.         Case "26"
    73.             strLetter = "Z"
    74.         Case "27"
    75.             strLetter = "AA"
    76.         Case "28"
    77.             strLetter = "BB"
    78.         Case "29"
    79.             strLetter = "CC"
    80.         Case "30"
    81.             strLetter = "DD"
    82.    
    83.    End Select
    84.  
    85.         '--> Insert data in MS Word template
    86.  
    87.         If Len(rsCustomerTable!Name) <> 0 Then
    88.            WordDoc.Bookmarks("" & strLetter & "1").Range.Text = rsCustomerTable!Name
    89.             Debug.Print rsCustomerTable!Name
    90.         End If
    91.        
    92.         If Len(rsCustomerTable!Address) <> 0 Then
    93.         WordDoc.Bookmarks("" & strLetter & "2").Range.Text = rsCustomerTable!Address
    94.             Debug.Print rsCustomerTable!Address
    95.         End If
    96.        
    97.         If Len(rsCustomerTable!City) <> 0 Then
    98.         WordDoc.Bookmarks("" & strLetter & "3").Range.Text = rsCustomerTable!City
    99.             Debug.Print rsCustomerTable!City
    100.         End If
    101.        
    102.         If Len(rsCustomerTable!State) <> 0 Then
    103.         WordDoc.Bookmarks("" & strLetter & "4").Range.Text = rsCustomerTable!State
    104.             Debug.Print rsCustomerTable!State
    105.         End If
    106.        
    107.         If Len(rsCustomerTable!Zip) <> 0 Then
    108.         WordDoc.Bookmarks("" & strLetter & "5").Range.Text = rsCustomerTable!Zip
    109.             Debug.Print rsCustomerTable!Zip
    110.         End If
    111.        
    112.         rsCustomerTable.MoveNext
    113.        
    114.         vbalProgressBar.Max = rsCustomerTable.RecordCount
    115.         vbalProgressBar.Value = intTimes
    116.        
    117.         intTimes = intTimes + 1
    118.        
    119.         Loop
    120.        
    121.     End With
    122.        
    123.         SavePrintClose 1
    124.  
    125. [B][color=red][size=4]This is where I want to go back through the loop for records 31-60....[/size][/color][/B][color=red][size=4][/size][/color][size=4][/size]        
    126.  
    127. End Sub

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

    Re: New page in Word

    Moved from Classic VB forum.


    After this line is where you will need to cut up the recordset into chunks of 30...

    VB Code:
    1. Do Until intTimes = .EOF
    You will want to check the recordcount and if its divisible by 30 evenly then call out to a function to save the doc and reopen a new template setting the word doc object variable to it. You can use the Mod function for testing divisibility by 30.
    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

  9. #9
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: New page in Word

    I'll leave you in the capable hands of RobDog, but I just thought I'd mention that your Select Case could be made much shorter and still have the same effect:
    VB Code:
    1. Select Case intTimes
    2.         Case 1 To 26
    3.             strLetter = Chr(64 + intTimes)
    4.         Case 27 To 30
    5.             strLetter = String(2, 64 - 26 + intTimes)
    6.         End Select

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