Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 01-28-2024, 01:17 AM
phbench phbench is offline Word create or open with VBA functionality? Windows 11 Word create or open with VBA functionality? Office 2021
Novice
Word create or open with VBA functionality?
 
Join Date: Jan 2024
Posts: 1
phbench is on a distinguished road
Smile Word create or open with VBA functionality?

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 –
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
Reply With Quote
  #2  
Old 01-28-2024, 06:06 AM
Italophile Italophile is offline Word create or open with VBA functionality? Windows 11 Word create or open with VBA functionality? Office 2021
Expert
 
Join Date: Mar 2022
Posts: 338
Italophile is just really niceItalophile is just really niceItalophile is just really niceItalophile is just really nice
Default

Please explain what you mean by "when I launch a document without the VBA template it does not have the functionality I desire."
You may find it helpful to read this article first: What is the relationship between a Microsoft Word document and its template? | ShaunaKelly.com

Please also explain your process of using the template. Typically, templates are stored in either the User Templates folder or the Workgroup Templates folder and accessed from File | New.

There is no need to use FSO to check if a file exists as VBA has a function for that. Just add the following into your project
Code:
Public Function FileExists(fullPath As String) As Boolean
  On Error GoTo NoFile
  FileExists = (Dir(fullPath, vbNormal) <> "")
  Exit Function
NoFile:
  FileExists = False
End Function
Reply With Quote
  #3  
Old 01-28-2024, 01:30 PM
Charles Kenyon Charles Kenyon is offline Word create or open with VBA functionality? Windows 11 Word create or open with VBA functionality? Office 2021
Moderator
 
Join Date: Mar 2012
Location: Sun Prairie, Wisconsin
Posts: 9,140
Charles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant future
Default

Cross-posted at:Word create or open with VBA functionality?


For cross-posting etiquette, please read: A Message to Forum Cross-Posters
Reply With Quote
Reply

Tags
template, vba



Similar Threads
Thread Thread Starter Forum Replies Last Post
Open my default template when right click within a folder to create a new Word document LaPlume Word 0 07-30-2023 02:38 PM
WORD Functionality works slowly mohsen.amiri Word 1 03-13-2023 02:51 AM
Word create or open with VBA functionality? how to create a box in word document that takes a password to open an access field kawtharz Word VBA 2 08-13-2015 12:43 AM
Word create or open with VBA functionality? Text Box with Word Functionality? elmousa68 Word VBA 3 01-09-2014 01:55 AM

Other Forums: Access Forums

All times are GMT -7. The time now is 12:00 AM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2024 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft