I have Word VBA code that is supposed to create an Msxml2.DOMDocument object and load an Xml file into it:
Set XDoc = CreateObject("MSXML2.DOMDocument")
XDoc.async = False: XDoc.validateOnParse = False
XDoc.Load (XmlFileName)
This code runs on my computer.
On a coworker's computer the Load method returns False and doesn't load the data.
I tried switching from MSXML 3.0 to MSXML 6.0 by changing the first line of code to
Set XDoc = CreateObject("MSXML2.DOMDocument60")
as well as changing the project's reference to v6.0. This resulted in failure to create the object, giving the error message
"Runtime error 429: ActiveX component can't create object"
I tried reregistering the .dll files for both MSXML versions, with no effect.
What might be causing the code to run on my machine but not on others?
How might I resolve the respective errors for either v3.0 or v6.0?
I figured out what was causing this problem. The XML file I was trying to load was inside a zip file and before loading it I had to find its name using pattern matching. My coworkers computer had the "File name extensions" checkbox unchecked so the file name that VBA read didn't match the required pattern that included the .xml extension. After I checked the checkbox it worked perfectly.
Related
I am working on a script that generates a MS-project plan from inside of excel. All is working fine but I have trouble setting the header and footer. It looks like I need to identify the view name, but I constantly get a runtime error 1101 or some other errors.
I am working on a danish version of MS-project (Side=Page, Sider=Pages) and according to the title of the pagesetup menu my default view is "Gantt-diagram". Also tried with "Gantt Chart" without luck.
Dim pjapp As Object
Dim newproj As Object
Set pjapp = CreateObject("MSProject.application")
pjapp.Visible = True
Set newproj = pjapp.Projects.Add
Set ActiveProject = newproj
' here I want to remove the legend (does not work)
pjapp.FilePageSetupLegendEx Name:="Gantt-diagram", LegendOn:=pjNoLegend
' here I want to set the footer
pjapp.FilePageSetupFooter Alignment:=pjRight
pjapp.FilePageSetupFooter Text:="&[Side] of &[Sider] just some text here"
' setting page to A3 format - this somehow works
pjapp.FilePageSetupPage PaperSize:=8
' here I want to setup the header (does not work)
pjapp.FilePageSetupHeader Name:="Gantt-diagram", Alignment:=pjRight, Text:="My header"
Here are a few things to try:
It looks like you are using early binding (otherwise intrinsic constants like pjNoLegend would cause a debug error), so declare the Project object as their native type (e.g. Dim pjapp As MSProject.Application).
Use FilePageSetupLegend instead of FilePageSetupLegendEx.
Call FilePageSetupHeader using positional arguments rather than the names and skip the view name argument (it will default to the current view). For example: pjapp.FilePageSetupFooter , pjRight, "&[Side] of &[Sider] just some text here"
ActiveProject is a reserved word in MS Project so use a different variable name there or just delete that line of code since you already have an object variable, newproj, and it is already the active project.
Note: To use early binding (easier), include a reference to the Microsoft Project Object Library. To do this, in the VB Editor, go to Tools: References and check the appropriate reference (it might be listed as "Microsoft Office Project Object Library" and will include the version number--I'm using 2013 which is v15.0). Also make sure there aren't any incorrect references checked (e.g. references to the wrong version).
I have a Word .dot file which works in older versions of Word but fails with error 432 when run in Word 2013.
When I debug the code I have the line:
Load customerForm
And VBA shows the error:
Run-time error '432': File name or class name not found during Automation operation
The project "pennyscode" includes "Module1" which contains the function being debugged, "ThisDocument" and a form called "customerForm".
I have tried changing the name to "pennyscode.customerForm" but this doesn't make any difference.
This code is being called from a Sub function which is called from Document_New().
Updates
I can place a breakpoint on the Load customerForm line and demonstrate that it is the line that is causing the problem. If at this point I mouse over the word "customerForm" VBA comes up with
customerForm = <Object variable or With block variable not set>
If I delete/skip the Load line then the next line is customerForm.Show and that produces the same error.
If I just open the .dotm file and then use Alt-F11 to open VBA, I can look at the code for selectCustomer, list properties/methods and customerForm appears in the list.
Additional Note
I believe that within the Load function it must be calling GetObject and it is this that is failing. It is as if VBA can't find the customerForm object even though it appears in the project.
I've posted the full code of the function being called from Document_New below.
Sub selectCustomer()
Dim Doc As Document
Set Doc = Application.ActiveDocument
If Doc.CustomDocumentProperties.Item("Customer") = "Nothing" Then
Load customerForm
customerForm.Show
Unload customerForm
Doc.Fields.Update
a$ = Doc.CustomDocumentProperties.Item("InvoiceNumber")
a$ = customerForm.pathBox.Value + "\" + a$
Doc.SaveAs (a$)
End If
End Sub
I've also posted the full .dotm (Excel 2013) and .dot (previous excel) and some sample data (.xls) here:
Dropbox/Public/Invoice 2015-16.dotm
Dropbox/Public/Invoice 2015-16.dot
Dropbox/Public/data.xls
Update
I've not had much luck making progress on this question. Can anyone suggest an approach to investigating this? Or how I might improve the information on the question?
I finally managed to fix this, and I have a few learnings.
Firstly the debugger shows the error as occurring on the Load customerForm line, but this is actually not the case.
The customerForm has an _Initialize function which loads data into it before it is displayed. This function was failing with but the debugger stops on the wrong place.
I was able to debug this more effectively by putting a breakpoint on the start of the _Initialize sub and then stepping through the code.
Once I had discovered this I realized that the code was failing to find the XLSX file due to a wrong path, thus causing the run-time error.
Once I'd fixed up all the paths, I then hit a second error: runtime error '9' which is a subscript problem. This also reported on the Load customerForm line and was also due to a problem with the _Initialize function.
This was the true source of the problem, and demonstrated a functional change between Office 2013 and previous versions of Office.
My code was opening an XLSX file and attempting to read data from it:
Dim myXL As Object
Dim myWS As Object
Set myXL = GetObject("C:\Test\data.xlsx")
myXL.Application.Visible = True
myXL.Parent.Windows(1).Visible = True
Set myWS = myXL.Application.Worksheets("Customers")
The run-time error 9 was due to the index of the Windows property, as their were no windows. In previous versions of Office, there was a single window, with 2013 the array is empty.
After much messing about I tried adding this line:
myXL.Activate
before accessing the Windows() array. Once that was executed Windows(1) existed and the code worked as before.
Hope this can help someone else struggling with similar problems.
I'm creating a html page that will import an excel file in to a tracking system. On a button click event excel file is located / ssis package is fired / data imported then closed out. Thats the idea work flow. Problem is the excel file access is being denied before the package even executes
Here is the exact error :
I've tried :
excel file properties have been shared to everyone
identity impersonate set to true
hard coding the path
here is the VB code
Protected Sub bntExecute_Click(sender As Object, e As EventArgs) Handles btnExecute.Click
Dim app As Application = New Application()
Dim package As Package = Nothing
'Dim fileName As String = "C:\Users\Desktop\T. Bryant III\PTSID_Update_Template"'
Try
Dim fileName As String = Server.MapPath(System.IO.Path.GetFileName(FileUpload1.PostedFile.FileName.ToString()))
FileUpload1.PostedFile.SaveAs(fileName)
package = app.LoadPackage("#C:\Users\Desktop\T.Bryant III\KitImport", Nothing)
'excel connection from package'
package.Connections("SourceConnectionExcel").ConnectionString = "provider=Microsoft.Jet.OLEDB.4.0data source =" + fileName + "Extended Properties = Excel 8.0"
'Execute the pakage'
Dim results As Microsoft.SqlServer.Dts.Runtime.DTSExecResult = package.Execute()
Catch ex As Exception
Throw ex
Finally
package.Dispose()
package = Nothing
End Try
End Sub
Thanks in advance or if there is an easier way to do this please let me know. The package when executing it in ssis works fine with its own connection manager etc.
A few things to try. If they don't work for you as permanent solutions, they should at least confirm that your code is working and you are dealing with a persmissions issue (which appears to be the case).
Move your file to the public folder (C:\Users\Public).
Run your application (or web browser) as an administrator (if applicable to your version of Windows).
If you are using a web browser, try using a different one.
If nothing else works, try pasting your code into a Windows Form Application.
If you still get the same error after trying all of this, it's time to take another look at your code. Remove the Try/Catch block to determine precisely which line is throwing the error. If you've tried hard coding, I'm guessing it's the SaveAs method. I'm not sure what class FileUpload1 is, but some SaveAs methods won't overwrite existing files unless you explicitly tell them to. Check the appropriate documentation and see if you don't need to pass a True value somewhere along with filename.
Update us with the results. At the very least, this should narrow down your problem and allow for a better diagnosis of it.
In a VS2013 VB.NET WinForms project I need to include an XML file in the deployed application that will be read from and written to at run time.
I have the file as an embedded resource, and have "Copy Always" selected for output. The file name is "Settings.xml" and the resource name is Settings.
Looking at this example I did the following to reference it in my code:
Private xmlFile as XmlDocument ' In the general declaration area, before the Load event
xmlFile.LoadXml(My.Resources.Settings) ' In the Load event, in a Try/Catch
But I get an "Object reference not set to an instance of an object" on the second line.
In the code I plan on accessing the xml with something like this:
Dim xmlDoc as New XmlDocument
xmlDoc = xmlFile
I'm not sure yet how to save any changes I make, as initial attempts of something like xmlDoc.Save(xmlFile) didn't work.
What am I missing?
First of all, you need to use the constructor for xmlFile:
Private xmlFile As New XmlDocument
Then, all you need to do is use the resource name, i.e. "Settings.xml" if the resource is embedded:
xmlFile.LoadXml("Settings.xml")
You shouldn't even need to set the resource as "CopyAlways".
`Dim medium As String = TextBox1.Text
Dim data_xml = XElement.Load("Assets\Manager.xml")
'next quaries the xml for desired attributes
Dim query = From DataTable1 In data_xml.Descendants("DataTable1")
Where (DataTable1.Attribute("Medium").Value = medium)
Select Uname = DataTable1.Attribute("Username").Value
For Each result In query
'displays results to textbox
TextBox2.Text = result
Next
I try to use this code to read from an xml file in the assets but the file does not seem to exist
At the declaration of the xelement.load("Assets\Manager.xml") I get this error
An exception of type 'System.IO.FileNotFoundException' occurred in mscorlib.ni.dll but was not handled in user code
Additional information: Could not find file 'C:\Data\SharedData\PhoneTools\AppxLayouts\d5d3a1e7-56d7-477c-bcd2-f949f3374de1VS.Debug_AnyCPU.NAME\Assets\Manager.xml'.
If there is a handler for this exception, the program may be safely continued.
Any ideas?
Steps to make it work:
Change Manager.xml Build action to Content
Change Copy to output directory to Copy if newer
Load file using XElement.Load("Assets/Manager.xml");
Edit:
Ok, since you are not willing to share more of the code even though you ask for help, there's nothing else I can do but put up an example :)
"Software" below, implemented in Visual Basic, reads and displays XML file named Manager.xml, that is set as Content in WP8 project. Working example can be found at https://github.com/mikkoviitala/read-content-xml