How to avoid runtime error 5152 when trying to add a URL as shape to a Word document - vba

I am trying to place a QR code generated through an API (api.qrserver,com) in a Word table using VBA. For certain reasons, the option of simply using "DisplayBarcode" is not possible.
This is the call to the API:
sURL = "https://api.qrserver.com/v1/create-qr-code/?data=" & UTF8_URL_Encode(VBA.Replace(QR_Value, " ", "+")) & "&size=240x240"
It seems to work well. I tried with a GET command and retrieved a string that - as I interpret - contains the QR code in png format.
Now, when I try to add the picture as a shape using
Set objGrafik = ActiveDocument.Shapes.AddPicture(sURL, True)
the call fails with runtime error 5152. As far as I could determine until now, the Addpicture method expects a pure filename and does not allow any of the following characters: /|?*<>:".
I also tried to store the GET result in an object variable:
Set oQRCode = http.responseText
but there I get the error "object required".
Research on the internet regarding a solution to either make the URL assignment work or to store the result as a picture didn't retrieve any useful results. Thanks in advance for your support

I am not sure that any of the ways you could insert something into Word (e.g. Shapes.AddPicture, InlineShapes.AddPicture, Range.InsertFile etc. will let you do that from any old https Url, although it seems to work for some Urls.
However, as it happens, you can use an INCLUDEPICTURE field to do it. FOr example
{ INCLUDEPICTURE https://api.qrserver.com/v1/create-qr-code/?data=Test&size=100x100 }
Here's some sample VBA to do that
Sub insertqrcode()
Dim sUrl As String
Dim f As Word.Field
Dim r1 As Word.Range
Dim r2 As Word.Range
' This is just a test - plug your own Url in.
sUrl = "https://api.qrserver.com/v1/create-qr-code/?data=abcdef&size=100x100"
' Pick an empty test cell in a table
Set r1 = ActiveDocument.Tables(1).Cell(5, 4).Range
' We have to remove the end of cell character to add the field to the range
' Simplify
Set r2 = r1.Duplicate
r2.Collapse WdCollapseDirection.wdCollapseStart
Set f = r2.Fields.Add(r2, WdFieldType.wdFieldIncludePicture, sUrl, False)
' If you don't want the field code any more, do this
f.Unlink
' check that we have a new inline shape that we could work with more
' if necessary
Debug.Print r1.InlineShapes.count
Set f = Nothing
Set r2 = Nothing
Set r1 = Nothing
End Sub
Using INCLUDEPICTURE works even on the current version of Mac Word (although I haven't tested that specific piece of VBA on Mac).
The only other way I have seen to do it uses WinHTTP to get the result from the web service, ADODB to stream it out to a local disk file, then AddPicture or whatever to include that file, so much more complicated and won't work on Mac either.

Related

How can I discover an "object access flag" that is apparently needed when deleting a Quickbooks invoice data entension using QBSDK

I am using the QuickBooks QBFC12 SDK, and specifically, I am attempting to delete a "data extension" on an invoice in QB.
Some of the time the following VB.NET code will work, but many times it doesn't:
Dim objRequest As IMsgSetRequest
Dim objDatExtAdder As IDataExtAdd
Dim objResponse As IMsgSetResponse
Dim objOurResponse As IResponse
Dim objInvoiceQuery As IInvoiceQuery
Dim objInvoiceRetList As IInvoiceRetList
Dim objInvoiceRet As IInvoiceRet
Dim szInvoiceTxnID As String = ""
Dim objDataExtDel As IDataExtDel
' Check to see if the invoice is already on file.
objInvoiceQuery = objRequest.AppendInvoiceQueryRq
objInvoiceQuery.ORInvoiceQuery.InvoiceFilter.MaxReturned.SetValue(1)
objInvoiceQuery.IncludeLineItems.SetValue(True)
objInvoiceQuery.IncludeLinkedTxns.SetValue(True)
objInvoiceQuery.ORInvoiceQuery.InvoiceFilter.ORRefNumberFilter.RefNumberFilter.MatchCriterion.SetValue(ENMatchCriterion.mcEndsWith)
objInvoiceQuery.ORInvoiceQuery.InvoiceFilter.ORRefNumberFilter.RefNumberFilter.RefNumber.SetValue(szQBInvoiceNumber)
objResponse = objSessionManager.DoRequests(objRequest)
objOurResponse = objResponse.ResponseList.GetAt(0)
If objOurResponse.StatusCode = 0 Then
' Lock onto the invoice.
objInvoiceRetList = objOurResponse.Detail
If objInvoiceRetList.Count > 0 Then
' The invoice already exists.
objInvoiceRet = objInvoiceRetList.GetAt(0)
szInvoiceTxnID = objInvoiceRet.TxnID.GetValue
End If
End If
objRequest.ClearRequests()
' Remove any previous value.
objRequest = objSessionManager.CreateMsgSetRequest("US", 11, 0)
objRequest.Attributes.OnError = ENRqOnError.roeStop
objDataExtDel = objRequest.AppendDataExtDelRq()
objDataExtDel.OwnerID.SetValue("0")
objDataExtDel.ORListTxn.TxnDataExt.TxnDataExtType.SetValue(ENTxnDataExtType.tdetInvoice)
objDataExtDel.ORListTxn.TxnDataExt.TxnID.SetValue(szInvoiceTxnID)
objDataExtDel.DataExtName.SetValue(szDataExtensionName)
objResponse = objSessionManager.DoRequests(objRequest)
objOurResponse = objResponse.ResponseList.GetAt(0)
If objOurResponse.StatusCode = 0 Then
Debug.Print("Worked")
Else
Debug.Print("Didn't work")
End If
On the occasions where it reports "Didn't Work," objourresponse.StatusMessage returns:
The necessary QuickBooks object access flag was not set in the attribute definition for an attribute. QuickBooks error message: Unknown Error
I have tried to understand what the "Object Access Flag" is and where it can be found, and I have searched on Google and the Intuit developer's site for more information, but I can't find anything.
Can someone help with understanding what this is, how I can get past this issue, and how I can consistently delete this data extension whenever necessary?
After a LOT of trial and error, I believe I have struck on a solution.
The particular extension was part of a drop-down selector that I have displayed on the invoice. Some of the valid values are "Printed" or "Shipped". Formerly, when I went to change the value in the extension, I was first deleting the extension using AppendDataExtDelRq, then adding it back in with the new value using AppendDataExtAddRq.
I was able to get the value to change if, instead of using the delete/add combination, I used AppendDataExtModRq and simply changed the value.
I still think there is a bug in the SDK like #InteXX suspects, but I also noticed that IF I set the extension to a pre-defined value in the drop-down, then all is good. BUT, if I try to set the extension to a value that isn't pre-defined in QB for that extension/drop-down, then I get the exact same error:
The necessary QuickBooks object access flag was not set in the attribute definition for an attribute. QuickBooks error message: Unknown Error
So the upshot is that it is working now by using the MOD and not the DELETE/ADD approach, but make sure you set the value to a pre-defined value.

Word automation failing on server, but dev station works great

I am automating a oft-used paper form by querying the user on a web page, then modifying a base Word document and feeding that modified doc file to the user's browser for hand-off to Word.
The code is Visual Basic, and I am using the Microsoft.Office.Interop module to manipulate the document by manipulating Word. Works fine on the development system (Visual Studio 2015) but not on the production server (IIS 8.5).
Both the Documents.Open() call and the doc.SaveAs() call fail with Message="Command failed" Source="Microsoft Word" HResult=0x800A1066
Things I've tried:
Added debugging out the whazoo: Single-stepping is not an option on the production machine, so I pinpointed the problem lines with debug output.
Googled and found that this problem has been reported as early as 2007, but no viable solutions were reported.
A couple sites mentioned timing issues, so I added several pauses and retries -- none helped.
Some mentioned privileging, so I tried changing file permissions & application pool users -- neither helped.
Enhanced my exception handling reports to show more details and include all inner exceptions. That yielded the magic number 800A1066 which led to many more google hits, but no answers.
Added fall-back code: if you can't open the main document, create a simple one. That's when I found the SaveAs() call also failing.
Dropped back to the development system several times to confirm that yes, the code does still work properly in the right environment.
Greatly condensed sample code does not include fallback logic. My Word document has a number of fields whose names match the XML tokens passed as parameters into this function. saveFields() is an array of those names.
Dim oWord As Word.Application
Dim oDoc As Word.Document
oWord = CreateObject("Word.Application")
oWord.Visible = True
oDoc = oWord.Documents.Open(docName)
Dim ev As String
For i = 0 To saveFields.Length - 1
Try
ev = dataXD.Elements(saveFields(i))(0).Value
Catch
ev = Nothing
End Try
If ev IsNot Nothing Then
Try
Dim field = oDoc.FormFields(saveFields(i))
If field IsNot Nothing Then
If field.Type = Word.WdFieldType.wdFieldFormTextInput Then
field.Result = ev
End If
End If
Catch e As Exception
ErrorOut("Caught exception! " & e.Message)
End Try
End If
Next
...
oDoc.SaveAs2(localDir & filename)
oDoc.Close()
oWord.Quit(0, 0, 0)
The code should generate a modified form (fields filled in with data from the parameters); instead it fails to open, and the fallback code fails to save the new document.
On my dev system the document gets modified as it should, and if I break at the right place and change the right variable, the fallback code runs and generates the alternate document successfully -- but on the production server both paths fail with the same error.
Barring any better answers here, my next steps are to examine and use OpenXML and/or DocX, but making a few changes to the existing code is far preferable to picking a new tool and starting over from scratch.
Unfortunately, Lex Li was absolutely correct, and of course, the link to the reason why is posted on a site my company considers off limits, thus never showed up in my google searches prior to coding this out.
None of the tools I tried were able to handle the form I was trying to automate either -- I needed to fill in named fields and check/uncheck checkboxes, abilities which seemed beyond (or terribly convoluted in) the tools I evaluated ...
Eventually I dug into the document.xml format myself; I developed a function to modify the XML to check a named checkbox, and manipulated the raw document.xml to replace text fields with *-delimited token names. This reduced all of the necessary changes to simple string manipulation -- the rest was trivial.
The tool is 100% home-grown, not dependent upon any non-System libraries and works 100% for this particular form. It is not a generic solution by any stretch, and I suspect the document.xml file will need manual changes if and when the document is ever revised.
But for this particular problem -- it is a solution.
This was the closest I got to a complicated part. This function will check (but not uncheck) a named checkbox from a document.xml if the given condition is true.
Private Shared Function markCheckbox(xmlString As String, cbName As String, checkValue As Boolean) As String
markCheckbox = xmlString
If checkValue Then ' Checkbox needs to be checked, proceed
Dim pos As Integer = markCheckbox.IndexOf("<w:ffData><w:name w:val=""" & cbName & """/>")
If pos > -1 Then ' We have a checkbox
Dim endPos As Integer = markCheckbox.IndexOf("</w:ffData>", pos+1)
Dim cbEnd As Integer = markCheckbox.IndexOf("</w:checkBox>", pos+1)
If endPos > cbEnd AndAlso cbEnd > -1 Then ' Have found the appropriate w:ffData element (pos -> endPos) and the included insert point (cbEnd)
markCheckbox = markCheckbox.Substring(0, cbEnd) & "<w:checked/>" & markCheckbox.Substring(cbEnd)
End If
' Any other logic conditions, return the original XML string unmangled.
End If
End If
End Function

replace inlineshapes using word VBA

I have a word document with many images in it and I wish to be able to select a file path (in which the new images are located and numbered i.e. 1 to 100) to then replace the existing images with the new images.
I have read a few posts of others retrieving the properties of existing inlineshapes to achieve this, but I have also read people succeeding with the following method which seems much simpler (I just haven't been able to get the code working fully yet). Currently the code will run and replace the last image perfectly fine, but then stops with error '438' - Object doesn't support this property or method when it tries to replace the second last image.
The code is as follows:
Sub Replace_images()
Dim rg As Word.Range
Dim i As Long
Dim doc As Word.Document
Dim path As String
'Ensure pictures are numbered with no leading zeros (in folder) & are .jpg
path = "C:\filepathtopictures\"
Set doc = ActiveDocument
For i = doc.InlineShapes.Count To 1 Step -1
Set rg = doc.InlineShapes(i).Range
doc.InlineShapes(i).Delete
rg = doc.InlineShapes.AddPicture(path & i & ".jpg", False, True, rg)
Next i
End Sub
I don't understand how the using the addpicture doesn't work for the next image when nothing has change from the last image. If someone could please explain why it doesn't work or tell me what needs to be changed that would be great!

Visual Basic - Getting basic information about another program

Alright, I am currently working on an Updater which is updating several files such as executable and drivers. I don't want to replace every single file every time I push an update and rather just replace the file that is actually getting updated. An easy way would be reading the Version of the program but I don't know how to read the required information. I went through IO.File but didn't find anything useful.
Try this for looping through a folder's files.
Option Explicit
Sub Macro1()
Dim varFile As Variant
Dim i As Integer
Dim objShell
Dim objDir
Set objShell = CreateObject("Shell.Application")
Set objDir = objShell.Namespace("C:\DIRECTORY_OF_FILES_HERE\")
For Each varFile In objDir.Items
Debug.Print objDir.GetDetailsOf(varFile, 156) ' 156 = File Version
Next
' Use this to see the numbers of all the attributes, e.g. 156 = File Version
'For i = 0 To 300
' Debug.Print i, objDir.GetDetailsOf(objDir.Items, i)
'Next
End Sub
You could create a separate file that will indicate the version of each individual files you will be updating. This is generally how I do things.
Say you have a file for this, we'll call it "version.txt". This is ideally what you would set it up like this: (you could do a lines format or javascript, whatever you feel most comfortable with)
mainapplication.version = 2.1
So, in your updater method, you would set the latest version as something like 2.2.
Dim alatestVersion As Double
alatestVersion = 2.2
You could then parse the text file and create a method to check if the component is of the latest version or not. Following this, if mainapplicationVersion != alatestVersion then [update].
Hope this helps.

Reading, Writing and controlling Autocad using external VBA

I'm using MS-Access 2010 and Autocad 2012 64bit and work in manufacturing.
I want to be able to at the very least, populate fields in a title block, better still I would like to use data in my access database to write data into a sheet set (the current system works by reading the sheet set values such as sheet title and number into my title block).
The following code is all I have at the moment and it will open autocad and write the date into the command line.
Private Sub OpenAutocad_Click()
Dim CadApp As AcadApplication
Dim CadDoc As AutoCAD.AcadDocument
On Error Resume Next
Set CadApp = GetObject(, "AutoCAD.Application")
If Err.Number <> 0 Then
Set CadApp = CreateObject("AutoCAD.Application")
End If
On Error GoTo 0
CadApp.Visible = True
CadApp.WindowState = acMax
Set CadDoc = CadApp.ActiveDocument
CadDoc.Utility.Prompt "Hello from Access, the time is: " & TheTime
Set CadApp = Nothing
End Sub
I have no idea where to go from here. What are the commands to control the sheet set manager and change data, and can the .dst file be edited without even opening up autocad? is there a list of all available autocad vba commands and functions?
If you are declaring CadApp as AcadApplication you must have added a reference to AutoCAD.
That means you should be able to see the object model using your Object Browser in your VBA IDE. No?
There is also a very helpful site www.theswamp.org which has a whole section devoted to AutoCAD VBA.
If I understand your question correctly, you want to automate filling attributes in a drawing title blocks (such as title, drawer, part number, etc) right from MS Access.
Your code can access the Autocad command line already, but Autocad doesn't seem to have the exact command for filling drawing attribute. (command list)
So looks like you need to fill the attributes programatically using the COM API.
The following question appears to be relevant with yours and the accepted answers does provide a sample code:
Is it possible to edit block attributes in AutoCAD using Autodesk.AutoCAD.Interop?
Note that in that question the asker was developing a standalone application in C# .NET, where as you will be using VB Automation from MS Access. Shouldn't be too different since the Component Object Model (COM) being used is the same.
What are the commands to control the sheet set manager and change data and can the .dst file be edited without even opening up autocad?
(sorry can't post more than 2 links)
docs.autodesk.com/ACD/2010/ENU/AutoCAD%202010%20User%20Documentation/files/WS1a9193826455f5ffa23ce210c4a30acaf-7470.htm
No mention about data change, though.
is there a list of all available autocad vba commands and functions?
Yes.
%ProgramFiles%\Common Files\Autodesk Shared\acad_aag.chm - Developer's Guide
%ProgramFiles%\Common Files\Autodesk Shared\acadauto.chm - Reference Guide
Online version:
help.autodesk.com/cloudhelp/2015/ENU/AutoCAD-ActiveX/files/GUID-36BF58F3-537D-4B59-BEFE-2D0FEF5A4443.htm
help.autodesk.com/cloudhelp/2015/ENU/AutoCAD-ActiveX/files/GUID-5D302758-ED3F-4062-A254-FB57BAB01C44.htm
More references here:
http://usa.autodesk.com/adsk/servlet/index?id=1911627&siteID=123112
:) Half the way gone ;)
If you has a open autocad with a loaded drawing you can access the whole thing directly.
Sub block_set_attribute(blo As AcadBlockReference, tagname, tagvalue)
Dim ATTLIST As Variant
If blo Is Nothing Then Exit Sub
If blo.hasattributes Then
tagname = Trim(UCase(tagname))
ATTLIST = blo.GetAttributes
For i = LBound(ATTLIST) To UBound(ATTLIST)
If UCase(ATTLIST(i).TAGSTRING) = tagname Or UCase(Trim(ATTLIST(i).TAGSTRING)) = tagname & "_001" Then
'On Error Resume Next
ATTLIST(i).textString = "" & tagvalue
Exit Sub
End If
Next
End If
End Sub
Sub findtitleblock(TITLEBLOCKNAME As String, attributename As String,
attributevalue As String)
Dim entity As AcadEntity
Dim block As acadblcck
Dim blockref As AcadBlockReference
For Each block In ThisDrawing.BLOCKS
For Each entity In block
If InStr(LCase(entity.objectname), "blockref") > 0 Then
Set blockref = entity
If blockref.effectivename = TITLEBLOCKNAME Then
Call block_set_attribute(blockref, attributename, attributevalue)
exit for
End If
End If
End If
Next
Next
End Sub
call findtitleblock("HEADER","TITLE","Bridge column AXIS A_A")
So assume you has a title block which has the attribute TITLE then it will set the Attribute to the drawing name. it mioght also possible you has to replace the thisdrawing. with your Caddoc. I usually control Access and Excel form autocad and not vice versa ;)
consider also to use "REGEN" and "ATTSYNC" if "nothing happens"
thisdrawing.sendcommens("_attsync" 6 vblf )