PDA

View Full Version : Word create or open with VBA functionality?



ph-bench
01-26-2024, 01:09 PM
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 it. 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 –


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 SubSJUpaper01.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

Aussiebear
01-27-2024, 02:06 AM
Have a look at this section of code. It may give you an alternative idea.



Public Sub SaveDemo()
Dim FName As Variant
FName = Environ("userprofile") & "\My Documents\" & ActiveSheet.Name & ".xls"
If Not Dir(FName) = "" Then
Ln1 = "The file " & FName & "already exists." & vbCrLf
Ln2 = "Please change the following path if you don't want to overwrite."
FName = Application.InputBox(Ln1 & Ln2, "OverWrite File ?", FName)
If FName = False Then Exit Sub
End If
If Not Len(Trim(FName)) = 0 Then
Application.DisplayAlerts = False
ActiveWorkbook.SaveAs Filename:=FName
Application.DisplayAlerts = True
MsgBox "The File Has Been Saved In " & FName
End If
End Sub

Chas Kenyon
01-27-2024, 04:09 PM
You could put your code into a Global Template (https://www.addbalance.com/usersguide/templates.htm#Global), have it test for the existence of your document. If it exists, open the document and go to a bookmark in the document to do whatever it is you want your code to do at that point. If it does not exist, create the document. It could create the document based on the global template, itself, or upon another template.

Also, look into a UserForm (a custom dialog box) as a way to do things that involve user input.


Create & Employ a Userform by Greg Maxey (http://gregmaxey.com/word_tip_pages/create_employ_userForm.html)
Create a Simple Userform by Graham Mayor, MVP (http://www.gmayor.com/Userform.htm)

Chas Kenyon
01-28-2024, 01:31 PM
Cross-posted at: https://www.msofficeforums.com/word-vba/51970-word-create-open-vba-functionality.html

For cross-posting etiquette, please read: A Message to Forum Cross-Posters (https://www.excelguru.ca/content.php?184)

Aussiebear
01-28-2024, 03:42 PM
ph-bench, for your information, you should always provide links to your cross posts.
This is a requirement, not just a request.
If you have cross posted at other places, please add links to them too.

ph-bench
01-30-2024, 10:29 PM
Cross-posted at: https://www.msofficeforums.com/word-vba/51970-word-create-open-vba-functionality.html

For cross-posting etiquette, please read: A Message to Forum Cross-Posters (https://www.excelguru.ca/content.php?184)

Hi, thanks for letting me know. I read the page from the link above. I will do this when I post some where else from now on.

ph-bench
01-30-2024, 10:30 PM
ph-bench,for your information, you should always provide links to your cross posts.
This is a requirement, not just a request.
If you have cross posted at other places, please add links to them too.

Noted. I will be more aware. Thanks

ph-bench
01-30-2024, 10:33 PM
You could put your code into a Global Template (https://www.addbalance.com/usersguide/templates.htm#Global)...
Hi, thanks for the replay. That is some good reading at the site you sent me to about templates. I looking at ideas.

ph-bench
01-30-2024, 10:42 PM
Have a look at this section of code. It may give you an alternative idea.

Thanks so much for this suggestion. I learned about labels and error handling that jumps to place or performs based on that logic. That code is helpful. I have come up with a way where the document is quickly closed after it is created. I do not know if this is smartest way. However, it works. Meaning that I can open or create new *.docx files that function with programming the original *.dotm has. I don't know if that opened and quickly closed document creates hooks that can cause trouble with memory later. I will try to incorporate some of what you show and keep an eye on what works.