MarkSELA
New Member
- Joined
- Dec 27, 2021
- Messages
- 13
- Office Version
- 365
- Platform
- Windows
I have a MS Word VBA sub which opens an Excel workbook which was created and saved as UNPROTECTED and when the workbook is opened again it is read only. Other workbooks in the same folder are not opened by the sub and NOT affected. I am attempting to pull a string from the workbook and convert it to a professional equation using the Omath object in Word VBA. Windows 11, Office 365. Here is my code:
I've include a picture of the worksheet.
VBA Code:
Sub CreateWorksheet()
Dim r As Range
Dim NewWS As Document
Dim NewTable As Table
Dim objRange As Range
Dim objEq As OMath
Dim objCell As Word.Cell
Dim i As Integer
Dim EqString As String
Dim t As String
Dim oXL As Excel.Application
Dim oWB As Excel.Workbook
Dim oSheet As Excel.Worksheet
Set oXL = CreateObject("Excel.Application")
Set oWB = oXL.Workbooks.Open("C:\Users\barry\Desktop\WS Generators\Experimental WS Generator3.xlsm")
oWB.Activate
Set oSheet = oXL.ActiveWorkbook.Sheets("EXPER01")
Documents.Open "C:\Users\barry\Desktop\VBA-Python\Worksheet Template.docm"
ActiveDocument.Activate
Set NewWS = ActiveDocument
i = 1
Set r = ActiveDocument.Range(Start:=0, End:=Selection.End)
Set NewTable = NewWS.Tables.Add(r, 6, 3)
Set objCell = NewTable.Cell(i, 2)
objCell.Range.ParagraphFormat.Alignment = wdAlignParagraphLeft
With NewTable
.Borders.Enable = True
.Columns(1).Width = CentimetersToPoints(1)
.Columns(2).Width = CentimetersToPoints(7)
.Columns(3).Width = CentimetersToPoints(7)
End With
For i = 1 To 6
EqString = oSheet.Cells.Range("r4").Value
Set objCell = NewTable.Cell(i, 2)
Set objRange = objCell.Range
objRange.Text = EqString
Set objRange = Selection.OMaths.Add(objRange)
Set objEq = objRange.OMaths(1)
objEq.BuildUp
Next i
End Sub
I've include a picture of the worksheet.