FORMTEXT Appearing in Word File Name Generated from Protected Form-Field Value and BookMark for VB Script - vb.net

I am using the following VB Script in a Word 2010 Doc saved as a Microsoft Word Macro-Enabled Template that is protected for form fields:
Sub SaveWithBkMarkedText()
'This code saves the Word file using the bookmark value for Maintenance Memo.
'The file is also saved to a folder in KnowHow for files related to this template.
Dim FileName As String
FileName = ActiveDocument.Bookmarks("mmn").Range.Text
'Use the C:\ code when saving the file locally
ActiveDocument.SaveAs "C:\Download\TemplatesFolders\" & FileName & ".doc"
MsgBox "Your Draft has been saved to KnowHow's Release Documentation site." & _
&vbCrLf & "The file name uses the MM that you included earlier: " & FileName, _
vbInformation + vbOKOnly, "Draft Saved to Minerva"
End Sub
The value entered into the Form Field for a FORMTEXT legacy-form object uses the Bookmark as the file name. Example, if the user enters 12345 as the value, the file is saved using this value as the filename: 12345.doc. This worked fine until a week ago when the filename is now being Prefixed with FORMTEXT 12345.doc. I have tried using this same VB script in older versions of Word on a different machine, and created from a NEW Template with the script added in from scratch, and the same issue is appearing on that machine as well. Prior to this, I was able to update my template with NO problem, but now I can't update this one FORMTEXT field without it affecting the whole file. I can update any other FORMTEXT in the template that does not use the Bookmark value as the file name, and it works. Also, I have tried changing the Bookmark Reference to another FORMTEXT object, as well as saving the file as a .DOCX and the same problem occurs regardless. What is causing the FORMTEXT to appear in the filename?

You have to un-protect the document (template) and then make the VBA programming. Once it is done, then you can protect it again (for filling forms) and you will not see the "FORMTEXT" in the filename when you run the macro.
Hope it helps.

To the OP, did you resolve this issue? I'm now having the same problem, I'm using form field text with bookmarks and using VB.net to get the bookmark.text which is now prefixed with FORMTEXT, just using a bookmark on it own and its OK, no prefix. I'm going to try and remove the first 9 characters from the result using code, a workaround, yes but it might work.

Know this is an old thread but ran into same issue. As a workaround...
Replace FORMTEXT with null "". In OP circumstance:
Dim FileName As String
FileName = ActiveDocument.Bookmarks("mmn").Range.Text
FileName = Replace(FileName, "FORMTEXT ", "")
Not a "fix" for the issue or elegant but it works.

Had the same issue. Simply delete current bookmark and add a new one. If that does not work than instead of using the following:
FileName = ActiveDocument.Bookmarks("mmn").Range.Text
try using:
Selection.GoTo What:=wdGoToBookmark, Name:="mmn"
Filename = Selection.Text

Related

Merge pdf files with VBA and Foxit

I use Foxit Phantompdf, the complete version and ACCESS.
In our program, we have to save multiple pdf files, some of them should be merged in single files when saved.
Here is the code I use;
Dim phApp As PhantomPDF.Application
Dim n1 As String
Dim n2 As String
n1 = "c:\Temp\F3769-190136-GROUPE OCÉAN.pdf"
n2 = "c:\Temp\f3769-190136-GROUPE OCÉAN- facture.pdf"
Set phApp = CreateObject("PhantomPDF.Application")
Dim phCreator As PhantomPDF.Creator
Set phCreator = phApp.Creator
***'Call phCreator.CombineFiles("c:\Temp\F3769-190136-GROUPE OCÉAN.pdf|c:\Temp\f3769-190136-GROUPE OCÉAN- facture.pdf", "c:\Temp\F3769-190136-GROUPE OCÉAN.pdf", COMBINE_ADD_CONTENTS)***
Call phCreator.CombineFiles("""c:\Temp\" & n1 & "|'" & n2 & """" & ", " & """c:\Temp\F3769-190136-GROUPE OCÉAN.pdf"""" &", COMBINE_ADD_CONTENTS)
phApp.Exit
When I try it with the complete files names (in bold) the code works perfectly.
However, when I try to use variables, I get a
"Argument not optional"
error.
Can somebody help me ?
Thanks
Your string definitions in call line is incorrect.
You have defined n1 and n2 with the c:\temp already, and in your string conversion you add this again. I do not know if this is the route cause to your issue.
Furthermore I do not know the actual needed syntax for this phcreator.combine()
But is it not possible using only:
call pHcreator.combine(n1 & "|" & n2, …
The 'Argument not option' might imply you should add another input to your pHcreator, I would guess it could have something to do with FOXIT's combine function page settings. Try adding a input variable integer at the end of the function maybe?
But the fact that it works when writing strings in clear text would maybe suggest that the string manipulations is not correct?
I'm not a vba professional, but interested in the outcome, working myself with Foxit and also want to combine with vba. I'm currently not using version 9 som I seem to be out of luck, and only upgrading it I know what I want to do is possible.
I tried it, but got the same error message. So I took advantage of the fact that the function works when the file names are in plain text. I copied the files to be merged in a temporary folder and rename them. The renamed files are the then used in the merge function. It works perfectly, but Foxit adds a table of content page, and I don't know, yet, how to remove it. Here is my solution:
Private Sub Command4_Click()
Dim addi As String 'file to be merged to main file
Dim princi As String 'main file
Dim phApp As PhantomPDF.Application
'A temporary folder, in this case c:\t2, should be present
'In this example c:\Temp is the working folder
addi = "c:\Temp\filetomerge.pdf" 'full path of file to be merged
princi = "c:\Temp\mainfile.pdf" 'full path of main file
'fadd,pdf and fmain.pdf are the temporay files used in Foxit's function
FileCopy addi, "c:\t2\fadd.pdf" 'temporary file to be merged in temporary folder
FileCopy princi, "c:\t2\fmain.pdf" 'temporary main file in temporary folder
'Merge action
Set phApp = CreateObject("PhantomPDF.Application")
Dim phCreator As PhantomPDF.Creator
Set phCreator = phApp.Creator
Call phCreator.CombineFiles("c:\t2\fmain.pdf|c:\t2\fadd.pdf", "c:\t2\fmain.pdf", COMBINE_ADD_CONTENTS)
phApp.Exit
'Save merged file in working folder under main file name
Kill princi
FileCopy "c:\t2\fmain.pdf", princi
'delete temporary files
Kill "c:\t2\fadd.pdf"
Kill "c:\t2\fmain.pdf"
End Sub

ActiveDocument.SaveAs2 saving the document but can not ever open the document once saved (only some computer models)

Currently I have a document template with macros endabled .dotm. The macro has a commandbutton in there that triggers the SaveAs2 object twice formatted as below.
Public Sub FileSaveAs()
Dim dlg As Dialog
Dim strSaveFolder
strSaveFolder = Application.Options.DefaultFilePath(wdDocumentsPath)
Application.Options.DefaultFilePath(wdDocumentsPath) = ActiveDocument.AttachedTemplate.Path
ActiveDocument.SaveAs2 (ActiveDocument.AttachedTemplate.Path & " UsersName" & " FORM234" & Format(Now(), "DD-MMM-YYYY hh mm ss AMPM") & ".docm")
End
Second Save as
Public Sub SuperSave()
Dim dlg As Dialog
Dim strSaveFolder
strSaveFolder = "I:\Form Storage\CoCopy\"
ActiveDocument.SaveAs2 (strSaveFolder & "UserName" & "Form234" & Format(Now(), "DD-MMM-YYYY hh mm ss AMPM") & ".docm")
End Sub
Now here is the interesting part that has me stumped now for about a week now. This code works but only on some computers. Older models it doesn't work on for some reason. It doesn't matter Windows 10 or Windows 7 or Version of Office itself. It doesn't work on computers that are older in models like a HP EliteDesk 1 or HP EliteDesk 2. The 1 won't work but 2 will.
I have never heard of the vba macro being affected by the model of the computer version of OS yes version of Word ofcourse but never version of model. I have googled left and right and went to documentation from 2010 (including microsoft's killing activex issue of 2014 which I already ruled out)
This is how blank I mean doesn't even open the white page underneath.
Any ideas? Or have you heard of some computer models not running vba code but having the same OS and same version of Office?
Justin, I figured it out by chance were you trying to save a dotm as a docm. If the macro tries to save the dotm as a docm the formatting for the data is two separate instances. I followed through with this and noticed it in a test I ran on the document when right clicked and opened and got the same result try saving the template as a docm document instead this should resolve your issue.
Cindy, Justin is right it does yield a proper path and handles correctly in the VBA console without the slash. In the first sub he seems to only be after the path for the folder and is saving on the next parent folder outside of the folder containing the template macro. I have seen this work elsewhere why it is not working on specific models has me at a loss.
The second seems to target the location more specifically than the second which makes since if the folder is targeting a co-worker. I am currently trying to recreate the issue but having no luck your code works perfectly on my two systems I have running, both are rather new.
Try changing the wdsaveformat to match an extension type the formatting might be handled differently on newer models (unlikely but worth a shot)
Or rewrite the vba and document on the older afflicted models to see if they handle the setup and formatting differently

Problems saving document immediately after Document Merge

I have an application where I need to merge several nearly identical documents which had been sent out to several users and received with minor changes and comments. The documents are fairly large (500K - 1M) and can contain tables and embedded graphics. The point of this is to consolidate the changes and comments from the various users into one document, keeping track of who did or said what.
The heart of this is the following procedure which uses Application.Merge.
Public Sub runMerge(files As String)
Dim i As Integer
Dim v() As String
v = Split(files, vbCr)
Word.Application.ScreenUpdating = False
Word.Documents.Open fileName:=v(0), ReadOnly:=False, addtorecentfiles:=False
For i = 1 To UBound(v)
ActiveDocument.Merge fileName:=v(i), _
MergeTarget:=wdMergeTargetCurrent, _
DetectFormatChanges:=True, _
UseFormattingFrom:=wdFormattingFromCurrent, _
addtorecentfiles:=False
DoEvents
Next i
Word.Application.ScreenUpdating = True
Word.Application.ScreenRefresh
ActiveDocument.SaveAs2 "Merge " & Replace(Now, ":", "."), , , , False
End Sub
The code opens the first file explicitly, then merges the remaining documents in a loop. Apart from being a bit fragile, this works correctly as long as the first document was explicitly opened.
Once merged, I use SaveAs2 with a new unqualified file name (i.e. no explicit directory). This works fairly well.
However, if I attempt to use SaveAs2 to place the file in a different directory, as in
ActiveDocument.SaveAs2 "C:\.....\Merge " & Replace(Now, ":", "."), , , , False
the resulting document does not re-appear on the screen after the merge. Further, when explicitly opened later, the document appears somehow damaged, strangely formatted, in one case, some of the text appeared in an orange colour. This behaviour has been seen in Word 2013 and 2016.
Apart from saving the document in whatever default directory (My Documents, in my case), then explicitly moving the file in VBA using copy and Kill and what not, how would one save this without an extra step?
In the end, I did the following to solve the save problem.
Using the super-easy FileCopy VBA function, I copied the first file in the merge group to the directory where I wanted the merged file to end up.
The merge loop remains unchanged.
When the merge loop is done, I used the solution from an earlier post, VBA word table copy loses data, ActiveDocument.Select followed by Selection.Collapse.
ActiveDocument.Save, not SaveAs

Activate window of another Visio instance

Currently I have a file name stored in string called filename. The file stored in the string is currently open. Issue is, this file could some times be opened in another instance of Visio.
I want to activate the file that is stored in filename string
My current method does not capture this - The code below only checks if the filename exists among the current/one instance of Visio.
For Each objDoc In objVisio.Documents
If objDoc.Name = filename Then
objDoc.activate
Exit for
End If
Next
How can I activate this file to bring it forward?
windows(filename & " - Microsoft Visio").activate
is not working either
I've tried
Dim objVisio as Visio.Application
Set objVisio = GetObject(filename).Application
which isn't working (maybe due to filename string only having the file name and not the entire file path as well)
Any other brute force methods available out there?
Any help is appreciated!
Try something like this:
objVisio.Application.Caption
Or
AppActivate "Microsoft Visio"
I guess another option is to look into this: https://msdn.microsoft.com/en-us/library/office/ff766749.aspx
I haven't worked extensively with Visio in VBA, so I am interested to see the true answer here.

Solidworks VBA save as new name

I am currently generating a solidworks part file name from a text document for a batch "rename" of the documents. This assigns sequential numbers to them which i then save each part in a folder as. I have been trying to figure out a method for the new file names to be referenced in the assembly they were a part of.
For saving the document I am using :
swModel.SaveAs3 "" & FileName & "", 0, 0
I have no issue creating all the files in a batch but I could not seem to find a method of apply this save in such a way that any open documents that it is referenced in change to referencing the new part name.
Edit: I figured out a serviceable solution using some of the information held in this link:
help.solidworks
Using the pack and go functionality in solidworks avoids having to deal with the references as they are already handled by the process itself Hope this helps.
Try the below approach:
Dim FileName As String
FileName = "blablabla" & Variable & "blablabla"
swModel.SaveAs3 ("C:\User\Username\Desktop" + FileName + ".SLDPRT", 0, 1)
Variable = Variable + 1
'continue with your loop