View Single Post
 
Old 03-05-2020, 06:13 AM
SamDsouza SamDsouza is offline Windows 10 Office 2013
Advanced Beginner
 
Join Date: Aug 2019
Posts: 71
SamDsouza is on a distinguished road
Default Create a Word Document with VBA

Hello

I am getting the above error on below syntax. Though i'v e ran the macro from VBA excel to open the word document

Code:
     objDoc.Range(objDoc.Paragraphs(17).Range.Characters(6).Start, objDoc.Paragraphs(17).Range.Characters(timeLen).End).Font.Bold = True
What i want is to Bold a text Value but the error does not allow.
Please guide with me proper set references, document etc

Code:
Public Sub msWord_Structure()

Dim objWord As Object
Dim txtword As String, sh As Worksheet
Dim objDoc As Object, objSelection As Object
Dim objRange As Object
Dim objTable As Object, objTable2 As Object
Dim para As Object
Dim intRows As Integer
Dim intCols As Integer
  Dim ocell As Object
Dim mpStart As Long
Dim chqNobold As String

Dim txtHeader As String, txtForRtgs As String

chqNobold = txtChqNo.Text
txtHeader = "RTGS/ Request Form" & vbCrLf & vbCrLf
txtword =  Chr(144) & "RTGS                             " & Chr(144) & "" & vbCrLf & vbCrLf
txtForRtgs = "For RTGS"


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
        objWord.activedocument.Range.Font.Name = "Tahoma"
        objWord.activedocument.Range.Font.Size = "12"
        objWord.activedocument.Paragraphs.SpaceAfter = 0 'InchesToPoints(0.5)
    
 Set objRange = objDoc.Range
    With objRange
        .Collapse Direction:=1
        .Text = txtHeader
          .Font.Name = "Tahoma"
          .Font.Size = "16"
          .Font.Underline = True 'wdUnderlineSingle
          .Paragraphs.SpaceAfter = 0
          .ParagraphFormat.Alignment = 1 'this centers\'wdAlignParagraphCenter '3
    .Collapse Direction:=0
    End With

      
      With objRange
           .Collapse Direction:=1
          .Text = txtword
          .Font.Name = "Tahoma"
          .Font.Size = "12"
          .Paragraphs.SpaceAfter = 0
          .ParagraphFormat.Alignment = 3
          '.Collapse Direction:=wdCollapseEnd
          .Collapse Direction:=0
      End With
      
With objRange
           .Collapse Direction:=1
          .Text = txtForRtgs
          .Font.Name = "Tahoma"
          .Font.Size = "12"
          .Font.Underline = True 'wdUnderlineSingle
          .Paragraphs.SpaceAfter = 0
          .ParagraphFormat.Alignment = 3

      End With
      
            
            intRows = 4: intCols = 2
    Set objTable = objDoc.Tables.Add(objRange, intRows, intCols)
  
   With objTable
          .Columns(1).Width = 200
          .Columns(2).Width = 300

          .Borders.Enable = True
  
       .Rows.HorizontalPosition = 5  '100
       .Rows.VerticalPosition = 10
       .Columns(1).Width = 300
       .Columns(2).Width = 130
       
       .Range.Collapse Direction:=0
       .Borders.Enable = True
              
       Set ocell = .Cell(1, 1).Range
           ocell.Text = "Maximum Limit For  Transaction"
           ocell.End = .Cell(1, 2).Range.End
           ocell.Cells.Merge
           ocell.ParagraphFormat.Alignment = 1
           'ocell.ParagraphFormat.Alignment = wdAlignParagraphCenter
           ocell.End = ocell.End - 1
            
            
      Set ocell = .Cell(2, 1).Range
            ocell.End = ocell.End - 1
            ocell.Text = "IDBI Bank Customer"
            ocell.ParagraphFormat.Alignment = 3
            
      Set ocell = .Cell(2, 2).Range
            ocell.End = ocell.End - 1
            ocell.Text = "No Limit"
            ocell.ParagraphFormat.Alignment = 3
            
      Set ocell = .Cell(3, 1).Range
            ocell.End = ocell.End - 1
            ocell.Text = "Non IDBI Bank Customer & Indo Nepal  Remittance"
            ocell.ParagraphFormat.Alignment = 3
 
      Set ocell = .Cell(3, 2).Range
            ocell.End = ocell.End - 1
            ocell.Text = "Upto INR 50,000/-"
            ocell.ParagraphFormat.Alignment = 3
            
      Set ocell = .Cell(4, 1).Range
            ocell.End = ocell.End - 1
            ocell.Text = "Purpose of Remittance to Nepal - Family Maintenance only"
            ocell.ParagraphFormat.Alignment = 3
            
      Set ocell = .Cell(4, 2).Range
            ocell.End = ocell.End - 1
            ocell.Text = "Yes / No"
            ocell.ParagraphFormat.Alignment = 3
                     ' .Range.Collapse Direction:=wdCollapseEnd
            ocell.Collapse Direction:=0

End With

Dim timeLen As Integer, custNameLen As Integer, RefAcNoLen As Integer, chqNoLen As Integer, MyContactNosLen As Integer
timeLen = (6 + Len(txtTime.Text) + 1)
custNameLen = (16 + Len(cmbMyCustName.Text) + 1)
RefAcNoLen = (13 + Len(txtRefAcNo.Text) + 1)
chqNoLen = (10 + Len(txtChqNo.Text) + 1)
MyContactNosLen = (10 + Len(txtMyContactNos.Text) + 1)
     
     objDoc.Range(objDoc.Paragraphs(17).Range.Characters(6).Start, _
     objDoc.Paragraphs(17).Range.Characters(timeLen).End).Font.Bold = True



txtword = vbNewLine & "Time " & txtTime.Text & vbNewLine & vbNewLine & "Customer Name : " & cmbMyCustName.Text & vbNewLine & vbNewLine & _
"Account No. " & txtRefAcNo.Text & " Chq No. " & txtChqNo.Text & vbNewLine & vbNewLine & "Mobile No" & txtMyContactNos.Text & " Tel No." & _
 vbNewLine & vbNewLine & "Address of Remitter (Mandatory for Non IDBI Bank Customer)___________________________________" & vbNewLine & vbNewLine & _
"__________________________________________" & " Email ID ______________________________________________" & vbNewLine & vbNewLine & _
 vbNewLine & vbNewLine & " Incase of NonIDBI Bank Customer Amount of Cash Deposited _____________________________________" & _
 vbNewLine & vbNewLine '& " Beneficiary Details :"


Set objRange = objDoc.Range
 With objRange
          .Collapse Direction:=0 
          .Text = txtword
          .Font.Name = "Tahoma"
          .Font.Size = "12"
          .Paragraphs.SpaceAfter = 0
          .Collapse Direction:=0 ' wdCollapseStart

      End With
Thanks
SamD
Reply With Quote