Consulting

Results 1 to 9 of 9

Thread: Word create or open with VBA functionality?

  1. #1
    VBAX Newbie
    Joined
    Jan 2024
    Posts
    5
    Location

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

  2. #2
    Moderator VBAX Wizard Aussiebear's Avatar
    Joined
    Dec 2005
    Location
    Queensland
    Posts
    5,059
    Location
    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
    Remember To Do the Following....
    Use [Code].... [/Code] tags when posting code to the thread.
    Mark your thread as Solved if satisfied by using the Thread Tools options.
    If posting the same issue to another forum please show the link

  3. #3
    VBAX Contributor
    Joined
    Jul 2020
    Location
    Sun Prairie
    Posts
    123
    Location
    You could put your code into a Global Template, 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.


  4. #4
    VBAX Contributor
    Joined
    Jul 2020
    Location
    Sun Prairie
    Posts
    123
    Location
    Cross-posted at: https://www.msofficeforums.com/word-...tionality.html

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

  5. #5
    Moderator VBAX Wizard Aussiebear's Avatar
    Joined
    Dec 2005
    Location
    Queensland
    Posts
    5,059
    Location
    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.
    Remember To Do the Following....
    Use [Code].... [/Code] tags when posting code to the thread.
    Mark your thread as Solved if satisfied by using the Thread Tools options.
    If posting the same issue to another forum please show the link

  6. #6
    VBAX Newbie
    Joined
    Jan 2024
    Posts
    5
    Location
    Quote Originally Posted by Chas Kenyon View Post
    Cross-posted at: https://www.msofficeforums.com/word-...tionality.html

    For cross-posting etiquette, please read: A Message to Forum Cross-Posters
    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.

  7. #7
    VBAX Newbie
    Joined
    Jan 2024
    Posts
    5
    Location
    Quote Originally Posted by Aussiebear View Post
    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

  8. #8
    VBAX Newbie
    Joined
    Jan 2024
    Posts
    5
    Location
    Quote Originally Posted by Chas Kenyon View Post
    You could put your code into a Global Template...
    Hi, thanks for the replay. That is some good reading at the site you sent me to about templates. I looking at ideas.

  9. #9
    VBAX Newbie
    Joined
    Jan 2024
    Posts
    5
    Location
    Quote Originally Posted by Aussiebear View Post
    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.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •