Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 08-03-2019, 02:21 AM
SamDsouza SamDsouza is offline syntax for inserting blank line before inserting table and after a line or paragraph Windows 10 syntax for inserting blank line before inserting table and after a line or paragraph Office 2013
Advanced Beginner
syntax for inserting blank line before inserting table and after a line or paragraph
 
Join Date: Aug 2019
Posts: 71
SamDsouza is on a distinguished road
Default syntax for inserting blank line before inserting table and after a line or paragraph

Hi Guys

As this is my First post for VBA for word



Just wanted to have syntax for inserting blank line after a line/or paragraph typed
and also to insert a blank line before table is inserted and after line typed or paragraph

Code:
Private Sub InsertingBlankLine()
Dim objWord As Word.Application
Dim txtword As String, sh As Worksheet
Dim objDoc As Document
Dim objRange As Range
Dim objTable As Table
Dim intRows As Integer
Dim intCols As Integer


txtWord = "fsdhfkhfkdhfdskfhd fkdshfdkfhdskfhdsfd fdshgfdjdsjfg"
    Set objWord = CreateObject("Word.Application")
    Set objDoc = objWord.Documents.Add
    objWord.Visible = True
    objWord.ActiveDocument.Content.FormattedText.Text = txtWord
 Set objRange = objDoc.Range

    Set objRange = objDoc.Content
    
    objRange.Collapse Direction:=wdCollapseEnd

'How to insert blank line here

    intRows = 8: intCols = 5
    objDoc.Tables.Add objRange, intRows, intCols

    Set objTable = objDoc.Tables(1)
    objTable.Borders.Enable = True

    Set objWord = Nothing
End Sub
Thanks and regards
SamD
Reply With Quote
  #2  
Old 08-03-2019, 03:03 AM
Guessed's Avatar
Guessed Guessed is offline syntax for inserting blank line before inserting table and after a line or paragraph Windows 10 syntax for inserting blank line before inserting table and after a line or paragraph Office 2016
Expert
 
Join Date: Mar 2010
Location: Canberra/Melbourne Australia
Posts: 3,977
Guessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant future
Default

Your code can be a lot simpler. If you are running the code from Word, there is no need to create the application.
You could use Range.InsertParagraphAfter but it is just as easy to include a paragraph mark with the text you are inserting.
Code:
Private Sub InsertingBlankLine()
Dim objWord As Word.Application
Dim txtword As String
Dim objDoc As Document
Dim objRange As Range
Dim objTable As Table
Dim intRows As Integer
Dim intCols As Integer

    txtWord = "fsdhfkhfkdhfdskfhd fkdshfdkfhdskfhdsfd fdshgfdjdsjfg" & vbCr
    Set objWord = CreateObject("Word.Application")
    objWord.Visible = True
    Set objDoc = objWord.Documents.Add
    objDoc.Range.Text = txtWord
    Set objRange = objDoc.Range    
    objRange.Collapse Direction:=wdCollapseEnd
    intRows = 8: intCols = 5
    Set objTable = objDoc.Tables.Add(objRange, intRows, intCols)
    objTable.Borders.Enable = True
End Sub
Also not sure why you remove the objWord but don't save or close the Word doc, nor remove the other objects you created along the way.
__________________
Andrew Lockton
Chrysalis Design, Melbourne Australia
Reply With Quote
  #3  
Old 08-03-2019, 05:20 AM
gmayor's Avatar
gmayor gmayor is offline syntax for inserting blank line before inserting table and after a line or paragraph Windows 10 syntax for inserting blank line before inserting table and after a line or paragraph Office 2016
Expert
 
Join Date: Aug 2014
Posts: 4,105
gmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud of
Default

I guess from the reference to 'sh' that you are running this code from Excel, in which case

Code:
Private Sub InsertingBlankLine()
Dim objWord As Object
Dim txtword As String, sh As Worksheet
Dim objDoc As Object
Dim objRange As Object
Dim objTable As Object
Dim intRows As Integer
Dim intCols As Integer


    txtword = "fsdhfkhfkdhfdskfhd fkdshfdkfhdskfhdsfd fdshgfdjdsjfg"
    On Error Resume Next
    Set objWord = GetObject(, "Word.Application")
    If Err Then
        Set objWord = CreateObject("Word.Application")
    End If
    On Error GoTo 0

    Set objDoc = objWord.Documents.Add
    objWord.Visible = True
    Set objRange = objDoc.Range
    With objRange
        .Text = txtword & vbCr 'add the paragraph break at the end of the text string
        .Collapse Direction:=0
    End With
    intRows = 8: intCols = 5
    Set objTable = objDoc.Tables.Add(objRange, intRows, intCols)
    objTable.Borders.Enable = True
    Set objWord = Nothing
    Set objDoc = Nothing
    Set objRange = Nothing
    Set objTable = Nothing
End Sub
__________________
Graham Mayor - MS MVP (Word) (2002-2019)
Visit my web site for more programming tips and ready made processes www.gmayor.com
Reply With Quote
  #4  
Old 08-03-2019, 08:22 AM
gmaxey gmaxey is offline syntax for inserting blank line before inserting table and after a line or paragraph Windows 10 syntax for inserting blank line before inserting table and after a line or paragraph Office 2016
Expert
 
Join Date: May 2010
Location: Brasstown, NC
Posts: 1,429
gmaxey is a jewel in the roughgmaxey is a jewel in the roughgmaxey is a jewel in the roughgmaxey is a jewel in the rough
Default

Alternatively:

Code:
Private Sub InsertingBlankLine()
Dim oWord As Word.Application
Dim oDoc As Document
Dim oRng As Range
Dim oTbl As Table
  Set oWord = CreateObject("Word.Application")
  oWord.Visible = True
  Set oDoc = oWord.Documents.Add
  Set oTbl = oDoc.Tables.Add(oDoc.Range, 8, 5)
  Set oRng = oTbl.Range
  oTbl.Borders.Enable = True
  oRng.Collapse wdCollapseStart
  oRng.InsertBreak Type:=wdColumnBreak
  oRng.Text = "fsdhfkhfkdhfdskfhd fkdshfdkfhdskfhdsfd fdshgfdjdsjfg"
End Sub
__________________
Greg Maxey
Please visit my web site at http://www.gregmaxey.com/
Reply With Quote
  #5  
Old 08-03-2019, 10:20 AM
SamDsouza SamDsouza is offline syntax for inserting blank line before inserting table and after a line or paragraph Windows 10 syntax for inserting blank line before inserting table and after a line or paragraph Office 2013
Advanced Beginner
syntax for inserting blank line before inserting table and after a line or paragraph
 
Join Date: Aug 2019
Posts: 71
SamDsouza is on a distinguished road
Default

Thanks Andrew Lockton (Guessed) . You realy made me my first .Docm file with your coding. Indeed it strated off with VBA word
the begining was perfect

Thanks to you Gmayor
Yes i started from Excel.

It is just what i wanted but then i thought i could move on but got stuck when i wanted to insert again new line after the table
As this got me more intrested added and modified with your code in RED

Code:
Private Sub InsertingBlankLine()
Dim objWord As Object
Dim txtword As String, sh As Worksheet
Dim objDoc As Object
Dim objRange As Object
Dim objTable As Object
Dim intRows As Integer
Dim intCols As Integer

    txtword = "fsdhfkhfkdhfdskfhd fkdshfdkfhdskfhdsfd fdshgfdjdsjfg"
    On Error Resume Next
    Set objWord = GetObject(, "Word.Application")
    If Err Then
        Set objWord = CreateObject("Word.Application")
    End If
    On Error GoTo 0

    Set objDoc = objWord.Documents.Add
    objWord.Visible = True
    Set objRange = objDoc.Range
    With objRange
        .Text = txtword & vbCr 'add the paragraph break at the end of the text string
        .Collapse Direction:=0
    End With
    intRows = 8: intCols = 5
    Set objTable = objDoc.Tables.Add(objRange, intRows, intCols)
    '''''''objTable.Borders.Enable = True

    With objTable
    .Borders.Enable = True
    .Cell(1, 1).Range.Text = "row 1"
    .Cell(1, 1).Range.Bold = True
    .Cell(2, 1).Range.Text = "row 2"
    .Cell(3, 1).Range.Text = "row 3"
    .Cell(4, 1).Range.Text = "row 4"
    .Cell(5, 1).Range.Text = "row 5"
    .Cell(6, 1).Range.Text = "row 6"
    .Cell(7, 1).Range.Text = "row 7"
    .Cell(8, 1).Range.Text = "row 8"
    .Range.Font.Name = "Tahoma"
    .Range.Font.Size = 15
   End With
    
   Set objTable = Nothing

'''''Here also  i would like blank line and start
   txtword = "Paragraph / Line after table"

   With objRange
        .Text = txtword & vbCr 'add the paragraph break at the end of the text string
        .Collapse Direction:=0
    End With
'''''The above range started in the table of 1st Row

   Set objWord = Nothing
    Set objDoc = Nothing
    Set objRange = Nothing
''''''    Set objTable = Nothing
End Sub
Gmaxey tried yours but got me type mismatch error
at Set oRng = oTbl.Range

Glad to be here on this forum
Thank you guys
SamD
Reply With Quote
  #6  
Old 08-03-2019, 12:56 PM
gmaxey gmaxey is offline syntax for inserting blank line before inserting table and after a line or paragraph Windows 10 syntax for inserting blank line before inserting table and after a line or paragraph Office 2016
Expert
 
Join Date: May 2010
Location: Brasstown, NC
Posts: 1,429
gmaxey is a jewel in the roughgmaxey is a jewel in the roughgmaxey is a jewel in the roughgmaxey is a jewel in the rough
Default

Sam,


Yes. I wrote the code from Word. Those declaration should be:

Dim oRng as Word.Range
Dim oTbl as Word.Table.


Best Regards,
Greg Maxey

May God have mercy on my enemies, because I won't.
~ General George S. Patton
__________________
Greg Maxey
Please visit my web site at http://www.gregmaxey.com/
Reply With Quote
  #7  
Old 08-03-2019, 08:04 PM
gmayor's Avatar
gmayor gmayor is offline syntax for inserting blank line before inserting table and after a line or paragraph Windows 10 syntax for inserting blank line before inserting table and after a line or paragraph Office 2016
Expert
 
Join Date: Aug 2014
Posts: 4,105
gmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud of
Default

To add a line after the table, you need to reset the range e.g.

Code:
Private Sub InsertingBlankLine()
Dim objWord As Object
Dim txtword As String, sh As Worksheet
Dim objDoc As Object
Dim objRange As Object
Dim objTable As Object
Dim intRows As Integer
Dim intCols As Integer
Dim oCell As Object

    txtword = "fsdhfkhfkdhfdskfhd fkdshfdkfhdskfhdsfd fdshgfdjdsjfg"
    On Error Resume Next
    Set objWord = GetObject(, "Word.Application")
    If Err Then
        Set objWord = CreateObject("Word.Application")
    End If
    On Error GoTo 0

    Set objDoc = objWord.Documents.Add
    objWord.Visible = True
    Set objRange = objDoc.Range
    With objRange
        .Text = txtword & vbCr    'add the paragraph break at the end of the text string
        .collapse Direction:=0
    End With
    intRows = 8: intCols = 5
    Set objTable = objDoc.Tables.Add(objRange, intRows, intCols)
    '''''''objTable.Borders.Enable = True

    With objTable
        .Borders.Enable = True
        Set oCell = .Cell(1, 1).Range
        oCell.End = oCell.End - 1
        oCell.Text = "row 1"
        oCell.Bold = True
        Set oCell = .Cell(2, 1).Range
        oCell.End = oCell.End - 1
        oCell.Text = "row 2"
        Set oCell = .Cell(3, 1).Range
        oCell.End = oCell.End - 1
        oCell.Text = "row 3"
        Set oCell = .Cell(4, 1).Range
        oCell.End = oCell.End - 1
        oCell.Text = "row 4"
        Set oCell = .Cell(5, 1).Range
        oCell.End = oCell.End - 1
        oCell.Text = "row 5"
        Set oCell = .Cell(6, 1).Range
        oCell.End = oCell.End - 1
        oCell.Text = "row 6"
        Set oCell = .Cell(7, 1).Range
        oCell.End = oCell.End - 1
        oCell.Text = "row 7"
        Set oCell = .Cell(8, 1).Range
        oCell.End = oCell.End - 1
        oCell.Text = "row 8"

        .Range.Font.Name = "Tahoma"
        .Range.Font.Size = 15
    End With


    '''''Here also  i would like blank line and start
    txtword = "Paragraph / Line after table"

    Set objRange = objDoc.Range

    With objRange
        .collapse Direction:=0
        .Text = txtword & vbCr    'add the paragraph break at the end of the text string
    End With
    '''''The above range started in the table of 1st Row

    Set objWord = Nothing
    Set objDoc = Nothing
    Set objRange = Nothing
    Set objTable = Nothing
    Set oCell = Nothing
End Sub
__________________
Graham Mayor - MS MVP (Word) (2002-2019)
Visit my web site for more programming tips and ready made processes www.gmayor.com
Reply With Quote
  #8  
Old 08-04-2019, 06:57 AM
gmaxey gmaxey is offline syntax for inserting blank line before inserting table and after a line or paragraph Windows 10 syntax for inserting blank line before inserting table and after a line or paragraph Office 2016
Expert
 
Join Date: May 2010
Location: Brasstown, NC
Posts: 1,429
gmaxey is a jewel in the roughgmaxey is a jewel in the roughgmaxey is a jewel in the roughgmaxey is a jewel in the rough
Default

How about:

Code:
Private Sub InsertingBlankLine()
Dim oWord As Object
Dim oDoc As Object
Dim oCell As Object
  On Error Resume Next
  Set oWord = GetObject(, "Word.Application")
  If Err Then
    Set oWord = CreateObject("Word.Application")
  End If
  On Error GoTo 0
  Set oDoc = oWord.Documents.Add
  oWord.Visible = True
  With oDoc
    .Content = "kdhfdskfhd fkdshfdkfhdskfhdsfd fdshgfdjdsjfg" & vbCr & vbCr & "Paragraph / Line after table"
    .Tables.Add .Paragraphs(2).Range, 8, 5
    With .Tables(1)
      .Borders.Enable = True
      Set oCell = .Cell(1, 1).Range
      oCell.End = oCell.End - 1
      oCell.Text = "row 1"
      oCell.Bold = True
      Set oCell = .Cell(2, 1).Range
      oCell.End = oCell.End - 1
      oCell.Text = "row 2"
      Set oCell = .Cell(3, 1).Range
      oCell.End = oCell.End - 1
      oCell.Text = "row 3"
      Set oCell = .Cell(4, 1).Range
      oCell.End = oCell.End - 1
      oCell.Text = "row 4"
      Set oCell = .Cell(5, 1).Range
      oCell.End = oCell.End - 1
      oCell.Text = "row 5"
      Set oCell = .Cell(6, 1).Range
      oCell.End = oCell.End - 1
      oCell.Text = "row 6"
      Set oCell = .Cell(7, 1).Range
      oCell.End = oCell.End - 1
      oCell.Text = "row 7"
      Set oCell = .Cell(8, 1).Range
      oCell.End = oCell.End - 1
      oCell.Text = "row 8"
      .Range.Font.Name = "Tahoma"
      .Range.Font.Size = 15
    End With
  End With
  Set oWord = Nothing
  Set oDoc = Nothing
  Set oCell = Nothing
End Sub
__________________
Greg Maxey
Please visit my web site at http://www.gregmaxey.com/
Reply With Quote
  #9  
Old 08-04-2019, 11:10 PM
SamDsouza SamDsouza is offline syntax for inserting blank line before inserting table and after a line or paragraph Windows 10 syntax for inserting blank line before inserting table and after a line or paragraph Office 2013
Advanced Beginner
syntax for inserting blank line before inserting table and after a line or paragraph
 
Join Date: Aug 2019
Posts: 71
SamDsouza is on a distinguished road
Default

Thank you so much GMayor and GMaxey for all your help and guidance.

I am posting all together new thread as again i am stuck in incorporating new table with different Placing and positioning new Table.
and bit confussed

Thanks
SamD
Reply With Quote
Reply



Similar Threads
Thread Thread Starter Forum Replies Last Post
syntax for inserting blank line before inserting table and after a line or paragraph Sparse words in line when inserting a link titanic Word 4 11-12-2018 01:50 PM
syntax for inserting blank line before inserting table and after a line or paragraph Inserting code line in Word Module mihnea96 Excel Programming 2 08-15-2017 04:57 AM
syntax for inserting blank line before inserting table and after a line or paragraph Word inserting line breaks / wrapping text Girloutside Word 1 08-10-2015 12:45 PM
syntax for inserting blank line before inserting table and after a line or paragraph inserting wordwrap table causes paragraph break in text sodiumkpump Word Tables 1 08-05-2015 06:36 PM
syntax for inserting blank line before inserting table and after a line or paragraph Inserting key strokes with a macro line ACA Word VBA 4 12-03-2012 04:40 AM

Other Forums: Access Forums

All times are GMT -7. The time now is 12:11 PM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2024 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft