I am trying to open a text document without a menu bar and at a specific size. The code…
- User
I am trying to open a text document without a menu bar and at a specific size. The code I am trying has error. Sub OpenTextFile() Dim fs As Object Dim f As Object
Set fs = CreateObject("Scripting.FileSystemObject") Set f = fs.OpenTextFile("C:\Users\HP\Desktop\VBA NOTES.txt", 1, False) With ActiveWindow .Width = 500 .Height = 500 End With f.CloseEnd Sub
- Assistant
The error could be because the code is not placed in a module or because "Scripting.FileSystemObject" reference is not enabled in the VBA references.
To open a text document without a menu bar and at a specific size, you can try the following code:
Sub OpenTextFile() Dim fs As Object Dim f As Object Dim doc As Document
Set fs = CreateObject("Scripting.FileSystemObject") Set f = fs.OpenTextFile("C:\Users\HP\Desktop\VBA NOTES.txt", 1, False)
Set doc = Documents.Add(Visible:=False) doc.ActiveWindow.View.ShowMenuFloaties = False doc.ActiveWindow.Width = 500 doc.ActiveWindow.Height = 500 doc.Content.InsertAfter f.ReadAll f.Close
doc.Activate End Sub
This will create a new Word document, hide the menu bar, set the width and height to 500, insert the contents of the text file, and show the final document.
- User
I get a User defined type not defined on doc As Document
- Assistant
The error "User defined type not defined" occurs when the VBA compiler does not recognize the object or data type you are trying to use. In this case, it means that the VBA compiler does not recognize the "Document" object.
To resolve this error, you need to add a reference to the Word object library.
Follow these steps:
- In the VBA editor, go to the Tools menu and select References
- In the References dialog box, find and check the box beside "Microsoft Word Object Library"
- Click OK to save the changes
Once you have added the reference to the Word object library, the error should disappear and the code should work.
- User
still getting errors
- Assistant
If you have added the reference to the Word object library and are still getting errors, it's possible that the VBA code is being run from a non-Word application, or that there is a problem with the installation or configuration of Word on your computer.
To troubleshoot this issue, you can try the following:
-
Make sure that you are running the code from within a Word document, and that the document is saved to a location that you have read/write access to.
-
Verify that the Word object library reference is selected correctly. To do this, go to Tools > References in the VBA Editor, and make sure that "Microsoft Word Object Library" is checked. Also, ensure that the version of the library matches the version of Word installed on your computer.
-
Ensure that there are no syntax errors or typos in your code, and that all variables are declared correctly.
-
Try repairing or reinstalling Word to ensure that it is installed correctly and functioning properly.
If none of these solutions work, it may be helpful to provide more specific details about the errors you are receiving, and any other relevant information about your computer, Word installation, and VBA environment.
-
- User
But the document I am trying to manipulate is a NotePad document not word
- Assistant
I apologize for the confusion - I misunderstood the original question and thought you were trying to open a text file in Word.
To open a text file in Notepad without the menu bar and at a specific size, you can use the following VBA code:
Option Explicit Private Declare Function SetWindowPos Lib "user32" (ByVal hwnd As Long, _ ByVal hWndInsertAfter As Long, ByVal x As Long, ByVal y As Long, _ ByVal cx As Long, ByVal cy As Long, ByVal wFlags As Long) As Long Private Declare Function FindWindowExA Lib "user32" (ByVal hWndParent As Long, _ ByVal hWndChildAfter As Long, ByVal lpszClass As String, _ ByVal lpszWindow As String) As Long Sub OpenNotepadFile() Dim path As String path = "C:\Users\HP\Desktop\VBA NOTES.txt" Shell "notepad.exe """ & path & """", vbNormalFocus Dim npWnd As Long, editWnd As Long npWnd = FindWindowExA(0, 0, "Notepad", and vbNullString) editWnd = FindWindowExA(npWnd, 0, "Edit", vbNullString) SetWindowPos npWnd, 0, 0, 0, 500, 500, 0 If editWnd <> 0 Then SendMessage editWnd, &H80, False, 0 End If End Sub - User
How can I set a variable to a line of code so that it only runs once when the workbook is open.
- Assistant
To run a line of code only once when the workbook is opened, you can use the Workbook_Open event. This event is triggered when the workbook is opened, and you can use it to initialize variables or perform other tasks that you only want to run once.
Here's an example of how to use the Workbook_Open event to set a variable to a specific value:
Private Sub Workbook_Open() 'Set a variable to a specific value Dim myVar As Long myVar = 10 End SubIn this example, the variable
myVaris set to a value of 10 when the workbook is opened. This code will only run once, when the workbook is first opened.To add this code to your workbook, open the VBA Editor and double-click on the ThisWorkbook module in the Project Explorer. This will open the code module for the workbook. Then, copy and paste the code above into the code window. The code will automatically run when the workbook is opened, and the variable
myVarwill be set to 10.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.