Microsoft Office Forums

Go Back   Microsoft Office Forums > >

 
 
Thread Tools Display Modes
Prev Previous Post   Next Post Next
  #1  
Old 03-03-2022, 05:13 AM
Thom.Simmo Thom.Simmo is offline Define range within word table between specific text instance and end of cell content Windows 10 Define range within word table between specific text instance and end of cell content Office 2019
Novice
Define range within word table between specific text instance and end of cell content
 
Join Date: Mar 2022
Posts: 5
Thom.Simmo is on a distinguished road
Default Define range within word table between specific text instance and end of cell content

Hi



Not entirely sure if this falls within Word VBA or Excel VBA as I am attempting to put together a script that copy's the content of a pivot table into word and then format it. The script is run from within excel but opens a new word document and formats the table within word.

The script is admittedly cobbled together from various examples.

I have attached 2 examples of the table below. Table eg1 shows the table as it arrives from excel

Table eg1.JPG

Table eg2 is an example of how I am trying to format the table contents.

Table eg2.JPG

The text that I am attempting to italicise is in the cells of the third column. The words Recommended Action will always be imported from excel and I would like to italicise these words and everything following within the cell.

I am having trouble defining the range which I am trying to italicise. I have tried various permutations and the current version is below,

Code:
'Find and italiscise text folowing "Reccomended Action"
    pos1 = InStr(oRng, "Reccomended Action")
    pos2 = oRow.Cells(3).Range.End
        rngFormat = oRng.Range(Start:=pos1, End:=pos2)
        
        rngFormat.Italic = True

I have included all the code I am using below as I am not sure if there may be some issues to do with making edits in word with VBA run from within Excel.


Code:
Sub ExcelRangeToWord()

'Includes  multiple edits and adaptations from original source
'PURPOSE: Copy/Paste An Excel Table Into a New Word Document
'NOTE: Must have Word Object Library Active in Order to Run _
  (VBE > Tools > References > Microsoft Word 12.0 Object Library)
'SOURCE: www.TheSpreadsheetGuru.com

Dim tbl As PivotTable
Dim WordApp As Word.Application
Dim myDoc As Word.Document
Dim WordTable As Word.Table

'Optimize Code
  Application.ScreenUpdating = False
  Application.EnableEvents = False

'Copy Range from Excel
  Set tbl = ThisWorkbook.Worksheets("InspectionPivot").PivotTables("InspectionPivot")

'Create an Instance of MS Word
  On Error Resume Next
    
    'Is MS Word already opened?
      Set WordApp = GetObject(class:="Word.Application")
    
    'Clear the error between errors
      Err.Clear

    'If MS Word is not already open then open MS Word
      If WordApp Is Nothing Then Set WordApp = CreateObject(class:="Word.Application")
    
    'Handle if the Word Application is not found
      If Err.Number = 429 Then
        MsgBox "Microsoft Word could not be found, aborting."
        GoTo EndRoutine
      End If

  On Error GoTo 0
  
'Make MS Word Visible and Active
  WordApp.Visible = True
  WordApp.Activate
    
'Create a New Document
  Set myDoc = WordApp.Documents.Add
  
'Copy Excel Table Range
  tbl.TableRange2.Copy

'Paste Table into MS Word
  myDoc.Paragraphs(1).Range.PasteExcelTable _
    LinkedToExcel:=False, _
    WordFormatting:=False, _
    RTF:=False
  
'set column widths
Set WordTable = myDoc.Tables(1)
    With WordTable
                WordTable.Columns(1).Width = CentimetersToPoints(0.5)
                WordTable.Columns(2).Width = CentimetersToPoints(3)
                WordTable.Columns(3).Width = CentimetersToPoints(11.5)
                WordTable.Columns(4).Width = CentimetersToPoints(1)
    End With

'delete empty rows
    With WordTable
        noOfCol = WordTable.Range.Rows(1).Cells.Count
                For i = .Rows.Count To 1 Step -1
                    With .Rows(i)
                        If Len(.Range) = noOfCol * 2 + 2 Then .Delete
                    End With
                Next i
    End With

'Insert report refrence section number before item numbers

    Dim SecNum As String
    Dim RefCell As Cell
        SecNum = "6."
    
        For Each RefCell In WordTable.Range.Columns(1).Cells
          RefCell.Range.InsertBefore (SecNum)
         Next RefCell

'format report text in column 3
   
Dim oTbl As Table
Dim oRow As Row
Dim oRng As Range
Dim rngFormat As Word.Range
Dim WrdFind As Find

Dim pos1 As Long
Dim pos2 As Long

Set oTbl = ActiveDocument.Tables(1)

For Each oTbl In ActiveDocument.Tables
  
  For Each oRow In WordTable.Rows
  
    Set oRng = oRow.Cells(3).Range
    
    'Replace line brakes with paragraph
    oRow.Cells(3).Range = Replace(oRow.Cells(3).Range.Text, vbVerticalTab, vbCrLf)
    
    'Make first para bold
    oRow.Cells(3).Range.Paragraphs(1).Range.Bold = True
    
    'Find and italiscise text folowing "Reccomended Action"
    pos1 = InStr(oRng, "Reccomended Action")
    pos2 = oRow.Cells(3).Range.End
        rngFormat = oRng.Range(Start:=pos1, End:=pos2)
        
        rngFormat.Italic = True
   
     
  Next
  
  
Next

EndRoutine:


'Optimize Code
  Application.ScreenUpdating = True
  Application.EnableEvents = True

'Clear The Clipboard
  Application.CutCopyMode = False

End Sub

Any help will be greatly appreciated,

Cheers
Thom
Reply With Quote
 



Similar Threads
Thread Thread Starter Forum Replies Last Post
Define range within word table between specific text instance and end of cell content How to change the font color of specific text within a Word table cell epid011 Word Tables 2 05-15-2017 05:21 PM
Define range within word table between specific text instance and end of cell content How do I add selection of multi line content control list to specific table cell Dudlee Word VBA 1 09-20-2016 04:58 PM
Define range within word table between specific text instance and end of cell content VBA Table – Search All Tables - Find & Replace Text in Table Cell With Specific Background Color jc491 Word VBA 8 09-30-2015 06:10 AM
Define range within word table between specific text instance and end of cell content Creating a plain text content control for every instance of a word or phrase RobsterCraw Word VBA 16 11-20-2012 03:25 PM
Define range within word table between specific text instance and end of cell content Word VBA: Cannot Edit Range (Delete characters except the first in a table cell) tinfanide Word VBA 3 04-27-2012 09:48 AM

Other Forums: Access Forums

All times are GMT -7. The time now is 06:01 AM.


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