catia vba change activedocument (drawingdocument) name in session - vba

I have a .CATdrawing template that I use to create drawings for all my parts.
My current macro opens the template as desired.
What I would like to do is to change the working name of the document so that when the user clicks "save" the correct name is already in the dialog box and he only needs to browse to the correct location.
To clarify I'll add an image: https://i.imgur.com/eckBwRQ.png
In this image the text "Drawing2" needs to change to whatever I want it to be.
I do not want to save the .CATdrawing at this moment, the user must be allowed to continue work and save the document when it suits him.
I've been doing some searches on google and in the V5Automation.chm but I can't seem to find the code that does this.
My best guess was to try the following code:
'remember currently opened part.
Dim partDoc As PartDocument
Set partDoc = CATIA.ActiveDocument
Dim documents1 As Documents
Set documents1 = CATIA.Documents
'Open drawing
Dim mydrawingdoc As DrawingDocument
Set mydrawingdoc = documents1.Open("X:\Path\Template.CATDrawing")
'rename drawing
Set CATIA.ActiveDocument.Name = partDoc.Part.Parameters.Item("CUSTOM_NAME").ValueAsString
However I get an error saying
Invalid use of property
Any help would be greatly appreciated.
EDIT:
I've tried a few more things to do this but as of yet I've seen no success:
Left(mydrawingdoc.FullName, 10) = partDoc.Part.UserRefProperties.Item("CUSTOM_NAME").ValueAsString
this won't work either
EDIT2:
Ok so I found something that will give a completely different error:
mydrawingdoc.FullName = partDoc.Part.UserRefProperties.Item("CUSTOM_NAME").ValueAsString
Can't assign to read-only property
Does this mean it's impossible to do what I want?
Could a possible solution be to use NewFrom instead of Open like this:
Set mydrawingdoc = documents1.NewFrom("path\Template.CATDrawing")
And then immediately use the desired name while creating this drawingdocument?

It is not possible to change the name of a top-level document that has not been saved yet.
The best and only workaround, as far as I could find, is saving the document with the correct name in a temporary folder.
Example:
'remember currently opened part.
Dim partDoc As PartDocument
Set partDoc = CATIA.ActiveDocument
Dim documents1 As Documents
Set documents1 = CATIA.Documents
'Open drawing
Dim mydrawingdoc As DrawingDocument
Set mydrawingdoc = documents1.NewFrom("path\Template.CATDrawing")
'Save drawing with custom name extracted from 3D part
CATIA.ActiveDocument.SaveAs ("C:\CATIA_temp\" & partDoc.Product.UserRefProperties.Item("CUSTOM_NAME").ValueAsString & ".CATDrawing")

Related

How to check pdf check box

I am trying to read one pdf and a VBA userform and then fill out another pdf.
I wrote code to read all text in a pdf and then find certain sub strings based on tokens that I can find in the string. It is intended to populate the fields in the destination pdf based on the substrings and check the appropriate text boxes based on the user form. I can get the code to fill the substrings and then save the document, but it won't check the boxes.
Before the code used a AVDoc, but I switched to a JSO because I don't want the pdf to pop up, and the jso avoids that problem.
I tried pdfBool.value = cBool(vbaBool), pdfBool.value = 1, pdfBool.value = "1", jso.setValue("checked"), jso.setValue("yes"), etc.
This code will run without crashing. I reduced the number of variables to one string and one bool for the sake of the example.
Sub main()
‘findString grabs all text from a pdf file. This code works.
Dim mystr As String
If findString(mystr) = False Then
Application.StatusBar = "Cannot find Source PDF"
Exit Sub
End If
Dim mypath As String
mypath = ActiveWorkbook.Path & "\destination.pdf"
Dim aApp As acrobat.AcroApp
Dim pdfDoc As acrobat.CAcroPDDoc
Dim jso As Object
Set aApp = CreateObject("AcroExch.App")
Set pdfDoc = CreateObject("AcroExch.PDDoc")
If pdfDoc.Open(mypath) = True Then
Set jso = pdfDoc.GetJSObject
Dim vbaText As String
Dim vbaBool As String
vbaText = returnString("Token1")
vbaBool = userForm.checkBox1.value
Dim pdfText As Object
Dim pdfBool As Object
Set pdfText = jso.getField("TextField1")
Set pdfBool = jso.getField("CheckBox1")
pdfText.Value = vbaText
pdfBool.Value = vbaBool
'save pdffile
Dim fileSavePath As String
fileSavePath = ActiveWorkbook.Path & "\My Save File.pdf"
pdfDoc.Save PDSaveFull, fileSavePath
'clean up memory
Set pdfDoc = Nothing
Set pdfText = Nothing
Set pdfBool = Nothing
Set jso = Nothing
End If
aApp.Exit
Set aApp = Nothing
Unload userForm1
End Sub
Ok, so after some searching, I have found a solution. Basically, forms created using Living Cycle don't work well with checkboxes. I asked somebody in my organization and they confirmed that Living Cycle was used on forms for a while until we got rid of it. Honestly, I don't know what Living Cycle is, but the solution seemed to work and so I think whatever the issue was related to something called "Living Cycle".
The solution? Redo the pdf form: I exported the pdf to an Encapsulated PostScript file. This stripped away all the fields. After that, I used the prepare form tool which automatically found all the relevant fields. Fortunately, with my pdf, it found all of the fields perfectly, though there was one or two extra ones that I had to delete. The field names and the code need to match so adjustments need to either be made to the PDF or to the code, but once I made that adjustment, everything was perfect.
Try jso.getfield(pdfFieldName).Value = "Yes" or "No". The value is case sensitive so you have to use Yes or No.

VBA for Visio 2013 to save as SVG

I need a macro which will allow me to save the current Visio drawing as an SVG file.
The quickest way I can do at the moment is to use the F12 keyboard shortcut which gives me the Save As dialog, but still each time I have to select the proper output file, i.e. PNG, and then write the name of the file.
Is it possible to automate this? I was looking for something like Macro recording in Visio, but couldn't find that.
For file formats of .bmp, .dib, .dwg, .dxf, .emf, .emz, .gif, .htm, .jpg, .png, .svg, .svgz, .tif, or .wmf
The extension will define the format.
Dim vsoPage As Visio.Page
Set vsoPage = ActivePage
vsoPage.Export ("C:\\myExportedPage.svg")
Here is an example of looping all pages exporting each one.
Dim PgObj As Visio.Page
Dim Pgs As Visio.Pages
Dim filename As String
Dim PgName As String
Dim iPgs As Integer
'Set a handle to the pages collection
Set Pgs = Application.ActiveDocument.Pages
'Loop Pages collections
For iPgs = 1 To Pgs.Count
'Set a handle to a page
Set PgObj = Pgs(iPgs)
'Get Page name
PgName = PgObj.Name
'Create path to save svg file
filename = Application.ActiveDocument.Path & PgName & ".svg"
'Export the page as svg file
PgObj.Export filename
Next iPgs
'Clean Up
Set PgObj = Nothing
Set Pgs = Nothing

VB.Net saving word document without prompt

Thanks in advance for any help you can give me.
I'm trying to create a VB application that will open an existing Word document, make some changes and save it with a new file name. Making the changes to the document is easy. Saving the document seems like it should be just as easy but there is one serious problem. When I try to save the document, the save as dialog opens. This is supposed to be automated so that doesn't work. I have tried a whole bunch of variations of:
Sub Main()
Dim oWord As Word.Application
Dim oDoc As Word.Document
Dim fileName As String
Dim templateName As String
Dim newFileName As String
'Start Word and open the document template.
oWord = CreateObject("Word.Application")
oWord.Visible = False
oWord.DisplayAlerts = Word.WdAlertLevel.wdAlertsNone
templateName = "C:\tmp\template.dotx"
fileName = "C:\tmp\document.docx"
newFileName = "C:\tmp\new document.docx"
oDoc = oWord.Documents.Add(templateName)
'oDoc = oWord.Documents.Open(fileName) I have tried both using a template and opening a docx file.
<make changes>
oDoc.SaveAs2(newFileName)
oWord.Documents.Close(Word.WdSaveOptions.wdDoNotSaveChanges)
oWord.Application.Quit()
oWord = Nothing
End Sub
It always stops here:
oDoc.SaveAs2
and opens the save as dialog. I need to find a way to suppress the save as dialog or a new way of editing the word files. Everything I have found so far about the problem has either not been solved/updated or was related to Word addins. I don't have any of the addins that people said caused the problem. To be safe, I disabled all word addins.
I would appreciate it if anyone has either solved it or has a different approach. I'm not stuck on the idea of using VB. The only reason I'm going this route is because I think it gives me the best library for editing charts and formatting the documents.
Thanks, Steve
I found the problem. The answer lies in what I was doing in the part I omitted where I was making changes to the document. Part of those changes are to update data in the charts in the document. I had something like:
For Each oShape As Word.InlineShape In oDoc.InlineShapes
If oShape.HasChart And oShape.Range.Bookmarks.Item(1).Name = "ChartName" Then
Console.WriteLine("Updating ChartName")
Dim oWorkbook As Excel.Workbook
oWorkbook = oShape.Chart.ChartData.Workbook
oWorkbook.Worksheets(1).Range("B2").FormulaR1C1 = "9"
oWorkbook.Worksheets(1).Range("B3").FormulaR1C1 = "5"
oWorkbook.Worksheets(1).Range("B4").FormulaR1C1 = "1"
oWorkbook.Application.Quit()
End If
Next
I changed it to:
For Each oShape As Word.InlineShape In oDoc.InlineShapes
If oShape.HasChart And oShape.Range.Bookmarks.Item(1).Name = "ChartName" Then
Console.WriteLine("Updating ChartName")
Dim oWorkbook As Excel.Workbook
oWorkbook = oShape.Chart.ChartData.Workbook
oWorkbook.Worksheets(1).Range("B2").FormulaR1C1 = "9"
oWorkbook.Worksheets(1).Range("B3").FormulaR1C1 = "5"
oWorkbook.Worksheets(1).Range("B4").FormulaR1C1 = "1"
oShape.Chart.Refresh()
oWorkbook.Close(True)
End If
Next
Now it works
myDoc.SaveAs(fileNameAndPath)
Works for me in Word 2007

Opening a Lotus Note draft with Excel VBA, in order to replace just values

I'm trying to write a Excel VBA code which allows me to automatically create and send a Lotus Notes Email. The problem I face is the difficulty to create a rich text Email, so I think it would be easier to open a draft email, with a marker text which will be replaced (for exameple PASTE EXCEL CELLS HERE) and then just:
.GotoField ("Body")
.FINDSTRING "PASTE EXCEL CELLS HERE"'
and replace.
Any help on how to open a certain draft email? Perhabs something as .CreateDocument property?
Thank you very much!
Others have proposed interesting concepts, but the most robust approach would be to use HTML in a MIME enitity that is mapped to the Body Rich Text item. Using NotesSession..Convertmime = False you can build the body as HTML and then send the message. Based on the post by Joseph Hoetzl here, the LotusScript equivalent is this:
Sub Initialize()
Dim s As New NotesSession
Dim db As NotesDatabase
Dim stime as Single
Dim alog As New NotesLog("debug")
Call alog.OpenAgentLog()
stime = Timer
On Error GoTo eh
Dim doc As NotesDocument
Dim body As NotesMIMEEntity
Dim header As NotesMIMEHeader
Dim stream As NotesStream
Dim child As NotesMIMEEntity
Dim sendTo As String
Dim subject As String
s.Convertmime = False
sendto = s.Effectiveusername
subject = "Demo Message"
Set db= s.Currentdatabase
Set doc=db.Createdocument()
Set stream = s.CreateStream
Set body = doc.CreateMIMEEntity
Set header = body.CreateHeader({MIME-Version})
Call header.SetHeaderVal("1.0")
Set header = body.CreateHeader("Content-Type")
Call header.SetHeaderValAndParams({multipart/alternative;boundary="=NextPart_="})
'Add the to field
Set header = body.CreateHeader("To")
Call header.SetHeaderVal(SendTo)
'Add Subject Line
Set header = body.CreateHeader("Subject")
Call header.SetHeaderVal(subject)
'Add the body of the message
Set child = body.CreateChildEntity
Call stream.WriteText("<h1>Demo HTML Message</h1>")
Call stream.WriteText(|<table colspacing="0" colpadding="0" border="none">|)
Call stream.WriteText(|<tr><td>cell 1.1</td><td>cell 1.2</td><td>cell 1.3</td></tr>|)
Call stream.WriteText(|<tr><td>cell 2.1</td><td>cell 2.2</td><td>cell 2.3</td></tr>|)
Call stream.WriteText(|<tr><td>cell 3.1</td><td>cell 3.2</td><td>cell 3.3</td></tr>|)
Call stream.WriteText(|</table>|)
Call stream.WriteText(|<div class="headerlogo">|)
Call stream.WriteText(|<!-- ...some more HTML -->|)
Call child.setContentFromText(stream, {text/html;charset="iso-8859-1"}, ENC_NONE)
Call stream.Truncate 'Not sure if I need this
Call stream.Close
Call doc.CloseMIMEEntities(True)
Call doc.replaceItemValue("Form", "Memo")
Call doc.Send(False, sendTo)
es:
Exit Sub
eh:
Dim emsg$
emsg = Error & " at " & Erl & " in " & s.Currentagent.name
Call alog.logError(Err, emsg)
MsgBox "ERROR: " & Err & ": " & emsg
Resume es
End Sub
All of this should convert fairly easily to VBA in Excel. You can, of course be as complex as you want with your HTML.
The word "draft" is probably inappropriate here, but then again so is the word "template". Both have specific meanings in Lotus Notes that aren't what you really want. Users can delete drafts, and templates are an entirely different thing. So let's just call it a boilerplate message.
I would recomemnd creating a special mail database (NSF file) on the Domino server, which will just serve as a repository for your boilerplate. You can create a folder in that mail database called "Boilerplates". Using Domino Designer you can modify that folder's design so that the Subject column is the first column in the view, and it is sorted.
Once you have that done and you have created some boilerplates and saved them in the folder, you can use VBA to do a NotesSession.getDatabase call, NotesDatabase.getView call (this is used for folders as well as views), and then use NotesView.getDocumentByKey() to retrieve a specific boilerplate by the Subject you have assigned to it. Note that you do not have to copy this document to the user's mail database in order to mail it.
What you want to do is non-trivial, but you mention a draft email so there may be a workaround.
In your mail settings you can specify a signature file which can be an external html file on disk. So modify the signature file, then create your new mail which will then populate the body field the way you want it.
For sample code, within the memo form there should be a button to specify what signature file to use. You can use that as the baseline.
Rich text is not that hard to work with, but you need to look at the Domino Designer help, especially the classes NotesRichTextItem and NotesRichTextStyle. You also need to understand the DOM (Domino Object Model). Then you can create your mail content programatically.
Otherwise I think Richard's solution is the best, that you have a separate database where you get the rich text snippets, and use the AppendRTItem method of the NotesRichText class to put it into your email.
Thank you all guys!
But I found exactly what I wanted, without having to recreate the whole Email everytime:
Sub EditSelectedMail()
Dim NSession As Object
Dim NDatabase As Object
Dim NUIWorkspace As Object
Dim NUIdoc As Object
Set NSession = CreateObject("Notes.NotesSession")
Set NUIWorkspace = CreateObject("Notes.NotesUIWorkspace")
Set NDatabase = NSession.GetDatabase("", "")
If Not NDatabase.IsOpen Then NDatabase.OPENMAIL
Set NUIdoc = NUIWorkspace.EDITDOCUMENT(True)
With NUIdoc
'Find the marker text in the Body item
.GotoField ("Body")
.FINDSTRING "**PASTE EXCEL CELLS HERE**"
'Copy Excel cells to clipboard
Sheets("Sheet1").Range("A1:E6").Copy
'Create a temporary Word Document
Set WordApp = CreateObject("Word.Application")
WordApp.Visible = False
WordApp.Documents.Add
'Paste into Word document and copy to clipboard
With WordApp.Selection
.PasteSpecial DataType:=10
'Enum WdPasteDataType: 10 = HTML; 2 = Text; 1 = RTF
.WholeStory
.Copy
End With
'Paste from clipboard (Word) to Lotus Notes document
.Paste
Application.CutCopyMode = False
'WordApp.Quit SaveChanges:=False
Set WordApp = Nothing
End With
End Sub
I just select my "template", copy it into a new message, and then run the macro.

Add Word docs to Word App without saving to Filesystem

I'm trying to create word documents on the fly in VB.NET, and I've found that the documentation on all of this seems very scarce. Right now my program works by looping through a database table, and for each row it pulls out the variables. The program then loops through and parses a template word doc, replacing variables in that template with variables from the database, then saving as a new doc.
Instead, for every "new doc" that I would be creating, I want to just append it onto another doc, that way when it comes time to parse 1000 rows in the table, I don't have to create and save 1000 different word documents to the filesystem.
I can't seem to find anything out there about this. Everything mentions merging documents (I actually want to append, not merge) or the only way I can append ("Insert") a document is by using WordApplication.Selection.InsertFile(newWordDocument.FullName). This works, but requires me to save newWordDocument to the filesystem before inserting it.
Is there a way to, while still in memory, add newWordDocument to my WordApplication object?
Here's the pseudocode of what I have now
For each row in TableOfVariables
Dim WordApplication as New Word.Application
Dim tempDoc as New Word.Document
tempDoc = WordApplication.Documents.Add
tempDoc = fillVariablesOfTheDocument(tempDoc, row)
tempDoc.Save() 'This is the problem - it saves as a new file rather than appending into WordApplication
Next
You didn't make an assignment, just a statement with
objWordApp.Documents.Add(tempDoc) 'This is where the problem is!
Dim wordApp as new Microsoft.Office.Interop.Word.Application
wordApp.Visible = true
Dim doc as Word.Document
doc = wordApp.Documents.Add
and you don't have to save it until you are good and ready. This also works
Dim Word As Word.Application
Dim Doc As Word.Document
'Start Word and open the document template.
Word = CreateObject("Word.Application")
Word.Visible = True
Doc = Word.Documents.Add
from http://support.microsoft.com/kb/316383
Took a while, but somehow I found out about MailMerge, which I had no idea existed. Long story short, I can have a template document with variables, and I can have an input excel document. The excel document will have a header row which contains the variable names, and each individual cell of the excel doc will represent the variable that will go into the document. When I execute MailMerge, the template will be replaced with n amount of pages (where n is the number of excel rows). This solves my problem of adding multiple documents to 1 big excel document. I tested this with 1,000 rows in my excel doc, and it worked flawlessly.
Here's the code I ended up with:
Dim wrdSelection As Word.Selection
Dim wrdMailMerge As Word.MailMerge
Dim wrdMergeFields As Word.MailMergeFields
' Create an instance of Word and make it visible.
wrdAppMailMrg = CreateObject("Word.Application")
wrdAppMailMrg.Visible = True
' Add a new document.
wrdDocMailMrg = wrdAppMailMrg.Documents.Open("Template.docx")
wrdDocMailMrg.Select()
wrdSelection = wrdAppMailMrg.Selection()
wrdMailMerge = wrdDocMailMrg.MailMerge()
'Open Data Source
wrdDocMailMrg.MailMerge.OpenDataSource("InputVariables.xlsx", SQLStatement:="SELECT * FROM [Sheet1$]")
' Perform mail merge.
wrdMailMerge.Destination = _
Word.WdMailMergeDestination.wdSendToNewDocument
wrdMailMerge.Execute(False)
' Close the original form document.
wrdDocMailMrg.Saved = True
wrdDocMailMrg.Close(False)
' Release References.
wrdSelection = Nothing
wrdMailMerge = Nothing
wrdMergeFields = Nothing
wrdDocMailMrg = Nothing
wrdAppMailMrg = Nothing
Now I just have to tweak it to make it run faster and clean up the UI, but the functionality is completely there.