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. Anonymous
    2019-12-17T13:08:42+00:00

    Thanks once again for the replies.  Unfortunately I am still having problems (user error most definitely)

    I have saved the word form as a DOTM file and sent it to a user.  that user has then saved it to a location within his PC.  upon opening the file again it creates a read only, already in developer mode, Document 1 version of my original.  He then clicks off of design mode (developer tab) and us unable to save/submit form although the code appears to be within this new document 1 version.  I am guessing this is because its read only?

    Am I missing something fundamental here? Doug above mentioned something about templates and opening new and finding a version of the form within the templates part in word?  How do I save something to there so that its usable and works as expected?

    I know this should be a simple task but for some reason I am missing something.... a brain perhaps :)

    Was this answer helpful?

    1 person found this answer helpful.
    0 comments No comments
  2. Anonymous
    2019-12-10T12:50:08+00:00

    Thank you all for your help this is now working perfectly

    When I save the original form with code it doesn't seem to be picking up any of the code.

    If I save it as a macro enabled word template it doesn't pick it up at all and doesn't allow macros??

    when I save it as a word template it doesn't save the code

    I need to be able to save one version of the form (master) whereby I can send it to a shared location and its used by a team of 4 to update and submit to a share (hence the coding)

    All the code works, my files are now saving in the proper format but I don't know what file type to save it as based upon my requirement above?

    any ideas?

    thanks

    Mark

    Was this answer helpful?

    0 comments No comments
  3. Anonymous
    2019-12-09T12:07:16+00:00

    The code you copied from Doug's reply in the other thread was for the situation where the intended filename is the first four words in the document. Specifically, the lines

     Set rngFilename = ActiveDocument.Range

     With rngFilename

         .End = .Start

         .MoveEnd wdWord, 4

    place the start of the range at the beginning of the document and then move the end of the range four words to the right. Then the line

        strFileName = .Text

    puts the text of the range (that is, the four words) into the string variable strFileName. 

    You have a different situation, so the code needs to be different.

    If the beginning of the text in the document that you want to use for a filename isn't at the beginning of the document, or isn't all in the same paragraph, or isn't in the order you want (guessing from your "word 13 and word 9" comment), then the macro needs some way to determine where the text begins and ends, and maybe some way to re-order the pieces. Please reply and give exact details of how to find the text and make it into the filename you want -- pretend you're doing this with pieces of paper and giving instructions to a small child. :-)

    One last bit: Because the variable strFileName contains just the words from the document text, and doesn't include an extension, Word's SaveAs command will apply the default extension; in Word 2010 that will be ".docx". That makes it a Word document, not a "text" (.txt) file, but it won't contain any macros. If you want to include the macro for the command button, you would need to replace the SaveAs line with

    ActiveDocument.SaveAs "shared drive location" & strFileName & ".docm", wdFormatXMLDocumentMacroEnabled

    Notice that there should not be a space between the backslash and the closing quote mark in either version of the line.

    Hi Jay,

    Thanks for your reply. 

    re the text I have added an image of the form where it shows the date and the technician name.  Ideally I would like the form to be saved upon the click of the command button in the format technician name.date e.g. John Smith.01.01.2020.doc.x (not sure if it would be doc.x as I am guessing ill need to save an original as a template?

     

    Was this answer helpful?

    0 comments No comments