Word Macro for saving word form with variable Name

Anonymous
2019-12-05T15:32:55+00:00

Hi,

I have been reading a post within this forum Here and have found that I can now create a saved word doc from a command button within a word form I have created. 

I have been saving the files as text files which I assume is because the code I am using shows as strFileName = .Text

I was hoping that to save the files as word docs all I would need to do is change this to .doc but that doesn't seem to work. 

Also I need to change the variable so that it creates a date and time that is added to the form I am using

The code I am currently using is the same from the previous reply on the above question

Private Sub CommandButton1_Click()

Dim rngFilename As Range

 Dim strFileName As String

 Set rngFilename = ActiveDocument.Range

 With rngFilename

     .End = .Start

     .MoveEnd wdWord, 4

     strFileName = .Text

 End With

 ActiveDocument.SaveAs "shared drive location\ " & strFileName

End Sub

From what I gather the 4 at the end of .MoveEnd wdWord 4 is the start of the file name but what if I need it to be further on down the form for example two words together on separate lines of the form, (e.g. word 13 and word 9) when I try to do this using the code above I am getting the following error

Runtime error 5096

Can someone help me with the code so that I can save the form as a word doc and save it automatically as the answers within the form?

Thanks

Mark

Microsoft 365 and Office | Word | For home | Windows

Locked Question. This question was migrated from the Microsoft Support Community. You can vote on whether it's helpful, but you can't add comments or replies or follow the question.

0 comments No comments
Answer accepted by question author
Jay Freedman 208.6K Reputation points Volunteer Moderator
2019-12-17T15:00:07+00:00

The important part is exactly where and how your user saved the template. Microsoft Office is strict about security because hackers love to attack you with malware, so users have to jump through a few hoops to make macros work. 

First, any Office file sent from one computer to another is marked by Windows and has to be manually unmarked. After saving the file on the local drive, open its Properties dialog from the right-click menu and check the Unblock box. 

The second requirement is to satisfy or bypass Office's security checks. To satisfy them, a security certificate has to be applied to the file, something that isn't available to most people. To bypass the checks, the template file must be stored in a 'trusted location', a folder that is designated as one where all the files are known to be good. By default, the Office installer marks the User Templates, the Program Templates, and the Word Startup folders as trusted. This can be seen in File > Options > Trust Center > Trust Options > Trusted Locations. If the template wasn't initially saved in the Templates folder, it should be moved there before basing a document on it.

Was this answer helpful?

4 people found this answer helpful.
0 comments No comments
Answer accepted by question author
Charles Kenyon 172K Reputation points Volunteer Moderator
2019-12-18T17:43:45+00:00

I believe there is information about (1) requiring information and (2) reloading the information from the form into the UserForm on one of these if not both:

I'm on my way out the door. If these don't help and no one else supplies how to do this, I will follow up when I return.

Was this answer helpful?

1 person found this answer helpful.
0 comments No comments

16 additional answers

Sort by: Most helpful
  1. Doug Robbins - MVP - Office Apps and Services 323.8K Reputation points MVP Volunteer Moderator
    2019-12-11T02:22:51+00:00

    The form with the code in it needs to be saved as a Macro Enabled Template (.dotm) and then that template needs to be distributed to the users for them to save it in the User Templates folder.  Then, when they need to make use of the form, they should use File>New and select the template as the basis of the document that they want to create.

    If when you save the form as a macro enabled template, the code is not contained in it, you must have the code in another location, possibly in your Normal.dotm template.

    Was this answer helpful?

    1 person found this answer helpful.
    0 comments No comments
  2. Anonymous
    2019-12-10T08:59:47+00:00

    Thanks again Jay its really appreciated.  I am getting some syntax errors when the code mentions msgbox prompts?

    I have updated the save location and amended the date format as I need but upon testing the code I get

    Compile error

    Syntax error

    If tbl.Cell(Row:=4, Column:=2).Range.ContentControls(1).ShowingPlaceholderText Then

             MsgBox prompt:="Please select date", _

             Title:="Oops!"

    I also get it here:

    If tbl.Cell(Row:=6, Column:=2).Range.ContentControls(1).ShowingPlaceholderText Then

            MsgBox prompt:="Please select technician name", _

                Title:="Oops!"

    Am I missing something or need to space things out better?

    Thanks

    Mark

    Was this answer helpful?

    1 person found this answer helpful.
    0 comments No comments
  3. Charles Kenyon 172K Reputation points Volunteer Moderator
    2019-12-05T17:22:22+00:00

    Yes, you are appending the .text file extension to the file name.

    See the following page on the Document.SaveAs2 method.

    https://docs.microsoft.com/en-us/office/vba/api/word.saveas2?f1url=https%3A%2F%2Fmsdn.microsoft.com%2Fquery%2Fdev11.query%3FappId%3DDev11IDEF1%26l%3Den-US%26k%3Dk(vbawd10.chm158007864)%3Bk(TargetFrameworkMoniker-Office.Version%3Dv16)%26rd%3Dtrue%26f%3D255%26MSPPError%3D-2147217396

    Your rngFileName is the range of the Active Document.

    Try ActiveDocument.SaveAs2("shared drive location\ " & strFileName,wdFormatDocumentDefault)

    Was this answer helpful?

    1 person found this answer helpful.
    0 comments No comments