Word VBA opens Excel workbook as read-only even though it was not originally created as such

MarkSELA

New Member
Joined
Dec 27, 2021
Messages
10
Office Version
  1. 365
Platform
  1. 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:

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.
 

Attachments

  • Screenshot 2023-02-25 150916.png
    Screenshot 2023-02-25 150916.png
    30.8 KB · Views: 7

Excel Facts

Formula for Yesterday
Name Manager, New Name. Yesterday =TODAY()-1. OK. Then, use =YESTERDAY in any cell. Tomorrow could be =TODAY()+1.
Your code doesn't close the wb or quit the Excel application. Maybe your wb is read only because it's already open? Dave
 
Upvote 0

Forum statistics

Threads
1,213,554
Messages
6,114,280
Members
448,562
Latest member
Flashbond

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top