Word create or open with VBA functionality?

phbench

New Member
Joined
Jan 28, 2024
Messages
1
Office Version
  1. 2021
Platform
  1. Windows
If I am thinking about this in the wrong way do not hesitate to say. I am new to VBA but I am not new to programming. I want to be able to automate the creation of Word documents. That is when the document does not exist already. If the document exists, I would like just to open it.
Where I am running into problems is when I use a *.dotm to do the creation, it works well with the VBA programming I am learning. However, if the document exists, I want to open it with the VBA programming functionality I have programmed into the original template. I make this a point because when I launch a document without the VBA template it does not have the functionality I desire.
For now, when I use some logic to just open it, VBA creates an additional document. I am sure this is because it is the normal way a *.dotm works. Besides the creation of the other unneeded document, it allows my document (SJUpaper01.docx) opened version to have the functionality I desire.
Here is some of the VBA code I am having success with –
VBA Code:
Sub NewOrOpenDoc()
    Dim h2nWrd As String
    Dim fso As Object
    Set fso = CreateObject("Scripting.FileSystemObject")
    Let h2nWrd = "D:/Stuff/VBA/Test/SJUpaper01.docx" ' Let to assign data variable although not nessicary
    If fso.FileExists(h2nWrd) Then
        Documents.Open FileName:=h2nWrd
    Else
        ActiveDocument.SaveAs2 FileName:=h2nWrd, _
        FileFormat:=wdFormatDocumentDefault
    End If
End Sub
SJUpaper01.docx is the name I will eventually populate dynamically each time I launch from the *.dotm template. For now, it is the document I am testing with. Could someone suggest a way to do this? Thanks
 

Excel Facts

Copy PDF to Excel
Select data in PDF. Paste to Microsoft Word. Copy from Word and paste to Excel.

Forum statistics

Threads
1,224,816
Messages
6,181,138
Members
453,021
Latest member
Justyna P

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